Skip to content

Commit 796f51c

Browse files
committed
Fluent API test cases WIP
1 parent b3a755d commit 796f51c

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

jsonpath/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
__all__ = (
3232
"compile",
3333
"CompoundJSONPath",
34+
"find",
3435
"findall_async",
3536
"findall",
3637
"finditer_async",
3738
"finditer",
39+
"first",
3840
"JSONPatch",
3941
"JSONPath",
4042
"JSONPathEnvironment",
@@ -53,13 +55,14 @@
5355
"Lexer",
5456
"match",
5557
"Parser",
58+
"query",
59+
"Query",
5660
"RelativeJSONPointer",
5761
"RelativeJSONPointerError",
5862
"RelativeJSONPointerIndexError",
5963
"RelativeJSONPointerSyntaxError",
6064
"resolve",
6165
"UNDEFINED",
62-
"Query",
6366
)
6467

6568

tests/test_fluent_api.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""Test cases for the fluent API."""
2+
import pytest
3+
4+
from jsonpath import query
5+
6+
7+
def test_iter_query() -> None:
8+
"""Test that `query` result is iterable, just like `finditer`."""
9+
it = query("$.some.*", {"some": [0, 1, 2, 3]})
10+
for i, match in enumerate(it):
11+
assert match.value == i
12+
13+
assert [m.obj for m in query("$.some.*", {"some": [0, 1, 2, 3]})] == [0, 1, 2, 3]
14+
15+
16+
def test_query_values() -> None:
17+
"""Test that we can get an iterable of values from a query."""
18+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).values()
19+
assert list(it) == [0, 1, 2, 3]
20+
21+
22+
def test_query_locations() -> None:
23+
"""Test that we can get an iterable of paths from a query."""
24+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).locations()
25+
assert list(it) == [
26+
"$['some'][0]",
27+
"$['some'][1]",
28+
"$['some'][2]",
29+
"$['some'][3]",
30+
]
31+
32+
33+
def test_query_items() -> None:
34+
"""Test that we can get an iterable of values and paths from a query."""
35+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).items()
36+
assert list(it) == [
37+
("$['some'][0]", 0),
38+
("$['some'][1]", 1),
39+
("$['some'][2]", 2),
40+
("$['some'][3]", 3),
41+
]
42+
43+
44+
def test_query_skip() -> None:
45+
"""Test that we can skip matches from the start of a query iterable."""
46+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(2)
47+
matches = list(it)
48+
assert len(matches) == 2 # noqa: PLR2004
49+
assert [m.obj for m in matches] == [2, 3]
50+
51+
52+
def test_query_skip_zero() -> None:
53+
"""Test that we can skip zero matches from the start of a query iterable."""
54+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(0)
55+
matches = list(it)
56+
assert len(matches) == 4 # noqa: PLR2004
57+
assert [m.obj for m in matches] == [0, 1, 2, 3]
58+
59+
60+
def test_query_skip_negative() -> None:
61+
"""Test that we get an exception when skipping a negative value."""
62+
with pytest.raises(ValueError, match="can't drop a negative number of matches"):
63+
query("$.some.*", {"some": [0, 1, 2, 3]}).skip(-1)
64+
65+
66+
def test_query_skip_all() -> None:
67+
"""Test that we can skip all matches from the start of a query iterable."""
68+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(4)
69+
matches = list(it)
70+
assert len(matches) == 0 # noqa: PLR2004
71+
assert [m.obj for m in matches] == []
72+
73+
74+
def test_query_skip_more() -> None:
75+
"""Test that we can skip more results than there are matches."""
76+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(5)
77+
matches = list(it)
78+
assert len(matches) == 0 # noqa: PLR2004
79+
assert [m.obj for m in matches] == []
80+
81+
82+
def test_query_drop() -> None:
83+
"""Test that we can skip matches with `drop`."""
84+
it = query("$.some.*", {"some": [0, 1, 2, 3]}).drop(2)
85+
matches = list(it)
86+
assert len(matches) == 2 # noqa: PLR2004
87+
assert [m.obj for m in matches] == [2, 3]

0 commit comments

Comments
 (0)