Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_subclass():
# Test that a new query test method in a custom subclass is properly usable
class MyQueryClass(Query):
def equal_double(self, rhs):
return self._generate_test(
lambda value: value == rhs * 2,
('equal_double', self._path, rhs)
)
query = MyQueryClass().val.equal_double('42')
assert query({'val': '4242'})
assert not query({'val': '42'})
assert not query({'': None})
assert hash(query)
def test_custom():
def test(value):
return value == 42
query = Query().val.test(test)
assert query({'val': 42})
assert not query({'val': 40})
assert not query({'val': '44'})
assert not query({'': None})
assert hash(query)
def in_list(value, l):
return value in l
query = Query().val.test(in_list, tuple([25, 35]))
assert not query({'val': 20})
assert query({'val': 25})
assert not query({'val': 30})
assert query({'val': 35})
assert not query({'val': 36})
def test_path_and():
query = Query()['value'] & (Query()['value'] == 5)
assert query({'value': 5})
assert not query({'value': 10})
assert not query({'something': 1})
assert hash(query)
assert hash(query) != hash(where('value'))
def test_not():
query = ~ (Query().val1 == 1)
assert query({'val1': 5, 'val2': 2})
assert not query({'val1': 1, 'val2': 2})
assert hash(query)
query = (
(~ (Query().val1 == 1)) &
(Query().val2 == 2)
)
assert query({'val1': '', 'val2': 2})
assert query({'val2': 2})
assert not query({'val1': 1, 'val2': 2})
assert not query({'val1': 1})
assert not query({'val1': '', 'val2': ''})
assert hash(query)
def test_has_key():
query = Query().val3.exists()
assert query({'val3': 1})
assert not query({'val1': 1, 'val2': 2})
assert hash(query)
def test_regex():
query = Query().val.matches(r'\d{2}\.')
assert query({'val': '42.'})
assert not query({'val': '44'})
assert not query({'val': 'ab.'})
assert not query({'': None})
assert hash(query)
query = Query().val.search(r'\d+')
assert query({'val': 'ab3'})
assert not query({'val': 'abc'})
assert not query({'val': ''})
assert not query({'': None})
assert hash(query)
query = Query().val.search(r'JOHN', flags=re.IGNORECASE)
def test_all():
query = Query().followers.all(Query().name == 'don')
assert query({'followers': [{'name': 'don'}]})
assert not query({'followers': [{'name': 'don'}, {'name': 'john'}]})
assert hash(query)
query = Query().followers.all(Query().num.matches('\\d+'))
assert query({'followers': [{'num': '123'}, {'num': '456'}]})
assert not query({'followers': [{'num': '123'}, {'num': 'abc'}]})
assert hash(query)
query = Query().followers.all(['don', 'john'])
assert query({'followers': ['don', 'john', 'greg']})
assert not query({'followers': ['don', 'greg']})
assert not query({})
assert hash(query)
query = Query().followers.all([{'name': 'jane'}, {'name': 'john'}])
assert query({'followers': [{'name': 'john'}, {'name': 'jane'}]})
assert query({'followers': [{'name': 'john'},
{'name': 'jane'},
{'name': 'bob'}]})
assert not query({'followers': [{'name': 'john'}, {'name': 'bob'}]})
def test_any():
query = Query().followers.any(Query().name == 'don')
assert query({'followers': [{'name': 'don'}, {'name': 'john'}]})
assert not query({'followers': 1})
assert not query({})
assert hash(query)
query = Query().followers.any(Query().num.matches('\\d+'))
assert query({'followers': [{'num': '12'}, {'num': 'abc'}]})
assert not query({'followers': [{'num': 'abc'}]})
assert hash(query)
query = Query().followers.any(['don', 'jon'])
assert query({'followers': ['don', 'greg', 'bill']})
assert not query({'followers': ['greg', 'bill']})
assert not query({})
assert hash(query)
query = Query().followers.any([{'name': 'don'}, {'name': 'john'}])
assert query({'followers': [{'name': 'don'}, {'name': 'greg'}]})
assert not query({'followers': [{'name': 'greg'}]})
assert hash(query)
def test_all():
query = Query().followers.all(Query().name == 'don')
assert query({'followers': [{'name': 'don'}]})
assert not query({'followers': [{'name': 'don'}, {'name': 'john'}]})
assert hash(query)
query = Query().followers.all(Query().num.matches('\\d+'))
assert query({'followers': [{'num': '123'}, {'num': '456'}]})
assert not query({'followers': [{'num': '123'}, {'num': 'abc'}]})
assert hash(query)
query = Query().followers.all(['don', 'john'])
assert query({'followers': ['don', 'john', 'greg']})
assert not query({'followers': ['don', 'greg']})
assert not query({})
assert hash(query)
query = Query().followers.all([{'name': 'jane'}, {'name': 'john'}])
def test_not():
query = ~ (Query().val1 == 1)
assert query({'val1': 5, 'val2': 2})
assert not query({'val1': 1, 'val2': 2})
assert hash(query)
query = (
(~ (Query().val1 == 1)) &
(Query().val2 == 2)
)
assert query({'val1': '', 'val2': 2})
assert query({'val2': 2})
assert not query({'val1': 1, 'val2': 2})
assert not query({'val1': 1})
assert not query({'val1': '', 'val2': ''})
assert hash(query)