2525import testscenarios
2626
2727from jsonpath_ng .ext import parser
28-
28+ rest_response1 = {
29+ "items" :
30+ [
31+ {
32+ "status" : "UP" ,
33+ "kind" : "compute#region" ,
34+ "description" : "us-central1" ,
35+ "quotas" : [
36+ {"usage" : 3.0 , "metric" : "CPUS" , "limit" : 72.0 },
37+ {"usage" : 261.0 , "metric" : "DISKS" , "limit" : 40960.0 },
38+ {"usage" : 0.0 , "metric" : "STATIC" , "limit" : 21.0 },
39+ {"usage" : 0.0 , "metric" : "IN_USE" , "limit" : 69.0 },
40+ {"usage" : 0.0 , "metric" : "SSD" , "limit" : 20480.0 }
41+ ],
42+ "id" : "1000" ,
43+ "name" : "us-central1"
44+ },
45+ {
46+ "status" : "UP" ,
47+ "kind" : "compute#region" ,
48+ "description" : "us-central2" ,
49+ "quotas" : [
50+ {"usage" : 0.0 , "metric" : "CPUS" , "limit" : 72.0 },
51+ {"usage" : 0.0 , "metric" : "DISKS" , "limit" : 40960.0 },
52+ {"usage" : 0.0 , "metric" : "STATIC" , "limit" : 21.0 },
53+ {"usage" : 0.0 , "metric" : "IN_USE" , "limit" : 69.0 },
54+ {"usage" : 0.0 , "metric" : "SSD" , "limit" : 20480.0 }
55+ ],
56+ "id" : "1001" ,
57+ "name" : "us-central2"
58+ },
59+ {
60+ "status" : "UP" ,
61+ "kind" : "compute#region" ,
62+ "description" : "us-central3" ,
63+ "quotas" : [
64+ {"usage" : 0.0 , "metric" : "CPUS" , "limit" : 90.0 },
65+ {"usage" : 0.0 , "metric" : "DISKS" , "limit" : 2040.0 },
66+ {"usage" : 0.0 , "metric" : "STATIC" , "limit" : 46.0 },
67+ {"usage" : 0.0 , "metric" : "IN_USE" , "limit" : 80.0 },
68+ {"usage" : 500.0 , "metric" : "SSD" , "limit" : 20480.0 }
69+ ],
70+ "id" : "1002" ,
71+ "name" : "us-central3"
72+ }
73+ ]
74+ }
2975
3076class Testjsonpath_ng_ext (testscenarios .WithScenarios ,
3177 base .BaseTestCase ):
@@ -50,12 +96,31 @@ class Testjsonpath_ng_ext(testscenarios.WithScenarios,
5096 ('len_list' , dict (string = 'objects.`len`' ,
5197 data = {'objects' : ['alpha' , 'gamma' , 'beta' ]},
5298 target = 3 )),
99+ ('filter_list' , dict (string = 'objects[?@="alpha"]' ,
100+ data = {'objects' : ['alpha' , 'gamma' , 'beta' ]},
101+ target = ['alpha' ])),
102+ ('filter_list_2' , dict (string = 'objects[?@ ~= "a.+"]' ,
103+ data = {'objects' : ['alpha' , 'gamma' , 'beta' ]},
104+ target = ['alpha' ,'gamma' ])),
105+ ('keys_list' , dict (string = 'objects.`keys`' ,
106+ data = {'objects' : ['alpha' , 'gamma' , 'beta' ]},
107+ target = [])),
53108 ('len_dict' , dict (string = 'objects.`len`' ,
54109 data = {'objects' : {'cow' : 'moo' , 'cat' : 'neigh' }},
55110 target = 2 )),
111+ ('keys_dict' , dict (string = 'objects.`keys`' ,
112+ data = {'objects' : {'cow' : 'moo' , 'cat' : 'neigh' }},
113+ target = ['cow' ,'cat' ])),
114+ #('filter_keys_dict', dict(string='objects.`keys`[?`this`="cow"]',
115+ # data={'objects': {'cow': 'moo', 'cat': 'neigh'}},
116+ # target=['cow'])),
117+ #TODO make keys dictionaries filterable
56118 ('len_str' , dict (string = 'objects[0].`len`' ,
57119 data = {'objects' : ['alpha' , 'gamma' ]},
58120 target = 5 )),
121+ ('contains_filter' , dict (string = 'objects[?id ~= "v.*[1-9]"].id' ,
122+ data = {'objects' : [{'id' :'vasll1' },{'id' :'v2' },{'id' :'vaal3' },{'id' :'other' },{'id' :'val' }]},
123+ target = ['vasll1' ,'v2' ,'vaal3' ])),
59124
60125 ('filter_exists_syntax1' , dict (string = 'objects[?cow]' ,
61126 data = {'objects' : [{'cow' : 'moo' },
@@ -94,6 +159,12 @@ class Testjsonpath_ng_ext(testscenarios.WithScenarios,
94159 {'cow' : 5 },
95160 {'cow' : 'neigh' }]},
96161 target = [{'cow' : 8 }, {'cow' : 7 }])),
162+ ('filter_gt_negation' , dict (string = 'objects[?!cow<=5]' ,
163+ data = {'objects' : [{'cow' : 8 },
164+ {'cow' : 7 },
165+ {'cow' : 5 },
166+ {'cow' : 'neigh' }]},
167+ target = [{'cow' : 8 }, {'cow' : 7 },{'cow' :'neigh' }])),
97168 ('filter_and' , dict (string = 'objects[?cow>5&cat=2]' ,
98169 data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
99170 {'cow' : 7 , 'cat' : 2 },
@@ -102,6 +173,85 @@ class Testjsonpath_ng_ext(testscenarios.WithScenarios,
102173 {'cow' : 8 , 'cat' : 3 }]},
103174 target = [{'cow' : 8 , 'cat' : 2 },
104175 {'cow' : 7 , 'cat' : 2 }])),
176+ ('filter_and_demorgans' , dict (string = 'objects[?!(cow<=5|cat!=2)]' ,
177+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
178+ {'cow' : 7 , 'cat' : 2 },
179+ {'cow' : 2 , 'cat' : 2 },
180+ {'cow' : 5 , 'cat' : 3 },
181+ {'cow' : 8 , 'cat' : 3 }]},
182+ target = [{'cow' : 8 , 'cat' : 2 },
183+ {'cow' : 7 , 'cat' : 2 }])),
184+ ('filter_or' , dict (string = 'objects[?cow=8|cat=3]' ,
185+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
186+ {'cow' : 7 , 'cat' : 2 },
187+ {'cow' : 2 , 'cat' : 2 },
188+ {'cow' : 5 , 'cat' : 3 },
189+ {'cow' : 8 , 'cat' : 3 }]},
190+ target = [{'cow' : 8 , 'cat' : 2 },
191+ {'cow' : 5 , 'cat' : 3 },
192+ {'cow' : 8 , 'cat' : 3 }])),
193+ ('filter_or_demorgans' , dict (string = 'objects[?!(cow!=8&cat!=3)]' ,
194+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
195+ {'cow' : 7 , 'cat' : 2 },
196+ {'cow' : 2 , 'cat' : 2 },
197+ {'cow' : 5 , 'cat' : 3 },
198+ {'cow' : 8 , 'cat' : 3 }]},
199+ target = [{'cow' : 8 , 'cat' : 2 },
200+ {'cow' : 5 , 'cat' : 3 },
201+ {'cow' : 8 , 'cat' : 3 }])),
202+ ('filter_or_and' , dict (string = 'objects[?cow=8&cat=2|cat=3]' ,
203+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
204+ {'cow' : 7 , 'cat' : 2 },
205+ {'cow' : 2 , 'cat' : 2 },
206+ {'cow' : 5 , 'cat' : 3 },
207+ {'cow' : 8 , 'cat' : 3 }]},
208+ target = [{'cow' : 8 , 'cat' : 2 },
209+ {'cow' : 5 , 'cat' : 3 },
210+ {'cow' : 8 , 'cat' : 3 }])),
211+ ('filter_or_and_overide' , dict (string = 'objects[?cow=8&(cat=2|cat=3)]' ,
212+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 },
213+ {'cow' : 7 , 'cat' : 2 },
214+ {'cow' : 2 , 'cat' : 2 },
215+ {'cow' : 5 , 'cat' : 3 },
216+ {'cow' : 8 , 'cat' : 3 }]},
217+ target = [{'cow' : 8 , 'cat' : 2 },
218+ {'cow' : 8 , 'cat' : 3 }])),
219+ ('filter_or_and' , dict (string = 'objects[?dog=1|cat=3&cow=8]' ,
220+ data = {'objects' : [{'cow' : 8 , 'cat' : 2 , 'dog' :1 },
221+ {'cow' : 7 , 'cat' : 2 },
222+ {'cow' : 2 , 'cat' : 2 },
223+ {'cow' : 5 , 'cat' : 3 },
224+ {'cow' : 8 , 'cat' : 3 }]},
225+ target = [{'cow' : 8 , 'cat' : 2 , 'dog' :1 },
226+ {'cow' : 8 , 'cat' : 3 }])),
227+ ('filter_complex' , dict (string = '$.items[?((!(val==4))&(id==2))|(!((id!=1)&(id!=3)))]' ,
228+ data = {"items" :[{"id" :1 , "val" :1 , "info" :1 },{"id" :2 , "val" :4 },{"id" :2 ,"val" :2 },{"id" :3 ,"val" :3 }]},
229+ target = [{'info' : 1 , 'id' : 1 , 'val' : 1 },
230+ {'id' : 2 , 'val' : 2 },
231+ {'id' : 3 , 'val' : 3 }])),
232+ ('filter_complex2' , dict (string = "$.items[?(@.quotas[?((@.metric='SSD' & @.usage>0) | (@.metric='CPU' & @.usage>0) | (@.metric='DISKS' & @.usage>0))])]" ,
233+ data = rest_response1 ,
234+ target = [{'description' : 'us-central1' ,
235+ 'id' : '1000' ,
236+ 'kind' : 'compute#region' ,
237+ 'name' : 'us-central1' ,
238+ 'quotas' : [{'limit' : 72.0 , 'metric' : 'CPUS' , 'usage' : 3.0 },
239+ {'limit' : 40960.0 , 'metric' : 'DISKS' , 'usage' : 261.0 },
240+ {'limit' : 21.0 , 'metric' : 'STATIC' , 'usage' : 0.0 },
241+ {'limit' : 69.0 , 'metric' : 'IN_USE' , 'usage' : 0.0 },
242+ {'limit' : 20480.0 , 'metric' : 'SSD' , 'usage' : 0.0 }],
243+ 'status' : 'UP' },
244+ {'description' : 'us-central3' ,
245+ 'id' : '1002' ,
246+ 'kind' : 'compute#region' ,
247+ 'name' : 'us-central3' ,
248+ 'quotas' : [{'limit' : 90.0 , 'metric' : 'CPUS' , 'usage' : 0.0 },
249+ {'limit' : 2040.0 , 'metric' : 'DISKS' , 'usage' : 0.0 },
250+ {'limit' : 46.0 , 'metric' : 'STATIC' , 'usage' : 0.0 },
251+ {'limit' : 80.0 , 'metric' : 'IN_USE' , 'usage' : 0.0 },
252+ {'limit' : 20480.0 , 'metric' : 'SSD' , 'usage' : 500.0 }],
253+ 'status' : 'UP' }])),
254+
105255 ('filter_float_gt' , dict (
106256 string = 'objects[?confidence>=0.5].prediction' ,
107257 data = {
0 commit comments