Skip to content

Commit 652f665

Browse files
authored
Merge pull request h2non#68 from kaapstorm/example_tests
Example tests
2 parents 9d1b056 + aa8833a commit 652f665

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

jsonpath_ng/ext/filter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def __repr__(self):
7171
def __str__(self):
7272
return '[?%s]' % self.expressions
7373

74+
def __eq__(self, other):
75+
return (isinstance(other, Filter)
76+
and self.expressions == other.expressions)
77+
7478

7579
class Expression(JSONPath):
7680
"""The JSONQuery expression"""
@@ -108,7 +112,7 @@ def find(self, datum):
108112
return found
109113

110114
def __eq__(self, other):
111-
return (isinstance(other, Filter) and
115+
return (isinstance(other, Expression) and
112116
self.target == other.target and
113117
self.op == other.op and
114118
self.value == other.value)

jsonpath_ng/jsonpath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ def __str__(self):
439439
def __eq__(self, other):
440440
return isinstance(other, Descendants) and self.left == other.left and self.right == other.right
441441

442+
def __repr__(self):
443+
return '%s(%r, %r)' % (self.__class__.__name__, self.left, self.right)
444+
445+
442446
class Union(JSONPath):
443447
"""
444448
JSONPath that returns the union of the results of each match.

tests/test_examples.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import pytest
2+
3+
from jsonpath_ng.ext.filter import Filter, Expression
4+
from jsonpath_ng.ext import parse
5+
from jsonpath_ng.jsonpath import *
6+
7+
8+
@pytest.mark.parametrize('string, parsed', [
9+
# The authors of all books in the store
10+
("$.store.book[*].author",
11+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
12+
Slice()), Fields('author'))),
13+
14+
# All authors
15+
("$..author", Descendants(Root(), Fields('author'))),
16+
17+
# All things in the store
18+
("$.store.*", Child(Child(Root(), Fields('store')), Fields('*'))),
19+
20+
# The price of everything in the store
21+
("$.store..price",
22+
Descendants(Child(Root(), Fields('store')), Fields('price'))),
23+
24+
# The third book
25+
("$..book[2]",
26+
Child(Descendants(Root(), Fields('book')),Index(2))),
27+
28+
# The last book in order
29+
# ("$..book[(@.length-1)]", # Not implemented
30+
# Child(Descendants(Root(), Fields('book')), Slice(start=-1))),
31+
("$..book[-1:]",
32+
Child(Descendants(Root(), Fields('book')), Slice(start=-1))),
33+
34+
# The first two books
35+
# ("$..book[0,1]", # Not implemented
36+
# Child(Descendants(Root(), Fields('book')), Slice(end=2))),
37+
("$..book[:2]",
38+
Child(Descendants(Root(), Fields('book')), Slice(end=2))),
39+
40+
# Filter all books with ISBN number
41+
("$..book[?(@.isbn)]",
42+
Child(Descendants(Root(), Fields('book')),
43+
Filter([Expression(Child(This(), Fields('isbn')), None, None)]))),
44+
45+
# Filter all books cheaper than 10
46+
("$..book[?(@.price<10)]",
47+
Child(Descendants(Root(), Fields('book')),
48+
Filter([Expression(Child(This(), Fields('price')), '<', 10)]))),
49+
50+
# All members of JSON structure
51+
("$..*", Descendants(Root(), Fields('*'))),
52+
])
53+
def test_goessner_examples(string, parsed):
54+
"""
55+
Test Stefan Goessner's `examples`_
56+
57+
.. _examples: https://goessner.net/articles/JsonPath/index.html#e3
58+
"""
59+
assert parse(string, debug=True) == parsed
60+
61+
62+
@pytest.mark.parametrize('string, parsed', [
63+
# Navigate objects
64+
("$.store.book[0].title",
65+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
66+
Index(0)), Fields('title'))),
67+
68+
# Navigate dictionaries
69+
("$['store']['book'][0]['title']",
70+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
71+
Index(0)), Fields('title'))),
72+
])
73+
def test_obj_v_dict(string, parsed):
74+
assert parse(string, debug=True) == parsed

0 commit comments

Comments
 (0)