|
9 | 9 | from libtmux._internal.query_list import (
|
10 | 10 | MultipleObjectsReturned,
|
11 | 11 | ObjectDoesNotExist,
|
| 12 | + PKRequiredException, |
12 | 13 | QueryList,
|
13 | 14 | keygetter,
|
14 | 15 | lookup_contains,
|
15 | 16 | lookup_exact,
|
16 | 17 | lookup_icontains,
|
17 | 18 | lookup_iexact,
|
| 19 | + lookup_in, |
| 20 | + lookup_iregex, |
| 21 | + lookup_nin, |
| 22 | + lookup_regex, |
18 | 23 | parse_lookup,
|
19 | 24 | )
|
20 | 25 |
|
@@ -402,3 +407,113 @@ def test_query_list_methods() -> None:
|
402 | 407 | # Test bool
|
403 | 408 | assert bool(ql) is True
|
404 | 409 | assert bool(QueryList([])) is False
|
| 410 | + |
| 411 | + |
| 412 | +def test_lookup_functions_additional_edge_cases() -> None: |
| 413 | + """Test additional edge cases for lookup functions.""" |
| 414 | + # Test lookup_in with various types |
| 415 | + assert not lookup_in("value", {"key": "value"}) # String in dict values |
| 416 | + assert not lookup_in("key", {"key": "value"}) # String in dict keys |
| 417 | + assert lookup_in("item", ["item", "other"]) # String in list |
| 418 | + assert not lookup_in("missing", {"key": "value"}) # Missing key in dict |
| 419 | + assert not lookup_in(123, "123") # Invalid type combination |
| 420 | + |
| 421 | + # Test lookup_nin with various types |
| 422 | + assert not lookup_nin( |
| 423 | + "missing", {"key": "value"} |
| 424 | + ) # Missing key in dict returns False |
| 425 | + assert not lookup_nin( |
| 426 | + "value", {"key": "value"} |
| 427 | + ) # String in dict values returns False |
| 428 | + assert lookup_nin("item", ["other", "another"]) # String not in list |
| 429 | + assert not lookup_nin("item", ["item", "other"]) # String in list |
| 430 | + assert not lookup_nin(123, "123") # Invalid type combination returns False |
| 431 | + |
| 432 | + # Test lookup_regex with various types |
| 433 | + assert lookup_regex("test123", r"\d+") # Match digits |
| 434 | + assert not lookup_regex("test", r"\d+") # No match |
| 435 | + assert not lookup_regex(123, r"\d+") # Invalid type |
| 436 | + assert not lookup_regex("test", 123) # Invalid pattern type |
| 437 | + |
| 438 | + # Test lookup_iregex with various types |
| 439 | + assert lookup_iregex("TEST123", r"test\d+") # Case-insensitive match |
| 440 | + assert not lookup_iregex("test", r"\d+") # No match |
| 441 | + assert not lookup_iregex(123, r"\d+") # Invalid type |
| 442 | + assert not lookup_iregex("test", 123) # Invalid pattern type |
| 443 | + |
| 444 | + |
| 445 | +def test_query_list_items() -> None: |
| 446 | + """Test QueryList items() method.""" |
| 447 | + # Test items() without pk_key |
| 448 | + ql = QueryList([{"id": 1}, {"id": 2}]) |
| 449 | + ql.pk_key = None # Initialize pk_key |
| 450 | + with pytest.raises(PKRequiredException): |
| 451 | + ql.items() |
| 452 | + |
| 453 | + |
| 454 | +def test_query_list_filter_with_invalid_op() -> None: |
| 455 | + """Test QueryList filter with invalid operator.""" |
| 456 | + ql = QueryList([{"id": 1}, {"id": 2}]) |
| 457 | + |
| 458 | + # Test filter with no operator (defaults to exact) |
| 459 | + result = ql.filter(id=1) |
| 460 | + assert len(result) == 1 |
| 461 | + assert result[0]["id"] == 1 |
| 462 | + |
| 463 | + # Test filter with valid operator |
| 464 | + result = ql.filter(id__exact=1) |
| 465 | + assert len(result) == 1 |
| 466 | + assert result[0]["id"] == 1 |
| 467 | + |
| 468 | + # Test filter with multiple conditions |
| 469 | + result = ql.filter(id__exact=1, id__in=[1, 2]) |
| 470 | + assert len(result) == 1 |
| 471 | + assert result[0]["id"] == 1 |
| 472 | + |
| 473 | + |
| 474 | +def test_query_list_filter_with_callable() -> None: |
| 475 | + """Test QueryList filter with callable.""" |
| 476 | + ql = QueryList([{"id": 1}, {"id": 2}, {"id": 3}]) |
| 477 | + |
| 478 | + # Test filter with callable |
| 479 | + def is_even(x: dict[str, int]) -> bool: |
| 480 | + return x["id"] % 2 == 0 |
| 481 | + |
| 482 | + filtered = ql.filter(is_even) |
| 483 | + assert len(filtered) == 1 |
| 484 | + assert filtered[0]["id"] == 2 |
| 485 | + |
| 486 | + # Test filter with lambda |
| 487 | + filtered = ql.filter(lambda x: x["id"] > 2) |
| 488 | + assert len(filtered) == 1 |
| 489 | + assert filtered[0]["id"] == 3 |
| 490 | + |
| 491 | + |
| 492 | +def test_query_list_get_with_callable() -> None: |
| 493 | + """Test QueryList get with callable.""" |
| 494 | + ql = QueryList([{"id": 1}, {"id": 2}, {"id": 3}]) |
| 495 | + |
| 496 | + # Test get with callable |
| 497 | + def get_id_2(x: dict[str, int]) -> bool: |
| 498 | + return x["id"] == 2 |
| 499 | + |
| 500 | + result = ql.get(get_id_2) |
| 501 | + assert result["id"] == 2 |
| 502 | + |
| 503 | + # Test get with lambda |
| 504 | + result = ql.get(lambda x: x["id"] == 3) |
| 505 | + assert result["id"] == 3 |
| 506 | + |
| 507 | + # Test get with callable returning multiple matches |
| 508 | + def get_id_greater_than_1(x: dict[str, int]) -> bool: |
| 509 | + return x["id"] > 1 |
| 510 | + |
| 511 | + with pytest.raises(MultipleObjectsReturned): |
| 512 | + ql.get(get_id_greater_than_1) |
| 513 | + |
| 514 | + # Test get with callable returning no matches |
| 515 | + def get_id_greater_than_10(x: dict[str, int]) -> bool: |
| 516 | + return x["id"] > 10 |
| 517 | + |
| 518 | + with pytest.raises(ObjectDoesNotExist): |
| 519 | + ql.get(get_id_greater_than_10) |
0 commit comments