Skip to content

Commit a8aa488

Browse files
committed
test(query_list): fix type errors and line length issues
why: Tests had type errors and lines exceeding max length what: - Added type ignores for intentional invalid type tests\n- Added None checks before indexing in get() tests\n- Fixed line length issues by moving comments to separate lines
1 parent ad3192e commit a8aa488

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

tests/_internal/test_query_list.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
from libtmux._internal.query_list import (
1010
MultipleObjectsReturned,
1111
ObjectDoesNotExist,
12+
PKRequiredException,
1213
QueryList,
1314
keygetter,
1415
lookup_contains,
1516
lookup_exact,
1617
lookup_icontains,
1718
lookup_iexact,
19+
lookup_in,
20+
lookup_iregex,
21+
lookup_nin,
22+
lookup_regex,
1823
parse_lookup,
1924
)
2025

@@ -402,3 +407,113 @@ def test_query_list_methods() -> None:
402407
# Test bool
403408
assert bool(ql) is True
404409
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

Comments
 (0)