GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docarray/utils/query_language
    • lookup.py
  • tests/units/util/query_language
    • __init__.py
    • test_lookup.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docarray/utils/query_language/lookup.py‎

Copy file name to clipboardExpand all lines: docarray/utils/query_language/lookup.py
+7-9Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131

3232
from functools import partial
3333

34-
if TYPE_CHECKING: # pragma: no cover
35-
from docarray import BaseDocument
36-
3734
PLACEHOLDER_PATTERN = re.compile(r'\{\s*([a-zA-Z0-9_]*)\s*}')
3835

3936

@@ -79,7 +76,7 @@ def point_get(_dict: Any, key: str) -> Any:
7976
return point_get(result, part2) if part2 else result
8077

8178

82-
def lookup(key: str, val: Any, doc: 'BaseDocument') -> bool:
79+
def lookup(key: str, val: Any, doc: Any) -> bool:
8380
"""Checks if key-val pair exists in doc using various lookup types
8481
8582
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:
162159
raise ValueError(
163160
'$exists operator can only accept True/False as value for comparison'
164161
)
165-
return val and field_exists
162+
if val:
163+
return field_exists
164+
else:
165+
return not field_exists
166166
else:
167167
raise ValueError(
168168
f'The given compare operator "{last}" (derived from "{key}")'
@@ -215,7 +215,7 @@ def __init__(self, op: Union[str, bool] = 'and', negate: bool = False):
215215
def add_child(self, child):
216216
self.children.append(child)
217217

218-
def evaluate(self, doc: 'BaseDocument'):
218+
def evaluate(self, doc: Any):
219219
"""Evaluates the expression represented by the object for the document
220220
221221
:param doc : the document to match
@@ -243,7 +243,7 @@ def __init__(self, **kwargs):
243243
super(LookupLeaf, self).__init__()
244244
self.lookups = kwargs
245245

246-
def evaluate(self, doc: 'BaseDocument'):
246+
def evaluate(self, doc: Any):
247247
"""Evaluates the expression represented by the object for the document
248248
249249
:param doc : the document to match
@@ -312,8 +312,6 @@ def guard_type(classinfo, val):
312312

313313

314314
guard_str = partial(guard_type, str)
315-
guard_list = partial(guard_type, list)
316-
guard_Q = partial(guard_type, Q)
317315

318316

319317
def guard_iter(val):
Collapse file

‎tests/units/util/query_language/__init__.py‎

Copy file name to clipboardExpand all lines: tests/units/util/query_language/__init__.py
Whitespace-only changes.
Collapse file

‎tests/units/util/query_language/test_lookup.py‎

Copy file name to clipboard
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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)

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.