|
2 | 2 |
|
3 | 3 | import dataclasses
|
4 | 4 | import typing as t
|
| 5 | +from contextlib import suppress |
5 | 6 |
|
6 | 7 | import pytest
|
7 | 8 |
|
8 | 9 | from libtmux._internal.query_list import (
|
9 | 10 | MultipleObjectsReturned,
|
10 | 11 | ObjectDoesNotExist,
|
11 | 12 | QueryList,
|
| 13 | + keygetter, |
| 14 | + lookup_contains, |
| 15 | + lookup_exact, |
| 16 | + lookup_icontains, |
| 17 | + lookup_iexact, |
| 18 | + parse_lookup, |
12 | 19 | )
|
13 | 20 |
|
14 | 21 | if t.TYPE_CHECKING:
|
@@ -291,3 +298,107 @@ def test_filter(
|
291 | 298 | else:
|
292 | 299 | assert qs.get(filter_expr) == expected_result
|
293 | 300 | assert exc.match("No objects found")
|
| 301 | + |
| 302 | + |
| 303 | +def test_keygetter_error_handling() -> None: |
| 304 | + """Test error handling in keygetter function.""" |
| 305 | + # Test accessing non-existent key |
| 306 | + obj: dict[str, int] = {"a": 1} |
| 307 | + assert keygetter(obj, "b") is None |
| 308 | + |
| 309 | + # Test accessing nested non-existent key |
| 310 | + nested_obj: dict[str, dict[str, int]] = {"a": {"b": 1}} |
| 311 | + assert keygetter(nested_obj, "a__c") is None |
| 312 | + |
| 313 | + # Test with invalid object type |
| 314 | + obj_none: t.Any = None |
| 315 | + with suppress(Exception): # Exception is expected and logged |
| 316 | + assert keygetter(obj_none, "any_key") is None |
| 317 | + |
| 318 | + |
| 319 | +def test_parse_lookup_error_handling() -> None: |
| 320 | + """Test error handling in parse_lookup function.""" |
| 321 | + # Test with invalid object |
| 322 | + assert parse_lookup({"field": "value"}, "field__contains", "__contains") is None |
| 323 | + |
| 324 | + # Test with invalid lookup |
| 325 | + obj: dict[str, str] = {"field": "value"} |
| 326 | + # Type ignore since we're testing error handling with invalid types |
| 327 | + assert parse_lookup(obj, "field", None) is None # type: ignore |
| 328 | + |
| 329 | + # Test with non-string path |
| 330 | + assert parse_lookup(obj, None, "__contains") is None # type: ignore |
| 331 | + |
| 332 | + |
| 333 | +def test_lookup_functions_edge_cases() -> None: |
| 334 | + """Test edge cases for lookup functions.""" |
| 335 | + # Test lookup_exact with non-string types |
| 336 | + assert lookup_exact("1", "1") |
| 337 | + assert not lookup_exact(["a", "b"], "test") |
| 338 | + assert not lookup_exact({"a": "1"}, "test") |
| 339 | + |
| 340 | + # Test lookup_iexact with non-string types |
| 341 | + assert not lookup_iexact(["a", "b"], "test") |
| 342 | + assert not lookup_iexact({"a": "1"}, "test") |
| 343 | + |
| 344 | + # Test lookup_contains with various types |
| 345 | + assert lookup_contains(["a", "b"], "a") |
| 346 | + assert not lookup_contains("123", "1") |
| 347 | + assert lookup_contains({"a": "1", "b": "2"}, "a") |
| 348 | + |
| 349 | + # Test lookup_icontains with various types |
| 350 | + assert not lookup_icontains("123", "1") |
| 351 | + assert lookup_icontains("TEST", "test") |
| 352 | + # Keys are case-insensitive |
| 353 | + assert lookup_icontains({"A": "1", "b": "2"}, "a") |
| 354 | + |
| 355 | + |
| 356 | +def test_query_list_get_error_cases() -> None: |
| 357 | + """Test error cases for QueryList.get method.""" |
| 358 | + ql = QueryList([{"id": 1}, {"id": 2}, {"id": 2}]) |
| 359 | + |
| 360 | + # Test get with no results |
| 361 | + with pytest.raises(ObjectDoesNotExist): |
| 362 | + ql.get(id=3) |
| 363 | + |
| 364 | + # Test get with multiple results |
| 365 | + with pytest.raises(MultipleObjectsReturned): |
| 366 | + ql.get(id=2) |
| 367 | + |
| 368 | + # Test get with default |
| 369 | + assert ql.get(id=3, default=None) is None |
| 370 | + |
| 371 | + |
| 372 | +def test_query_list_filter_error_cases() -> None: |
| 373 | + """Test error cases for QueryList.filter method.""" |
| 374 | + ql = QueryList([{"id": 1}, {"id": 2}]) |
| 375 | + |
| 376 | + # Test filter with invalid field |
| 377 | + assert len(ql.filter(nonexistent=1)) == 0 |
| 378 | + |
| 379 | + # Test filter with invalid lookup |
| 380 | + assert len(ql.filter(id__invalid="test")) == 0 |
| 381 | + |
| 382 | + |
| 383 | +def test_query_list_methods() -> None: |
| 384 | + """Test additional QueryList methods.""" |
| 385 | + ql = QueryList([1, 2, 3]) |
| 386 | + |
| 387 | + # Test len |
| 388 | + assert len(ql) == 3 |
| 389 | + |
| 390 | + # Test iter |
| 391 | + assert list(iter(ql)) == [1, 2, 3] |
| 392 | + |
| 393 | + # Test getitem |
| 394 | + assert ql[0] == 1 |
| 395 | + assert ql[1:] == QueryList([2, 3]) |
| 396 | + |
| 397 | + # Test eq |
| 398 | + assert ql == QueryList([1, 2, 3]) |
| 399 | + assert ql != QueryList([1, 2]) |
| 400 | + assert ql == [1, 2, 3] # QueryList should equal regular list with same contents |
| 401 | + |
| 402 | + # Test bool |
| 403 | + assert bool(ql) is True |
| 404 | + assert bool(QueryList([])) is False |
0 commit comments