|
| 1 | +import unittest |
| 2 | +from cloudinary_cli.utils.search_utils import ( |
| 3 | + parse_aggregate, |
| 4 | + parse_json_aggregate, |
| 5 | + parse_aggregate_string, |
| 6 | + parse_range_definition, |
| 7 | + parse_range_bounds |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class TestAggregateParsing(unittest.TestCase): |
| 12 | + |
| 13 | + # --- Tests for parse_json_aggregate --- |
| 14 | + |
| 15 | + def test_parse_json_aggregate_valid(self): |
| 16 | + s = '{"type": "bytes", "ranges": [{"key": "tiny", "to": 500}]}' |
| 17 | + result = parse_json_aggregate(s) |
| 18 | + expected = {"type": "bytes", "ranges": [{"key": "tiny", "to": 500}]} |
| 19 | + self.assertEqual(expected, result) |
| 20 | + |
| 21 | + def test_parse_json_aggregate_invalid_json(self): |
| 22 | + s = '{"type": "bytes", "ranges": [{"key": "tiny", "to": 500}' # missing closing ] |
| 23 | + with self.assertRaises(ValueError): |
| 24 | + parse_json_aggregate(s) |
| 25 | + |
| 26 | + def test_parse_json_aggregate_missing_type(self): |
| 27 | + s = '{"ranges": [{"key": "tiny", "to": 500}]}' |
| 28 | + with self.assertRaises(ValueError): |
| 29 | + parse_json_aggregate(s) |
| 30 | + |
| 31 | + # --- Tests for parse_aggregate_string --- |
| 32 | + |
| 33 | + def test_parse_aggregate_string_valid(self): |
| 34 | + s = "bytes:tiny_-500,medium_501-1999,big_2000-" |
| 35 | + result = parse_aggregate_string(s) |
| 36 | + expected = { |
| 37 | + "type": "bytes", |
| 38 | + "ranges": [ |
| 39 | + {"key": "tiny", "to": 500}, |
| 40 | + {"key": "medium", "from": 501, "to": 1999}, |
| 41 | + {"key": "big", "from": 2000} |
| 42 | + ] |
| 43 | + } |
| 44 | + self.assertEqual(expected, result) |
| 45 | + |
| 46 | + def test_parse_aggregate_string_no_colon(self): |
| 47 | + s = "format" |
| 48 | + result = parse_aggregate_string(s) |
| 49 | + self.assertEqual(s, result) |
| 50 | + |
| 51 | + # --- Tests for parse_aggregate (supports list and non-string inputs) --- |
| 52 | + |
| 53 | + def test_parse_aggregate_simple_string(self): |
| 54 | + s = "format" |
| 55 | + result = parse_aggregate(s) |
| 56 | + self.assertEqual([s], result) |
| 57 | + |
| 58 | + def test_parse_aggregate_json(self): |
| 59 | + s = '{"type": "bytes", "ranges": [{"key": "tiny", "to": 500}]}' |
| 60 | + result = parse_aggregate(s) |
| 61 | + expected = [{"type": "bytes", "ranges": [{"key": "tiny", "to": 500}]}] |
| 62 | + self.assertEqual(expected, result) |
| 63 | + |
| 64 | + def test_parse_aggregate_transformation_string(self): |
| 65 | + s = "bytes:tiny_-500,medium_501-1999,big_2000-" |
| 66 | + result = parse_aggregate(s) |
| 67 | + expected = [{ |
| 68 | + "type": "bytes", |
| 69 | + "ranges": [ |
| 70 | + {"key": "tiny", "to": 500}, |
| 71 | + {"key": "medium", "from": 501, "to": 1999}, |
| 72 | + {"key": "big", "from": 2000} |
| 73 | + ] |
| 74 | + }] |
| 75 | + self.assertEqual(expected, result) |
| 76 | + |
| 77 | + def test_parse_aggregate_list_input(self): |
| 78 | + input_list = [ |
| 79 | + "format", |
| 80 | + "bytes:tiny_-500,medium_501-1999,big_2000-" |
| 81 | + ] |
| 82 | + result = parse_aggregate(input_list) |
| 83 | + expected = [ |
| 84 | + "format", |
| 85 | + { |
| 86 | + "type": "bytes", |
| 87 | + "ranges": [ |
| 88 | + {"key": "tiny", "to": 500}, |
| 89 | + {"key": "medium", "from": 501, "to": 1999}, |
| 90 | + {"key": "big", "from": 2000} |
| 91 | + ] |
| 92 | + } |
| 93 | + ] |
| 94 | + self.assertEqual(expected, result) |
| 95 | + |
| 96 | + def test_parse_aggregate_non_string(self): |
| 97 | + # If a non-string (e.g. dict) is passed, build_array wraps it, and it is returned as is. |
| 98 | + d = {"type": "custom", "value": 123} |
| 99 | + result = parse_aggregate(d) |
| 100 | + self.assertEqual([d], result) |
| 101 | + |
| 102 | + # --- Tests for parse_range_definition --- |
| 103 | + |
| 104 | + def test_parse_range_definition_valid_tiny(self): |
| 105 | + part = "tiny_-500" |
| 106 | + result = parse_range_definition(part) |
| 107 | + expected = {"key": "tiny", "to": 500} |
| 108 | + self.assertEqual(expected, result) |
| 109 | + |
| 110 | + def test_parse_range_definition_valid_medium(self): |
| 111 | + part = "medium_501-1999" |
| 112 | + result = parse_range_definition(part) |
| 113 | + expected = {"key": "medium", "from": 501, "to": 1999} |
| 114 | + self.assertEqual(expected, result) |
| 115 | + |
| 116 | + def test_parse_range_definition_valid_big(self): |
| 117 | + part = "big_2000-" |
| 118 | + result = parse_range_definition(part) |
| 119 | + expected = {"key": "big", "from": 2000} |
| 120 | + self.assertEqual(expected, result) |
| 121 | + |
| 122 | + def test_parse_range_definition_missing_underscore(self): |
| 123 | + part = "big2000-" |
| 124 | + with self.assertRaises(ValueError): |
| 125 | + parse_range_definition(part) |
| 126 | + |
| 127 | + def test_parse_range_definition_missing_dash(self): |
| 128 | + part = "big_2000" |
| 129 | + with self.assertRaises(ValueError): |
| 130 | + parse_range_definition(part) |
| 131 | + |
| 132 | + # --- Tests for parse_range_bounds --- |
| 133 | + |
| 134 | + def test_parse_range_bounds_whole_numbers(self): |
| 135 | + value = "501-1999" |
| 136 | + result = parse_range_bounds(value, "test") |
| 137 | + expected = (501, 1999) |
| 138 | + self.assertEqual(expected, result) |
| 139 | + |
| 140 | + def test_parse_range_bounds_floats(self): |
| 141 | + value = "24.5-29.97" |
| 142 | + result = parse_range_bounds(value, "test") |
| 143 | + expected = (24.5, 29.97) |
| 144 | + self.assertEqual(expected, result) |
| 145 | + |
| 146 | + def test_parse_range_bounds_empty_from(self): |
| 147 | + value = "-500" |
| 148 | + result = parse_range_bounds(value, "test") |
| 149 | + expected = (None, 500) |
| 150 | + self.assertEqual(expected, result) |
| 151 | + |
| 152 | + def test_parse_range_bounds_empty_to(self): |
| 153 | + value = "2000-" |
| 154 | + result = parse_range_bounds(value, "test") |
| 155 | + expected = (2000, None) |
| 156 | + self.assertEqual(expected, result) |
| 157 | + |
| 158 | + def test_parse_range_bounds_invalid_from(self): |
| 159 | + value = "abc-100" |
| 160 | + with self.assertRaises(ValueError): |
| 161 | + parse_range_bounds(value, "test") |
| 162 | + |
| 163 | + def test_parse_range_bounds_invalid_to(self): |
| 164 | + value = "100-abc" |
| 165 | + with self.assertRaises(ValueError): |
| 166 | + parse_range_bounds(value, "test") |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + unittest.main() |
0 commit comments