Skip to content

Commit 8a9e41d

Browse files
kurtmckeemichaelmior
authored andcommitted
Refactor tests/test_examples.py
1 parent 350ca41 commit 8a9e41d

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

tests/test_examples.py

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,67 @@
11
import pytest
22

3-
from jsonpath_ng.ext.filter import Filter, Expression
43
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-
])
4+
from jsonpath_ng.ext.filter import Expression, Filter
5+
from jsonpath_ng.jsonpath import Child, Descendants, Fields, Index, Root, Slice, This
6+
7+
8+
@pytest.mark.parametrize(
9+
"string, parsed",
10+
[
11+
# The authors of all books in the store
12+
(
13+
"$.store.book[*].author",
14+
Child(
15+
Child(Child(Child(Root(), Fields("store")), Fields("book")), Slice()),
16+
Fields("author"),
17+
),
18+
),
19+
#
20+
# All authors
21+
("$..author", Descendants(Root(), Fields("author"))),
22+
#
23+
# All things in the store
24+
("$.store.*", Child(Child(Root(), Fields("store")), Fields("*"))),
25+
#
26+
# The price of everything in the store
27+
(
28+
"$.store..price",
29+
Descendants(Child(Root(), Fields("store")), Fields("price")),
30+
),
31+
#
32+
# The third book
33+
("$..book[2]", Child(Descendants(Root(), Fields("book")), Index(2))),
34+
#
35+
# The last book in order
36+
# "$..book[(@.length-1)]" # Not implemented
37+
("$..book[-1:]", Child(Descendants(Root(), Fields("book")), Slice(start=-1))),
38+
#
39+
# The first two books
40+
# "$..book[0,1]" # Not implemented
41+
("$..book[:2]", Child(Descendants(Root(), Fields("book")), Slice(end=2))),
42+
#
43+
# Filter all books with an ISBN
44+
(
45+
"$..book[?(@.isbn)]",
46+
Child(
47+
Descendants(Root(), Fields("book")),
48+
Filter([Expression(Child(This(), Fields("isbn")), None, None)]),
49+
),
50+
),
51+
#
52+
# Filter all books cheaper than 10
53+
(
54+
"$..book[?(@.price<10)]",
55+
Child(
56+
Descendants(Root(), Fields("book")),
57+
Filter([Expression(Child(This(), Fields("price")), "<", 10)]),
58+
),
59+
),
60+
#
61+
# All members of JSON structure
62+
("$..*", Descendants(Root(), Fields("*"))),
63+
],
64+
)
5365
def test_goessner_examples(string, parsed):
5466
"""
5567
Test Stefan Goessner's `examples`_
@@ -59,16 +71,7 @@ def test_goessner_examples(string, parsed):
5971
assert parse(string, debug=True) == parsed
6072

6173

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'))),
74+
def test_attribute_and_dict_syntax():
75+
"""Verify that attribute and dict syntax result in identical parse trees."""
6776

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
77+
assert parse("$.store.book[0].title") == parse("$['store']['book'][0]['title']")

0 commit comments

Comments
 (0)