Skip to content

Commit 482e1e3

Browse files
committed
Test coverage.
1 parent 3e40d74 commit 482e1e3

13 files changed

Lines changed: 204 additions & 21 deletions

‎jsonpath/env.py‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def compile(self, path: str) -> Union[JSONPath, CompoundJSONPath]: # noqa: A003
7575
selectors=self.parser.parse(stream),
7676
)
7777
)
78-
else:
78+
else: # pragma: no cover
79+
# Parser.parse catches this too
7980
raise JSONPathSyntaxError( # noqa: TRY003
8081
f"unexpected token {stream.current.value!r}",
8182
token=stream.current,
@@ -192,8 +193,9 @@ def compare(self, left: object, operator: str, right: object) -> bool:
192193
return left >= right
193194
if operator == "<":
194195
return left < right
195-
if operator == ">":
196-
return left > right
196+
197+
assert operator == ">"
198+
return left > right
197199

198200
# This will catch booleans too.
199201
if isinstance(left, (int, float)) and isinstance(right, (int, float)):
@@ -203,8 +205,9 @@ def compare(self, left: object, operator: str, right: object) -> bool:
203205
return left >= right
204206
if operator == "<":
205207
return left < right
206-
if operator == ">":
207-
return left > right
208+
209+
assert operator == ">"
210+
return left > right
208211

209212
if (
210213
isinstance(left, Mapping)

‎jsonpath/match.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(
2727
self._filter_context = filter_context
2828

2929
def __str__(self) -> str:
30-
return f"{_truncate(str(self.obj), 5)} @ {_truncate(self.path, 5)}"
30+
return f"{_truncate(str(self.obj), 5)!r} @ {_truncate(self.path, 5)}"
3131

3232
def filter_context(self) -> FilterContextVars:
3333
""""""

‎jsonpath/parse.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def parse_selector_list(self, stream: TokenStream) -> ListSelector:
296296
)
297297

298298
if stream.peek.kind != TOKEN_LIST_END:
299-
stream.expect_peek(TOKEN_COMMA)
300299
stream.next_token()
301300

302301
stream.next_token()

‎jsonpath/path.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ def findall(
5151
if isinstance(data, str):
5252
data = json.loads(data)
5353
return [
54-
match.obj
55-
for match in self.finditer(
56-
data,
57-
filter_context=filter_context,
58-
)
54+
match.obj for match in self.finditer(data, filter_context=filter_context)
5955
]
6056

6157
def finditer(
@@ -162,7 +158,8 @@ def findall(
162158
_objs = path.findall(data, filter_context=filter_context)
163159
if op == self.union_token:
164160
objs.extend(_objs)
165-
elif op == self.intersection_token:
161+
else:
162+
assert op == self.intersection_token, op
166163
objs = [obj for obj in objs if obj in _objs]
167164

168165
return objs
@@ -180,9 +177,10 @@ def finditer(
180177
_matches = path.finditer(data, filter_context=filter_context)
181178
if op == self.union_token:
182179
matches = itertools.chain(matches, _matches)
183-
elif op == self.intersection_token:
180+
else:
181+
assert op == self.intersection_token
184182
_objs = [match.obj for match in _matches]
185-
_matches = (match for match in matches if match.obj in _objs)
183+
matches = (match for match in matches if match.obj in _objs)
186184

187185
return matches
188186

@@ -198,7 +196,8 @@ async def findall_async(
198196
_objs = await path.findall_async(data, filter_context=filter_context)
199197
if op == self.union_token:
200198
objs.extend(_objs)
201-
elif op == self.intersection_token:
199+
else:
200+
assert op == self.intersection_token
202201
objs = [obj for obj in objs if obj in _objs]
203202

204203
return objs
@@ -216,9 +215,10 @@ async def finditer_async(
216215
_matches = await path.finditer_async(data, filter_context=filter_context)
217216
if op == self.union_token:
218217
matches = _achain(matches, _matches)
219-
elif op == self.intersection_token:
218+
else:
219+
assert op == self.intersection_token
220220
_objs = [match.obj async for match in _matches]
221-
_matches = (match async for match in matches if match.obj in _objs)
221+
matches = (match async for match in matches if match.obj in _objs)
222222

223223
return matches
224224

‎jsonpath/stream.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,6 @@ def expect_peek(self, typ: str) -> None:
8686
""""""
8787
if self.peek.kind != typ:
8888
raise JSONPathSyntaxError(
89-
f"expected {typ!r}, found {self.current.kind!r}",
89+
f"expected {typ!r}, found {self.peek.kind!r}",
9090
token=self.peek,
9191
)

‎jsonpath/token.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(
9191
self.index = index
9292
self.path = path
9393

94-
def __repr__(self) -> str:
94+
def __repr__(self) -> str: # pragma: no cover
9595
return (
9696
f"Token(kind={self.kind!r}, value={self.value!r}, "
9797
f"index={self.index}, path={self.path!r})"

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test = "pytest {args}"
6363
[tool.coverage.run]
6464
branch = true
6565
parallel = true
66-
omit = ["jsonpath/__about__.py"]
66+
omit = ["jsonpath/__about__.py", "tests/compliance.py", "tests/consensus.py"]
6767

6868
[tool.coverage.report]
6969
exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]

‎tests/test_async.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import asyncio
2+
from collections.abc import Mapping
3+
from typing import Iterator
4+
from typing import List
5+
6+
import jsonpath
7+
8+
9+
class MockLazyMapping(Mapping[str, object]):
10+
def __init__(self, val: object):
11+
self.key = "bar"
12+
self.val = val
13+
self.call_count = 0
14+
self.await_count = 0
15+
16+
def __len__(self) -> int: # pragma: no cover
17+
return 1
18+
19+
def __iter__(self) -> Iterator[str]: # pragma: no cover
20+
return iter([self.key])
21+
22+
def __getitem__(self, k: str) -> object: # pragma: no cover
23+
self.call_count += 1
24+
if k == self.key:
25+
return self.val
26+
raise KeyError(k)
27+
28+
async def __getitem_async__(self, k: str) -> object:
29+
self.await_count += 1
30+
if k == self.key:
31+
return self.val
32+
raise KeyError(k) # pragma: no cover
33+
34+
35+
def test_async_getitem() -> None:
36+
lazy_mapping = MockLazyMapping("thing")
37+
data = {"foo": lazy_mapping}
38+
39+
async def coro() -> List[object]:
40+
return await jsonpath.findall_async("$.foo.bar", data)
41+
42+
matches = asyncio.run(coro())
43+
44+
assert len(matches) == 1
45+
assert matches[0] == "thing"
46+
assert lazy_mapping.call_count == 0
47+
assert lazy_mapping.await_count == 1

‎tests/test_compare.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Default filter expression comparison test cases."""
2+
import dataclasses
3+
import operator
4+
5+
import pytest
6+
7+
from jsonpath import JSONPathEnvironment
8+
9+
10+
@dataclasses.dataclass
11+
class Case:
12+
description: str
13+
left: object
14+
op: str
15+
right: object
16+
want: bool
17+
18+
19+
TEST_CASES = [
20+
Case(
21+
description="true and true",
22+
left=True,
23+
op="&&",
24+
right=True,
25+
want=True,
26+
),
27+
Case(
28+
description="left in right",
29+
left="thing",
30+
op="in",
31+
right=["some", "thing"],
32+
want=True,
33+
),
34+
Case(
35+
description="right contains left",
36+
left=["some", "thing"],
37+
op="contains",
38+
right="thing",
39+
want=True,
40+
),
41+
Case(
42+
description="string >= string",
43+
left="thing",
44+
op=">=",
45+
right="thing",
46+
want=True,
47+
),
48+
Case(
49+
description="string < string",
50+
left="abc",
51+
op="<",
52+
right="bcd",
53+
want=True,
54+
),
55+
Case(
56+
description="string > string",
57+
left="bcd",
58+
op=">",
59+
right="abcd",
60+
want=True,
61+
),
62+
Case(
63+
description="int >= int",
64+
left=2,
65+
op=">=",
66+
right=1,
67+
want=True,
68+
),
69+
Case(
70+
description="nil >= nil",
71+
left=None,
72+
op=">=",
73+
right=None,
74+
want=True,
75+
),
76+
Case(
77+
description="nil <= nil",
78+
left=None,
79+
op="<=",
80+
right=None,
81+
want=True,
82+
),
83+
]
84+
85+
86+
@pytest.fixture()
87+
def env() -> JSONPathEnvironment:
88+
return JSONPathEnvironment()
89+
90+
91+
@pytest.mark.parametrize("case", TEST_CASES, ids=operator.attrgetter("description"))
92+
def test_compare(env: JSONPathEnvironment, case: Case) -> None:
93+
result = env.compare(case.left, case.op, case.right)
94+
assert result == case.want

‎tests/test_errors.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from jsonpath import JSONPathEnvironment
4+
from jsonpath.exceptions import JSONPathSyntaxError
5+
6+
7+
@pytest.fixture()
8+
def env() -> JSONPathEnvironment:
9+
return JSONPathEnvironment()
10+
11+
12+
def test_unclosed_selection_list(env: JSONPathEnvironment) -> None:
13+
with pytest.raises(JSONPathSyntaxError, match=r"unexpected end of selector list"):
14+
env.compile("$[1,2")

0 commit comments

Comments
 (0)