|
| 1 | +import unittest |
| 2 | + |
| 3 | +from adminapi.datatype import DatatypeError |
| 4 | +from adminapi.filters import ( |
| 5 | + BaseFilter, |
| 6 | + Regexp, |
| 7 | + Any, |
| 8 | + GreaterThan, |
| 9 | +) |
| 10 | +from adminapi.parse import parse_query, parse_function_string |
| 11 | + |
| 12 | + |
| 13 | +def assert_filters_equal(test_case, result, expected): |
| 14 | + """Compare filter dictionaries by their repr, which includes structure and values.""" |
| 15 | + test_case.assertEqual(sorted(result.keys()), sorted(expected.keys())) |
| 16 | + for key in expected: |
| 17 | + test_case.assertEqual(repr(result[key]), repr(expected[key])) |
| 18 | + |
| 19 | + |
| 20 | +class TestParseQuery(unittest.TestCase): |
| 21 | + def test_simple_attribute(self): |
| 22 | + result = parse_query("hostname=web01") |
| 23 | + expected = {"hostname": BaseFilter("web01")} |
| 24 | + assert_filters_equal(self, result, expected) |
| 25 | + |
| 26 | + def test_whitespace_handling(self): |
| 27 | + result = parse_query(" hostname=test ") |
| 28 | + expected = {"hostname": BaseFilter("test")} |
| 29 | + assert_filters_equal(self, result, expected) |
| 30 | + |
| 31 | + def test_multiple_attributes(self): |
| 32 | + result = parse_query("hostname=web01 state=online") |
| 33 | + expected = { |
| 34 | + "hostname": BaseFilter("web01"), |
| 35 | + "state": BaseFilter("online"), |
| 36 | + } |
| 37 | + assert_filters_equal(self, result, expected) |
| 38 | + |
| 39 | + def test_hostname_shorthand(self): |
| 40 | + result = parse_query("web01 state=online") |
| 41 | + expected = { |
| 42 | + "hostname": BaseFilter("web01"), |
| 43 | + "state": BaseFilter("online"), |
| 44 | + } |
| 45 | + assert_filters_equal(self, result, expected) |
| 46 | + |
| 47 | + def test_hostname_shorthand_with_regexp(self): |
| 48 | + # Hostname shortcuts automatically detect regex patterns |
| 49 | + result = parse_query("web.*") |
| 50 | + expected = {"hostname": Regexp("web.*")} |
| 51 | + assert_filters_equal(self, result, expected) |
| 52 | + |
| 53 | + def test_regexp_pattern_as_literal(self): |
| 54 | + # Regex patterns in attribute values are treated as literals |
| 55 | + # Use Regexp() function for actual regex filtering |
| 56 | + result = parse_query("hostname=web.*") |
| 57 | + expected = {"hostname": BaseFilter("web.*")} |
| 58 | + assert_filters_equal(self, result, expected) |
| 59 | + |
| 60 | + def test_explicit_regexp_function(self): |
| 61 | + # Use explicit Regexp() function for regex filtering |
| 62 | + result = parse_query("hostname=Regexp(web.*)") |
| 63 | + expected = {"hostname": Regexp("web.*")} |
| 64 | + assert_filters_equal(self, result, expected) |
| 65 | + |
| 66 | + def test_function_filter(self): |
| 67 | + result = parse_query("num_cores=GreaterThan(4)") |
| 68 | + expected = {"num_cores": GreaterThan(4)} |
| 69 | + assert_filters_equal(self, result, expected) |
| 70 | + |
| 71 | + def test_function_with_multiple_args(self): |
| 72 | + result = parse_query("hostname=Any(web01 web02)") |
| 73 | + expected = {"hostname": Any("web01", "web02")} |
| 74 | + assert_filters_equal(self, result, expected) |
| 75 | + |
| 76 | + def test_empty_query(self): |
| 77 | + result = parse_query("") |
| 78 | + self.assertEqual(result, {}) |
| 79 | + |
| 80 | + def test_whitespace_only_query(self): |
| 81 | + result = parse_query(" ") |
| 82 | + self.assertEqual(result, {}) |
| 83 | + |
| 84 | + def test_newline_in_query(self): |
| 85 | + result = parse_query("hostname=web01\nstate=online") |
| 86 | + expected = { |
| 87 | + "hostname": BaseFilter("web01"), |
| 88 | + "state": BaseFilter("online"), |
| 89 | + } |
| 90 | + assert_filters_equal(self, result, expected) |
| 91 | + |
| 92 | + def test_any_filter_with_duplicate_hostname(self): |
| 93 | + # Hostname shorthand triggers regex, but explicit attribute assignment doesn't |
| 94 | + # Order: explicit assignment value comes first, then shorthand value |
| 95 | + result = parse_query("web.* hostname=db.*") |
| 96 | + expected = {"hostname": Any(BaseFilter("db.*"), Regexp("web.*"))} |
| 97 | + assert_filters_equal(self, result, expected) |
| 98 | + |
| 99 | + def test_invalid_function(self): |
| 100 | + with self.assertRaisesRegex(DatatypeError, r"Invalid function InvalidFunc"): |
| 101 | + parse_query("hostname=InvalidFunc(test)") |
| 102 | + |
| 103 | + |
| 104 | + def test_top_level_literal_error(self): |
| 105 | + with self.assertRaisesRegex( |
| 106 | + DatatypeError, r"Invalid term: Top level literals are not allowed" |
| 107 | + ): |
| 108 | + parse_query("hostname=test value") |
| 109 | + |
| 110 | + def test_top_level_function_as_hostname(self): |
| 111 | + # Function syntax without key is treated as hostname shorthand |
| 112 | + result = parse_query("GreaterThan(4)") |
| 113 | + expected = {"hostname": BaseFilter("GreaterThan(4)")} |
| 114 | + assert_filters_equal(self, result, expected) |
| 115 | + |
| 116 | + def test_garbled_hostname_error(self): |
| 117 | + with self.assertRaisesRegex(DatatypeError, r"Garbled hostname: db01"): |
| 118 | + parse_query("web01", hostname="db01") |
| 119 | + |
| 120 | + |
| 121 | +class TestParseFunctionString(unittest.TestCase): |
| 122 | + def test_simple_key_value(self): |
| 123 | + result = parse_function_string("hostname=web01") |
| 124 | + self.assertEqual(result, [("key", "hostname"), ("literal", "web01")]) |
| 125 | + |
| 126 | + def test_quoted_string(self): |
| 127 | + result = parse_function_string('hostname="web 01"') |
| 128 | + self.assertEqual(result, [("key", "hostname"), ("literal", "web 01")]) |
| 129 | + |
| 130 | + def test_single_quoted_string(self): |
| 131 | + result = parse_function_string("hostname='web 01'") |
| 132 | + self.assertEqual(result, [("key", "hostname"), ("literal", "web 01")]) |
| 133 | + |
| 134 | + def test_escaped_quote(self): |
| 135 | + result = parse_function_string('hostname="web\\"01"') |
| 136 | + # Note: string_buf stores the actual chars, but the result is the original slice |
| 137 | + self.assertEqual(result[1][0], "literal") |
| 138 | + |
| 139 | + def test_function_call(self): |
| 140 | + result = parse_function_string("num_cores=GreaterThan(4)") |
| 141 | + expected = [ |
| 142 | + ("key", "num_cores"), |
| 143 | + ("func", "GreaterThan"), |
| 144 | + ("literal", 4), |
| 145 | + ("endfunc", ""), |
| 146 | + ] |
| 147 | + self.assertEqual(result, expected) |
| 148 | + |
| 149 | + def test_nested_function(self): |
| 150 | + result = parse_function_string("attr=Func1(Func2(value))") |
| 151 | + self.assertEqual(result[0], ("key", "attr")) |
| 152 | + self.assertEqual(result[1], ("func", "Func1")) |
| 153 | + self.assertEqual(result[2], ("func", "Func2")) |
| 154 | + |
| 155 | + def test_multiple_values(self): |
| 156 | + result = parse_function_string("host1 host2 host3") |
| 157 | + expected = [ |
| 158 | + ("literal", "host1"), |
| 159 | + ("literal", "host2"), |
| 160 | + ("literal", "host3"), |
| 161 | + ] |
| 162 | + self.assertEqual(result, expected) |
| 163 | + |
| 164 | + def test_datatype_conversion(self): |
| 165 | + result = parse_function_string("count=42") |
| 166 | + self.assertEqual(result, [("key", "count"), ("literal", 42)]) |
| 167 | + |
| 168 | + def test_unterminated_string(self): |
| 169 | + with self.assertRaisesRegex(DatatypeError, r"Unterminated string"): |
| 170 | + parse_function_string('hostname="web01', strict=True) |
| 171 | + |
| 172 | + def test_invalid_escape(self): |
| 173 | + with self.assertRaisesRegex(DatatypeError, r"Invalid escape"): |
| 174 | + parse_function_string('hostname="web\\01"', strict=True) |
| 175 | + |
| 176 | + |
| 177 | + def test_empty_string(self): |
| 178 | + result = parse_function_string("") |
| 179 | + self.assertEqual(result, []) |
| 180 | + |
| 181 | + def test_whitespace_only(self): |
| 182 | + result = parse_function_string(" ") |
| 183 | + self.assertEqual(result, []) |
| 184 | + |
| 185 | + def test_parentheses_handling(self): |
| 186 | + result = parse_function_string("func(a b)") |
| 187 | + expected = [ |
| 188 | + ("func", "func"), |
| 189 | + ("literal", "a"), |
| 190 | + ("literal", "b"), |
| 191 | + ("endfunc", ""), |
| 192 | + ] |
| 193 | + self.assertEqual(result, expected) |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + unittest.main() |
0 commit comments