Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,37 @@ def test(value, minimum, maximum):
assert hash(query)


def test_custom_with_unhashable_params():
"""Test that Query.test works when extra args contain lists or dicts."""

def in_allowed(value, allowed):
return value in allowed

# list argument must not raise TypeError when hashing the query
query = Query().val.test(in_allowed, [1, 2, 3])
assert query({'val': 1})
assert not query({'val': 4})
assert hash(query)

# dict argument must not raise TypeError when hashing the query
def has_key(value, mapping):
return value in mapping

query2 = Query().val.test(has_key, {'a': 1, 'b': 2})
assert query2({'val': 'a'})
assert not query2({'val': 'c'})
assert hash(query2)

# identical args produce the same hash (cache key correctness)
q_a = Query().val.test(in_allowed, [1, 2, 3])
q_b = Query().val.test(in_allowed, [1, 2, 3])
assert hash(q_a) == hash(q_b)

# different args produce different hashes
q_c = Query().val.test(in_allowed, [1, 2, 4])
assert hash(q_a) != hash(q_c)


def test_any():
query = Query().followers.any(Query().name == 'don')

Expand Down
2 changes: 1 addition & 1 deletion tinydb/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test(self, func: Callable[[Mapping], bool], *args) -> QueryInstance:
"""
return self._generate_test(
lambda value: func(value, *args),
('test', self._path, func, args)
('test', self._path, func, tuple(freeze(arg) for arg in args))
)

def any(self, cond: Union[QueryInstance, list[Any]]) -> QueryInstance:
Expand Down