Skip to content

Commit 56a80cf

Browse files
committed
Tests based on Stefan Goessner's examples
1 parent de4df60 commit 56a80cf

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tests/test_parser.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,64 @@ def test_nested(self):
3838
('foo where baz', Where(Fields('foo'), Fields('baz'))),
3939
('foo..baz', Descendants(Fields('foo'), Fields('baz'))),
4040
('foo..baz.bing', Descendants(Fields('foo'), Child(Fields('baz'), Fields('bing'))))])
41+
42+
def test_goessner_examples(self):
43+
"""
44+
Test Stefan Goessner's `examples`_
45+
46+
.. _examples: https://goessner.net/articles/JsonPath/index.html#e3
47+
"""
48+
self.check_parse_cases([
49+
# The authors of all books in the store
50+
("$.store.book[*].author",
51+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
52+
Slice()), Fields('author'))),
53+
54+
# All authors
55+
("$..author", Descendants(Root(), Fields('author'))),
56+
57+
# All things in the store
58+
("$.store.*", Child(Child(Root(), Fields('store')), Fields('*'))),
59+
60+
# The price of everything in the store
61+
("$.store..price",
62+
Descendants(Child(Root(), Fields('store')), Fields('price'))),
63+
64+
# The third book
65+
("$..book[2]",
66+
Child(Descendants(Root(), Fields('book')),Index(2))),
67+
68+
# The last book in order
69+
# ("$..book[(@.length-1)]", # Not implemented
70+
# Child(Descendants(Root(), Fields('book')), Slice(start=-1))),
71+
("$..book[-1:]",
72+
Child(Descendants(Root(), Fields('book')), Slice(start=-1))),
73+
74+
# The first two books
75+
# ("$..book[0,1]", # Not implemented
76+
# Child(Descendants(Root(), Fields('book')), Slice(end=2))),
77+
("$..book[:2]",
78+
Child(Descendants(Root(), Fields('book')), Slice(end=2))),
79+
80+
# Filter all books with ISBN number
81+
# ("$..book[?(@.isbn)]", None), # Not implemented
82+
83+
# Filter all books cheaper than 10
84+
# ("$..book[?(@.price<10)]", None), # Not implemented
85+
86+
# All members of JSON structure
87+
("$..*", Descendants(Root(), Fields('*'))),
88+
])
89+
90+
def test_a_few_more_examples(self):
91+
self.check_parse_cases([
92+
# Navigate objects
93+
("$.store.book[0].title",
94+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
95+
Index(0)), Fields('title'))),
96+
97+
# Navigate dictionaries
98+
("$['store']['book'][0]['title']",
99+
Child(Child(Child(Child(Root(), Fields('store')), Fields('book')),
100+
Index(0)), Fields('title'))),
101+
])

0 commit comments

Comments
 (0)