GeezerVison is showing github.com — test: add tests for query language package · docarray/docarray@824d69a
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings docarray/utils/query_language tests/units/util/query_language
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docarray/utils/query_language/lookup.py +7-9Lines changed: 7 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 31 | 31 |
| 32 | 32 | from functools import partial | 33 | 33 |
| 34 | | -if TYPE_CHECKING: # pragma: no cover | 35 | | - from docarray import BaseDocument | 36 | | - | 37 | 34 | PLACEHOLDER_PATTERN = re.compile(r'\{\s*([a-zA-Z0-9_]*)\s*}') | 38 | 35 |
| 39 | 36 |
| @@ -79,7 +76,7 @@ def point_get(_dict: Any, key: str) -> Any: | 79 | 76 | return point_get(result, part2) if part2 else result | 80 | 77 |
| 81 | 78 |
| 82 | | -def lookup(key: str, val: Any, doc: 'BaseDocument') -> bool: | | 79 | +def lookup(key: str, val: Any, doc: Any) -> bool: | 83 | 80 | """Checks if key-val pair exists in doc using various lookup types | 84 | 81 |
| 85 | 82 | The lookup types are derived from the `key` and then used to check | @@ -162,7 +159,10 @@ def lookup(key: str, val: Any, doc: 'BaseDocument') -> bool: | 162 | 159 | raise ValueError( | 163 | 160 | '$exists operator can only accept True/False as value for comparison' | 164 | 161 | ) | 165 | | - return val and field_exists | | 162 | + if val: | | 163 | + return field_exists | | 164 | + else: | | 165 | + return not field_exists | 166 | 166 | else: | 167 | 167 | raise ValueError( | 168 | 168 | f'The given compare operator "{last}" (derived from "{key}")' | @@ -215,7 +215,7 @@ def __init__(self, op: Union[str, bool] = 'and', negate: bool = False): | 215 | 215 | def add_child(self, child): | 216 | 216 | self.children.append(child) | 217 | 217 |
| 218 | | - def evaluate(self, doc: 'BaseDocument'): | | 218 | + def evaluate(self, doc: Any): | 219 | 219 | """Evaluates the expression represented by the object for the document | 220 | 220 |
| 221 | 221 | :param doc : the document to match | @@ -243,7 +243,7 @@ def __init__(self, **kwargs): | 243 | 243 | super(LookupLeaf, self).__init__() | 244 | 244 | self.lookups = kwargs | 245 | 245 |
| 246 | | - def evaluate(self, doc: 'BaseDocument'): | | 246 | + def evaluate(self, doc: Any): | 247 | 247 | """Evaluates the expression represented by the object for the document | 248 | 248 |
| 249 | 249 | :param doc : the document to match | @@ -312,8 +312,6 @@ def guard_type(classinfo, val): | 312 | 312 |
| 313 | 313 |
| 314 | 314 | guard_str = partial(guard_type, str) | 315 | | -guard_list = partial(guard_type, list) | 316 | | -guard_Q = partial(guard_type, Q) | 317 | 315 |
| 318 | 316 |
| 319 | 317 | def guard_iter(val): | |
Collapse file Copy file name to clipboardExpand all lines: tests/units/util/query_language/__init__.py Collapse file Copy file name to clipboard +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import pytest | | 2 | +from docarray.utils.query_language.lookup import point_get, lookup | | 3 | + | | 4 | + | | 5 | +class A: | | 6 | + class B: | | 7 | + c = 0 | | 8 | + d = 'docarray' | | 9 | + e = [0, 1] | | 10 | + f = {} | | 11 | + | | 12 | + b: B = B() | | 13 | + | | 14 | + | | 15 | +@pytest.mark.parametrize('input', [A(), {'b': {'c': 0, 'd': 'docarray'}}]) | | 16 | +def test_point_get(input): | | 17 | + assert point_get(input, 'b.c') == 0 | | 18 | + | | 19 | + expected_exception = KeyError if isinstance(input, dict) else AttributeError | | 20 | + with pytest.raises(expected_exception): | | 21 | + _ = point_get(input, 'z') | | 22 | + | | 23 | + with pytest.raises(expected_exception): | | 24 | + _ = point_get(input, 'b.z') | | 25 | + | | 26 | + | | 27 | +@pytest.mark.parametrize('input', [A(), {'b': {'c': 0, 'd': 'docarray', 'e': [0, 1], 'f': {}}}]) | | 28 | +def test_lookup(input): | | 29 | + assert lookup('b.c__exact', 0, input) | | 30 | + assert not lookup('b.c__gt', 0, input) | | 31 | + assert lookup('b.c__gte', 0, input) | | 32 | + assert not lookup('b.c__lt', 0, input) | | 33 | + assert lookup('b.c__lte', 0, input) | | 34 | + assert lookup('b.d__regex', 'array*', input) | | 35 | + assert lookup('b.d__contains', 'array', input) | | 36 | + assert lookup('b.d__icontains', 'Array', input) | | 37 | + assert lookup('b.d__in', ['a', 'docarray'], input) | | 38 | + assert lookup('b.d__nin', ['a', 'b'], input) | | 39 | + assert lookup('b.d__startswith', 'doc', input) | | 40 | + assert lookup('b.d__istartswith', 'Doc', input) | | 41 | + assert lookup('b.d__endswith', 'array', input) | | 42 | + assert lookup('b.d__iendswith', 'Array', input) | | 43 | + assert lookup('b.e__size', 2, input) | | 44 | + assert not lookup('b.e__size', 3, input) | | 45 | + assert lookup('b.d__size', len('docarray'), input) | | 46 | + assert not lookup('b.e__size', len('docarray') + 1, input) | | 47 | + assert not lookup('b.z__exists', True, input) | | 48 | + assert lookup('b.z__exists', False, input) | | 49 | + assert not lookup('b.f.z__exists', True, input) | | 50 | + assert lookup('b.f.z__exists', False, input) |
|