@@ -86,10 +86,13 @@ def setup_class(cls):
86
86
#
87
87
# Check that the data value returned is good
88
88
#
89
- def check_cases (self , test_cases ):
89
+ def check_cases (self , test_cases , auto_id_field = None ):
90
90
# Note that just manually building an AST would avoid this dep and isolate the tests, but that would suck a bit
91
91
# Also, we coerce iterables, etc, into the desired target type
92
92
93
+ # This is a global parameter, it should be updated with each call of this function automatically
94
+ # Not manually outside this function, or someone might forget to switch it back to None
95
+ jsonpath .auto_id_field = auto_id_field
93
96
for string , data , target in test_cases :
94
97
print ('parse("%s").find(%s) =?= %s' % (string , data , target ))
95
98
result = parse (string ).find (data )
@@ -101,25 +104,23 @@ def check_cases(self, test_cases):
101
104
assert result .value == target
102
105
103
106
def test_fields_value (self ):
104
- jsonpath .auto_id_field = None
105
107
self .check_cases ([ ('foo' , {'foo' : 'baz' }, ['baz' ]),
106
108
('foo,baz' , {'foo' : 1 , 'baz' : 2 }, [1 , 2 ]),
107
109
('@foo' , {'@foo' : 1 }, [1 ]),
108
- ('*' , {'foo' : 1 , 'baz' : 2 }, set ([1 , 2 ])) ])
110
+ ('*' , {'foo' : 1 , 'baz' : 2 }, set ([1 , 2 ]))
111
+ ])
109
112
110
- jsonpath . auto_id_field = 'id'
111
- self . check_cases ([ ( '*' , { 'foo' : 1 , 'baz' : 2 }, set ([ 1 , 2 , '`this`' ])) ] )
113
+ self . check_cases ([ ( '*' , { 'foo' : 1 , 'baz' : 2 }, set ([ 1 , 2 , '`this`' ]))
114
+ ], 'id' )
112
115
113
116
def test_root_value (self ):
114
- jsonpath .auto_id_field = None
115
117
self .check_cases ([
116
118
('$' , {'foo' : 'baz' }, [{'foo' :'baz' }]),
117
119
('foo.$' , {'foo' : 'baz' }, [{'foo' :'baz' }]),
118
120
('foo.$.foo' , {'foo' : 'baz' }, ['baz' ]),
119
121
])
120
122
121
123
def test_this_value (self ):
122
- jsonpath .auto_id_field = None
123
124
self .check_cases ([
124
125
('`this`' , {'foo' : 'baz' }, [{'foo' :'baz' }]),
125
126
('foo.`this`' , {'foo' : 'baz' }, ['baz' ]),
@@ -131,14 +132,16 @@ def test_index_value(self):
131
132
('[0]' , [42 ], [42 ]),
132
133
('[5]' , [42 ], []),
133
134
('[2]' , [34 , 65 , 29 , 59 ], [29 ]),
135
+ ('[0,2,5]' , [34 , 65 , 29 , 59 , 17 , 3 ], [34 , 29 , 3 ]),
134
136
('[0]' , None , [])
135
137
])
136
138
137
139
def test_slice_value (self ):
138
140
self .check_cases ([('[*]' , [1 , 2 , 3 ], [1 , 2 , 3 ]),
139
141
('[*]' , xrange (1 , 4 ), [1 , 2 , 3 ]),
140
142
('[1:]' , [1 , 2 , 3 , 4 ], [2 , 3 , 4 ]),
141
- ('[:2]' , [1 , 2 , 3 , 4 ], [1 , 2 ])])
143
+ ('[:2]' , [1 , 2 , 3 , 4 ], [1 , 2 ])
144
+ ])
142
145
143
146
# Funky slice hacks
144
147
self .check_cases ([
@@ -151,7 +154,8 @@ def test_slice_value(self):
151
154
def test_child_value (self ):
152
155
self .check_cases ([('foo.baz' , {'foo' : {'baz' : 3 }}, [3 ]),
153
156
('foo.baz' , {'foo' : {'baz' : [3 ]}}, [[3 ]]),
154
- ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, [5 ])])
157
+ ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, [5 ])
158
+ ])
155
159
156
160
def test_descendants_value (self ):
157
161
self .check_cases ([
@@ -161,28 +165,28 @@ def test_descendants_value(self):
161
165
162
166
def test_parent_value (self ):
163
167
self .check_cases ([('foo.baz.`parent`' , {'foo' : {'baz' : 3 }}, [{'baz' : 3 }]),
164
- ('foo.`parent`.foo.baz.`parent`.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, [5 ])])
168
+ ('foo.`parent`.foo.baz.`parent`.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, [5 ])
169
+ ])
165
170
166
171
def test_hyphen_key (self ):
167
172
self .check_cases ([('foo.bar-baz' , {'foo' : {'bar-baz' : 3 }}, [3 ]),
168
- ('foo.[bar-baz,blah-blah]' , {'foo' : {'bar-baz' : 3 , 'blah-blah' :5 }},
169
- [ 3 , 5 ]) ])
173
+ ('foo.[bar-baz,blah-blah]' , {'foo' : {'bar-baz' : 3 , 'blah-blah' :5 }}, [ 3 , 5 ])
174
+ ])
170
175
self .assertRaises (JsonPathLexerError , self .check_cases ,
171
176
[('foo.-baz' , {'foo' : {'-baz' : 8 }}, [8 ])])
172
177
173
178
174
-
175
-
176
179
#
177
180
# Check that the paths for the data are correct.
178
181
# FIXME: merge these tests with the above, since the inputs are the same anyhow
179
182
#
180
- def check_paths (self , test_cases ):
183
+ def check_paths (self , test_cases , auto_id_field = None ):
181
184
# Note that just manually building an AST would avoid this dep and isolate the tests, but that would suck a bit
182
185
# Also, we coerce iterables, etc, into the desired target type
183
186
187
+ jsonpath .auto_id_field = auto_id_field
184
188
for string , data , target in test_cases :
185
- print ('parse("%s").find(%s).paths =?= %s' % (string , data , target ))
189
+ print ('parse("%s").find(%s).path =?= %s' % (string , data , target ))
186
190
result = parse (string ).find (data )
187
191
if isinstance (target , list ):
188
192
assert [str (r .full_path ) for r in result ] == target
@@ -192,24 +196,22 @@ def check_paths(self, test_cases):
192
196
assert str (result .path ) == target
193
197
194
198
def test_fields_paths (self ):
195
- jsonpath .auto_id_field = None
196
199
self .check_paths ([ ('foo' , {'foo' : 'baz' }, ['foo' ]),
197
200
('foo,baz' , {'foo' : 1 , 'baz' : 2 }, ['foo' , 'baz' ]),
198
- ('*' , {'foo' : 1 , 'baz' : 2 }, set (['foo' , 'baz' ])) ])
201
+ ('*' , {'foo' : 1 , 'baz' : 2 }, set (['foo' , 'baz' ]))
202
+ ], None )
199
203
200
- jsonpath . auto_id_field = ' id'
201
- self . check_paths ([ ( '*' , { 'foo' : 1 , 'baz' : 2 }, set ([ 'foo' , 'baz' , ' id'])) ] )
204
+ self . check_paths ([ ( '*' , { 'foo' : 1 , 'baz' : 2 }, set ([ 'foo' , 'baz' , ' id']))
205
+ ], ' id' )
202
206
203
207
def test_root_paths (self ):
204
- jsonpath .auto_id_field = None
205
208
self .check_paths ([
206
209
('$' , {'foo' : 'baz' }, ['$' ]),
207
210
('foo.$' , {'foo' : 'baz' }, ['$' ]),
208
211
('foo.$.foo' , {'foo' : 'baz' }, ['foo' ]),
209
212
])
210
213
211
214
def test_this_paths (self ):
212
- jsonpath .auto_id_field = None
213
215
self .check_paths ([
214
216
('`this`' , {'foo' : 'baz' }, ['`this`' ]),
215
217
('foo.`this`' , {'foo' : 'baz' }, ['foo' ]),
@@ -218,70 +220,72 @@ def test_this_paths(self):
218
220
219
221
def test_index_paths (self ):
220
222
self .check_paths ([('[0]' , [42 ], ['[0]' ]),
221
- ('[2]' , [34 , 65 , 29 , 59 ], ['[2]' ])])
223
+ ('[2]' , [34 , 65 , 29 , 59 ], ['[2]' ]),
224
+ ('[1,2,3]' , [34 , 65 , 29 , 59 ], ['[1]' , '[2]' , '[3]' ]),
225
+ ])
222
226
223
227
def test_slice_paths (self ):
224
228
self .check_paths ([ ('[*]' , [1 , 2 , 3 ], ['[0]' , '[1]' , '[2]' ]),
225
- ('[1:]' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ]) ])
229
+ ('[1:]' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ])
230
+ ])
226
231
227
232
def test_child_paths (self ):
228
233
self .check_paths ([('foo.baz' , {'foo' : {'baz' : 3 }}, ['foo.baz' ]),
229
234
('foo.baz' , {'foo' : {'baz' : [3 ]}}, ['foo.baz' ]),
230
- ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])])
235
+ ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])
236
+ ])
231
237
232
238
def test_descendants_paths (self ):
233
- self .check_paths ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, ['foo.baz' , 'foo.bing.baz' ] )])
239
+ self .check_paths ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, ['foo.baz' , 'foo.bing.baz' ] )
240
+ ])
234
241
235
242
236
243
#
237
244
# Check the "auto_id_field" feature
238
245
#
239
246
def test_fields_auto_id (self ):
240
- jsonpath .auto_id_field = "id"
241
247
self .check_cases ([ ('foo.id' , {'foo' : 'baz' }, ['foo' ]),
242
248
('foo.id' , {'foo' : {'id' : 'baz' }}, ['baz' ]),
243
249
('foo,baz.id' , {'foo' : 1 , 'baz' : 2 }, ['foo' , 'baz' ]),
244
250
('*.id' ,
245
251
{'foo' :{'id' : 1 },
246
252
'baz' : 2 },
247
- set (['1' , 'baz' ])) ])
253
+ set (['1' , 'baz' ]))
254
+ ], 'id' )
248
255
249
256
def test_root_auto_id (self ):
250
- jsonpath .auto_id_field = 'id'
251
257
self .check_cases ([
252
258
('$.id' , {'foo' : 'baz' }, ['$' ]), # This is a wonky case that is not that interesting
253
259
('foo.$.id' , {'foo' : 'baz' , 'id' : 'bizzle' }, ['bizzle' ]),
254
260
('foo.$.baz.id' , {'foo' : 4 , 'baz' : 3 }, ['baz' ]),
255
- ])
261
+ ], 'id' )
256
262
257
263
def test_this_auto_id (self ):
258
- jsonpath .auto_id_field = 'id'
259
264
self .check_cases ([
260
265
('id' , {'foo' : 'baz' }, ['`this`' ]), # This is, again, a wonky case that is not that interesting
261
266
('foo.`this`.id' , {'foo' : 'baz' }, ['foo' ]),
262
267
('foo.`this`.baz.id' , {'foo' : {'baz' : 3 }}, ['foo.baz' ]),
263
- ])
268
+ ], 'id' )
264
269
265
270
def test_index_auto_id (self ):
266
- jsonpath .auto_id_field = "id"
267
271
self .check_cases ([('[0].id' , [42 ], ['[0]' ]),
268
- ('[2].id' , [34 , 65 , 29 , 59 ], ['[2]' ])])
272
+ ('[2].id' , [34 , 65 , 29 , 59 ], ['[2]' ])
273
+ ], 'id' )
269
274
270
275
def test_slice_auto_id (self ):
271
- jsonpath .auto_id_field = "id"
272
276
self .check_cases ([ ('[*].id' , [1 , 2 , 3 ], ['[0]' , '[1]' , '[2]' ]),
273
- ('[1:].id' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ]) ])
277
+ ('[1:].id' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ])
278
+ ], 'id' )
274
279
275
280
def test_child_auto_id (self ):
276
- jsonpath .auto_id_field = "id"
277
281
self .check_cases ([('foo.baz.id' , {'foo' : {'baz' : 3 }}, ['foo.baz' ]),
278
282
('foo.baz.id' , {'foo' : {'baz' : [3 ]}}, ['foo.baz' ]),
279
283
('foo.baz.id' , {'foo' : {'id' : 'bizzle' , 'baz' : 3 }}, ['bizzle.baz' ]),
280
284
('foo.baz.id' , {'foo' : {'baz' : {'id' : 'hi' }}}, ['foo.hi' ]),
281
- ('foo.baz.bizzle.id' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])])
285
+ ('foo.baz.bizzle.id' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])
286
+ ], 'id' )
282
287
283
288
def test_descendants_auto_id (self ):
284
- jsonpath .auto_id_field = "id"
285
289
self .check_cases ([('foo..baz.id' ,
286
290
{'foo' : {
287
291
'baz' : 1 ,
@@ -290,9 +294,11 @@ def test_descendants_auto_id(self):
290
294
}
291
295
} },
292
296
['foo.baz' ,
293
- 'foo.bing.baz' ] )])
297
+ 'foo.bing.baz' ] )
298
+ ], 'id' )
294
299
295
- def check_update_cases (self , test_cases ):
300
+ def check_update_cases (self , test_cases , auto_id_field = None ):
301
+ jsonpath .auto_id_field = auto_id_field
296
302
for original , expr_str , value , expected in test_cases :
297
303
print ('parse(%r).update(%r, %r) =?= %r'
298
304
% (expr_str , original , value , expected ))
0 commit comments