@@ -64,137 +64,71 @@ def test_repr(sample_data):
6464 )
6565
6666
67- def test_all_queries (sample_data ):
68- queries = [
69- "$.store.book[*].author" ,
70- "$..book[?(@.isbn)]" ,
71- "$.store.*" ,
72- "$..author" ,
73- "$.store..price" ,
74- "$..book[2]" ,
75- "$..book[-2]" ,
76- "$..book[0,1]" ,
77- "$..book[:2]" ,
78- "$..book[1:2]" ,
79- "$..book[-2:]" ,
80- "$..book[2:]" ,
81- "$.store.book[?(@.price<10)]" ,
82- "$..book[?(@.price<=$.expensive)]" ,
83- # "$..book[[email protected] ~= '(?i)REES']", 84- "$..*" ,
85- ]
86- queries_results = [
87- """[JsonPathResult(data='Nigel Rees', path="$['store']['book'][0]['author']"),
88- JsonPathResult(data='Evelyn Waugh', path="$['store']['book'][1]['author']"),
89- JsonPathResult(data='Herman Melville', path="$['store']['book'][2]['author']"),
90- JsonPathResult(data='J. R. R. Tolkien', path="$['store']['book'][3]['author']")]""" ,
91- """[JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
92- path="$['store']['book'][2]"),
93- JsonPathResult(data={'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'},
94- path="$['store']['book'][3]")]""" ,
95- """[JsonPathResult(data={'color': 'red', 'price': 19.95}, path="$['store']['bicycle']"),
96- JsonPathResult(data=[{'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
97- {'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'},
98- {'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
99- {'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'}],
100- path="$['store']['book']")]""" ,
101- """[JsonPathResult(data='Nigel Rees', path="$['store']['book'][0]['author']"),
102- JsonPathResult(data='Evelyn Waugh', path="$['store']['book'][1]['author']"),
103- JsonPathResult(data='Herman Melville', path="$['store']['book'][2]['author']"),
104- JsonPathResult(data='J. R. R. Tolkien', path="$['store']['book'][3]['author']")]""" ,
105- """[JsonPathResult(data=19.95, path="$['store']['bicycle']['price']"),
106- JsonPathResult(data=8.95, path="$['store']['book'][0]['price']"),
107- JsonPathResult(data=12.99, path="$['store']['book'][1]['price']"),
108- JsonPathResult(data=8.99, path="$['store']['book'][2]['price']"),
109- JsonPathResult(data=22.99, path="$['store']['book'][3]['price']")]""" ,
110- """[JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
111- path="$['store']['book'][2]")]""" ,
112- """[JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
113- path="$['store']['book'][2]")]""" ,
114- """[JsonPathResult(data={'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
115- path="$['store']['book'][0]"),
116- JsonPathResult(data={'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'}, path="$['store']['book'][1]")]""" ,
117- """[JsonPathResult(data={'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
118- path="$['store']['book'][0]"),
119- JsonPathResult(data={'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'}, path="$['store']['book'][1]")]""" ,
120- """[JsonPathResult(data={'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'},
121- path="$['store']['book'][1]")]""" ,
122- """[JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
123- path="$['store']['book'][2]"),
124- JsonPathResult(data={'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'},
125- path="$['store']['book'][3]")]""" ,
126- """[JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
127- path="$['store']['book'][2]"),
128- JsonPathResult(data={'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'},
129- path="$['store']['book'][3]")]""" ,
130- """[JsonPathResult(data={'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
131- path="$['store']['book'][0]"),
132- JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
133- path="$['store']['book'][2]")]""" ,
134- """[JsonPathResult(data={'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
135- path="$['store']['book'][0]"),
136- JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
137- path="$['store']['book'][2]")]""" ,
138- """[JsonPathResult(data=10, path="$['expensive']"),
139- JsonPathResult(data={'bicycle': {'color': 'red', 'price': 19.95},
140- 'book': [{'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
141- {'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'},
142- {'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
143- {'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'}]},
144- path="$['store']"),
145- JsonPathResult(data={'color': 'red', 'price': 19.95}, path="$['store']['bicycle']"),
146- JsonPathResult(data=[{'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
147- {'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'},
148- {'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
149- {'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'}],
150- path="$['store']['book']"),
151- JsonPathResult(data='red', path="$['store']['bicycle']['color']"),
152- JsonPathResult(data=19.95, path="$['store']['bicycle']['price']"),
153- JsonPathResult(data={'author': 'Nigel Rees', 'category': 'reference', 'price': 8.95, 'title': 'Sayings of the Century'},
154- path="$['store']['book'][0]"),
155- JsonPathResult(data={'author': 'Evelyn Waugh', 'category': 'fiction', 'price': 12.99, 'title': 'Sword of Honour'}, path="$['store']['book'][1]"),
156- JsonPathResult(data={'author': 'Herman Melville', 'category': 'fiction', 'isbn': '0-553-21311-3', 'price': 8.99, 'title': 'Moby Dick'},
157- path="$['store']['book'][2]"),
158- JsonPathResult(data={'author': 'J. R. R. Tolkien', 'category': 'fiction', 'isbn': '0-395-19395-8', 'price': 22.99, 'title': 'The Lord of the Rings'},
159- path="$['store']['book'][3]"),
160- JsonPathResult(data='Nigel Rees', path="$['store']['book'][0]['author']"),
161- JsonPathResult(data='reference', path="$['store']['book'][0]['category']"),
162- JsonPathResult(data=8.95, path="$['store']['book'][0]['price']"),
163- JsonPathResult(data='Sayings of the Century', path="$['store']['book'][0]['title']"),
164- JsonPathResult(data='Evelyn Waugh', path="$['store']['book'][1]['author']"),
165- JsonPathResult(data='fiction', path="$['store']['book'][1]['category']"),
166- JsonPathResult(data=12.99, path="$['store']['book'][1]['price']"),
167- JsonPathResult(data='Sword of Honour', path="$['store']['book'][1]['title']"),
168- JsonPathResult(data='Herman Melville', path="$['store']['book'][2]['author']"),
169- JsonPathResult(data='fiction', path="$['store']['book'][2]['category']"),
170- JsonPathResult(data='0-553-21311-3', path="$['store']['book'][2]['isbn']"),
171- JsonPathResult(data=8.99, path="$['store']['book'][2]['price']"),
172- JsonPathResult(data='Moby Dick', path="$['store']['book'][2]['title']"),
173- JsonPathResult(data='J. R. R. Tolkien', path="$['store']['book'][3]['author']"),
174- JsonPathResult(data='fiction', path="$['store']['book'][3]['category']"),
175- JsonPathResult(data='0-395-19395-8', path="$['store']['book'][3]['isbn']"),
176- JsonPathResult(data=22.99, path="$['store']['book'][3]['price']"),
177- JsonPathResult(data='The Lord of the Rings', path="$['store']['book'][3]['title']")]""" ,
178- ]
179- f = Finder (sample_data )
180- res = []
181- res_data = []
182- res_absolute_path = []
183- for query , queries_result in zip (queries , queries_results ):
184- temp_res = f .find (query )
185- res .extend (temp_res )
186- res_data .extend (f .find_data (query ))
187- res_absolute_path .extend (f .find_absolute_path (query ))
188- assert str (temp_res ) == queries_result .replace ("\n " , "" )
189- assert [r .data for r in res ] == res_data
190- assert [r .path for r in res ] == res_absolute_path
191- # print(query)
192- # print(f.find(query), '\n')
193- # print('----------')
194-
195-
19667def test_overflow ():
19768 big_number = 18446744005107584948
19869 f = Finder ({"test" : big_number })
19970 res = f .find ('$.test' )[0 ].data
20071 assert res == big_number
72+
73+
74+ QUERIES = [
75+ "$.store.book[*].author" ,
76+ "$..book[?(@.isbn)]" ,
77+ "$.store.*" ,
78+ "$..author" ,
79+ "$.store..price" ,
80+ "$..book[2]" ,
81+ "$..book[-2]" ,
82+ "$..book[0,1]" ,
83+ "$..book[:2]" ,
84+ "$..book[1:2]" ,
85+ "$..book[-2:]" ,
86+ "$..book[2:]" ,
87+ "$.store.book[?(@.price<10)]" ,
88+ "$..book[?(@.price<=$.expensive)]" ,
89+ "$..*" ,
90+ r'$..book[?match(@.author, "(?i).*rees.*")]' ,
91+ ]
92+
93+ @pytest .mark .parametrize ("query" , QUERIES , ids = QUERIES )
94+ def test_smoke (sample_data , query ):
95+ f = Finder (sample_data )
96+
97+ results = f .find (query )
98+ assert isinstance (results , list )
99+ assert all (isinstance (r , JsonPathResult ) for r in results )
100+
101+ assert [r .data for r in results ] == f .find_data (query )
102+ assert [r .path for r in results ] == f .find_absolute_path (query )
103+
104+
105+ def test_authors_data_and_paths (sample_data ):
106+ f = Finder (sample_data )
107+ results = f .find ("$.store.book[*].author" )
108+ data , paths = _extract (results )
109+
110+ assert data == [
111+ "Nigel Rees" ,
112+ "Evelyn Waugh" ,
113+ "Herman Melville" ,
114+ "J. R. R. Tolkien" ,
115+ ]
116+ assert paths == [
117+ "$['store']['book'][0]['author']" ,
118+ "$['store']['book'][1]['author']" ,
119+ "$['store']['book'][2]['author']" ,
120+ "$['store']['book'][3]['author']" ,
121+ ]
122+
123+ def test_bicycle_color_value_and_path (sample_data ):
124+ f = Finder (sample_data )
125+ results = f .find ("$.store.bicycle.color" )
126+ data , paths = _extract (results )
127+
128+ assert data == ["red" ]
129+ assert paths == ["$['store']['bicycle']['color']" ]
130+
131+
132+ def _extract (results ):
133+ assert all (isinstance (r , JsonPathResult ) for r in results )
134+ return [r .data for r in results ], [r .path for r in results ]
0 commit comments