|
| 1 | +import dataclasses |
| 2 | +import typing as t |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from libtmux._internal.query_list import ( |
| 7 | + MultipleObjectsReturned, |
| 8 | + ObjectDoesNotExist, |
| 9 | + QueryList, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@dataclasses.dataclass |
| 14 | +class Obj: |
| 15 | + test: int |
| 16 | + fruit: list[str] = dataclasses.field(default_factory=list) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + "items,filter_expr,expected_result", |
| 21 | + [ |
| 22 | + [[Obj(test=1)], None, [Obj(test=1)]], |
| 23 | + [[Obj(test=1)], {"test": 1}, [Obj(test=1)]], |
| 24 | + [[Obj(test=1)], {"test": 2}, []], |
| 25 | + [ |
| 26 | + [Obj(test=2, fruit=["apple"])], |
| 27 | + {"fruit__in": "apple"}, |
| 28 | + QueryList([Obj(test=2, fruit=["apple"])]), |
| 29 | + ], |
| 30 | + [[{"test": 1}], None, [{"test": 1}]], |
| 31 | + [[{"test": 1}], None, QueryList([{"test": 1}])], |
| 32 | + [[{"fruit": "apple"}], None, QueryList([{"fruit": "apple"}])], |
| 33 | + [ |
| 34 | + [{"fruit": "apple", "banana": object()}], |
| 35 | + None, |
| 36 | + QueryList([{"fruit": "apple", "banana": object()}]), |
| 37 | + ], |
| 38 | + [ |
| 39 | + [{"fruit": "apple", "banana": object()}], |
| 40 | + {"fruit__eq": "apple"}, |
| 41 | + QueryList([{"fruit": "apple", "banana": object()}]), |
| 42 | + ], |
| 43 | + [ |
| 44 | + [{"fruit": "apple", "banana": object()}], |
| 45 | + {"fruit__eq": "notmatch"}, |
| 46 | + QueryList([]), |
| 47 | + ], |
| 48 | + [ |
| 49 | + [{"fruit": "apple", "banana": object()}], |
| 50 | + {"fruit__exact": "apple"}, |
| 51 | + QueryList([{"fruit": "apple", "banana": object()}]), |
| 52 | + ], |
| 53 | + [ |
| 54 | + [{"fruit": "apple", "banana": object()}], |
| 55 | + {"fruit__exact": "notmatch"}, |
| 56 | + QueryList([]), |
| 57 | + ], |
| 58 | + [ |
| 59 | + [{"fruit": "apple", "banana": object()}], |
| 60 | + {"fruit__iexact": "Apple"}, |
| 61 | + QueryList([{"fruit": "apple", "banana": object()}]), |
| 62 | + ], |
| 63 | + [ |
| 64 | + [{"fruit": "apple", "banana": object()}], |
| 65 | + {"fruit__iexact": "Notmatch"}, |
| 66 | + QueryList([]), |
| 67 | + ], |
| 68 | + [ |
| 69 | + [{"fruit": "apple", "banana": object()}], |
| 70 | + {"fruit": "notmatch"}, |
| 71 | + QueryList([]), |
| 72 | + ], |
| 73 | + [ |
| 74 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 75 | + {"fruit": "apple"}, |
| 76 | + [{"fruit": "apple"}], |
| 77 | + ], |
| 78 | + [ |
| 79 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 80 | + {"fruit__in": "app"}, |
| 81 | + [{"fruit": "apple"}], |
| 82 | + ], |
| 83 | + [ |
| 84 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 85 | + {"fruit__icontains": "App"}, |
| 86 | + [{"fruit": "apple"}], |
| 87 | + ], |
| 88 | + [ |
| 89 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 90 | + {"fruit__contains": "app"}, |
| 91 | + [{"fruit": "apple"}], |
| 92 | + ], |
| 93 | + [ |
| 94 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 95 | + {"fruit__regex": r"app.*"}, |
| 96 | + [{"fruit": "apple"}], |
| 97 | + ], |
| 98 | + [ |
| 99 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 100 | + {"fruit__iregex": r"App.*"}, |
| 101 | + [{"fruit": "apple"}], |
| 102 | + ], |
| 103 | + [ |
| 104 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 105 | + {"fruit__startswith": "a"}, |
| 106 | + [{"fruit": "apple"}], |
| 107 | + ], |
| 108 | + [ |
| 109 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 110 | + {"fruit__istartswith": "AP"}, |
| 111 | + [{"fruit": "apple"}], |
| 112 | + ], |
| 113 | + [ |
| 114 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 115 | + {"fruit__startswith": "z"}, |
| 116 | + [], |
| 117 | + ], |
| 118 | + [ |
| 119 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 120 | + {"fruit__endswith": "le"}, |
| 121 | + [{"fruit": "apple"}], |
| 122 | + ], |
| 123 | + [ |
| 124 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 125 | + {"fruit__iendswith": "LE"}, |
| 126 | + [{"fruit": "apple"}], |
| 127 | + ], |
| 128 | + [ |
| 129 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 130 | + {"fruit__endswith": "z"}, |
| 131 | + [], |
| 132 | + ], |
| 133 | + [ |
| 134 | + [ |
| 135 | + {"fruit": "apple"}, |
| 136 | + {"fruit": "mango"}, |
| 137 | + {"fruit": "banana"}, |
| 138 | + {"fruit": "kiwi"}, |
| 139 | + ], |
| 140 | + {"fruit__in": ["apple", "mango"]}, |
| 141 | + [{"fruit": "apple"}, {"fruit": "mango"}], |
| 142 | + ], |
| 143 | + [ |
| 144 | + [ |
| 145 | + {"fruit": "apple"}, |
| 146 | + {"fruit": "mango"}, |
| 147 | + {"fruit": "banana"}, |
| 148 | + {"fruit": "kiwi"}, |
| 149 | + ], |
| 150 | + {"fruit__nin": ["apple", "mango"]}, |
| 151 | + [{"fruit": "banana"}, {"fruit": "kiwi"}], |
| 152 | + ], |
| 153 | + [ |
| 154 | + [ |
| 155 | + {"place": "book store", "city": "Tampa", "state": "Florida"}, |
| 156 | + {"place": "coffee shop", "city": "Tampa", "state": "Florida"}, |
| 157 | + { |
| 158 | + "place": "chinese restaurant", |
| 159 | + "city": "ybor city", |
| 160 | + "state": "Florida", |
| 161 | + }, |
| 162 | + { |
| 163 | + "place": "walt disney world", |
| 164 | + "city": "Lake Buena Vista", |
| 165 | + "state": "Florida", |
| 166 | + }, |
| 167 | + ], |
| 168 | + {"city": "Tampa", "state": "Florida"}, |
| 169 | + [ |
| 170 | + {"place": "book store", "city": "Tampa", "state": "Florida"}, |
| 171 | + {"place": "coffee shop", "city": "Tampa", "state": "Florida"}, |
| 172 | + ], |
| 173 | + ], |
| 174 | + [ |
| 175 | + [ |
| 176 | + {"place": "book store", "city": "Tampa", "state": "Florida"}, |
| 177 | + {"place": "coffee shop", "city": "Tampa", "state": "Florida"}, |
| 178 | + { |
| 179 | + "place": "chinese restaurant", |
| 180 | + "city": "ybor city", |
| 181 | + "state": "Florida", |
| 182 | + }, |
| 183 | + { |
| 184 | + "place": "walt disney world", |
| 185 | + "city": "Lake Buena Vista", |
| 186 | + "state": "Florida", |
| 187 | + }, |
| 188 | + ], |
| 189 | + {"place__contains": "coffee", "state": "Florida"}, |
| 190 | + [ |
| 191 | + {"place": "coffee shop", "city": "Tampa", "state": "Florida"}, |
| 192 | + ], |
| 193 | + ], |
| 194 | + [ |
| 195 | + [ |
| 196 | + { |
| 197 | + "place": "Largo", |
| 198 | + "city": "Tampa", |
| 199 | + "state": "Florida", |
| 200 | + "foods": {"fruit": ["banana", "orange"], "breakfast": "cereal"}, |
| 201 | + }, |
| 202 | + { |
| 203 | + "place": "Chicago suburbs", |
| 204 | + "city": "Elmhurst", |
| 205 | + "state": "Illinois", |
| 206 | + "foods": {"fruit": ["apple", "cantelope"], "breakfast": "waffles"}, |
| 207 | + }, |
| 208 | + ], |
| 209 | + {"foods__fruit__contains": "banana"}, |
| 210 | + [ |
| 211 | + { |
| 212 | + "place": "Largo", |
| 213 | + "city": "Tampa", |
| 214 | + "state": "Florida", |
| 215 | + "foods": {"fruit": ["banana", "orange"], "breakfast": "cereal"}, |
| 216 | + }, |
| 217 | + ], |
| 218 | + ], |
| 219 | + [ |
| 220 | + [ |
| 221 | + { |
| 222 | + "place": "Largo", |
| 223 | + "city": "Tampa", |
| 224 | + "state": "Florida", |
| 225 | + "foods": {"fruit": ["banana", "orange"], "breakfast": "cereal"}, |
| 226 | + }, |
| 227 | + { |
| 228 | + "place": "Chicago suburbs", |
| 229 | + "city": "Elmhurst", |
| 230 | + "state": "Illinois", |
| 231 | + "foods": {"fruit": ["apple", "cantelope"], "breakfast": "waffles"}, |
| 232 | + }, |
| 233 | + ], |
| 234 | + {"foods__breakfast": "cereal"}, |
| 235 | + [ |
| 236 | + { |
| 237 | + "place": "Largo", |
| 238 | + "city": "Tampa", |
| 239 | + "state": "Florida", |
| 240 | + "foods": {"fruit": ["banana", "orange"], "breakfast": "cereal"}, |
| 241 | + }, |
| 242 | + ], |
| 243 | + ], |
| 244 | + [[1, 2, 3, 4, 5], None, QueryList([1, 2, 3, 4, 5])], |
| 245 | + [[1, 2, 3, 4, 5], [1], QueryList([1])], |
| 246 | + [[1, 2, 3, 4, 5], [1, 4], QueryList([1, 4])], |
| 247 | + [[1, 2, 3, 4, 5], lambda val: val == 1, QueryList([1])], |
| 248 | + [[1, 2, 3, 4, 5], lambda val: val == 2, QueryList([2])], |
| 249 | + ], |
| 250 | +) |
| 251 | +def test_filter( |
| 252 | + items: list[dict[str, t.Any]], |
| 253 | + filter_expr: t.Optional[t.Union[t.Callable[[t.Any], bool], t.Any]], |
| 254 | + expected_result: t.Union[QueryList[t.Any], list[dict[str, t.Any]]], |
| 255 | +) -> None: |
| 256 | + qs = QueryList(items) |
| 257 | + if filter_expr is not None: |
| 258 | + if isinstance(filter_expr, dict): |
| 259 | + assert qs.filter(**filter_expr) == expected_result |
| 260 | + else: |
| 261 | + assert qs.filter(filter_expr) == expected_result |
| 262 | + else: |
| 263 | + assert qs.filter() == expected_result |
| 264 | + |
| 265 | + if ( |
| 266 | + isinstance(expected_result, list) |
| 267 | + and len(expected_result) > 0 |
| 268 | + and not isinstance(expected_result[0], dict) |
| 269 | + ): |
| 270 | + if len(expected_result) == 1: |
| 271 | + if isinstance(filter_expr, dict): |
| 272 | + assert qs.get(**filter_expr) == expected_result[0] |
| 273 | + else: |
| 274 | + assert qs.get(filter_expr) == expected_result[0] |
| 275 | + elif len(expected_result) > 1: |
| 276 | + with pytest.raises(MultipleObjectsReturned) as e: |
| 277 | + if isinstance(filter_expr, dict): |
| 278 | + assert qs.get(**filter_expr) == expected_result |
| 279 | + else: |
| 280 | + assert qs.get(filter_expr) == expected_result |
| 281 | + assert e.match("Multiple objects returned") |
| 282 | + elif len(expected_result) == 0: |
| 283 | + with pytest.raises(ObjectDoesNotExist) as exc: |
| 284 | + if isinstance(filter_expr, dict): |
| 285 | + assert qs.get(**filter_expr) == expected_result |
| 286 | + else: |
| 287 | + assert qs.get(filter_expr) == expected_result |
| 288 | + assert exc.match("No objects found") |
0 commit comments