@@ -290,3 +290,65 @@ def test_descendants_auto_id(self):
290290 } },
291291 ['foo.baz' ,
292292 'foo.bing.baz' ] )])
293+
294+ def check_update_cases (self , test_cases ):
295+ for original , expr_str , value , expected in test_cases :
296+ print ('parse(%r).update(%r, %r) =?= %r'
297+ % (expr_str , original , value , expected ))
298+ expr = parse (expr_str )
299+ actual = expr .update (original , value )
300+ assert actual == expected
301+
302+ def test_update_root (self ):
303+ self .check_update_cases ([
304+ ('foo' , '$' , 'bar' , 'bar' )
305+ ])
306+
307+ def test_update_this (self ):
308+ self .check_update_cases ([
309+ ('foo' , '`this`' , 'bar' , 'bar' )
310+ ])
311+
312+ def test_update_fields (self ):
313+ self .check_update_cases ([
314+ ({'foo' : 1 }, 'foo' , 5 , {'foo' : 5 }),
315+ ({'foo' : 1 , 'bar' : 2 }, '$.*' , 3 , {'foo' : 3 , 'bar' : 3 })
316+ ])
317+
318+ def test_update_child (self ):
319+ self .check_update_cases ([
320+ ({'foo' : 'bar' }, '$.foo' , 'baz' , {'foo' : 'baz' }),
321+ ({'foo' : {'bar' : 1 }}, 'foo.bar' , 'baz' , {'foo' : {'bar' : 'baz' }})
322+ ])
323+
324+ def test_update_where (self ):
325+ self .check_update_cases ([
326+ ({'foo' : {'bar' : {'baz' : 1 }}, 'bar' : {'baz' : 2 }},
327+ '*.bar where baz' , 5 , {'foo' : {'bar' : 5 }, 'bar' : {'baz' : 2 }})
328+ ])
329+
330+ def test_update_descendants_where (self ):
331+ self .check_update_cases ([
332+ ({'foo' : {'bar' : 1 , 'flag' : 1 }, 'baz' : {'bar' : 2 }},
333+ '(* where flag) .. bar' , 3 ,
334+ {'foo' : {'bar' : 3 , 'flag' : 1 }, 'baz' : {'bar' : 2 }})
335+ ])
336+
337+ def test_update_descendants (self ):
338+ self .check_update_cases ([
339+ ({'somefield' : 1 }, '$..somefield' , 42 , {'somefield' : 42 }),
340+ ({'outer' : {'nestedfield' : 1 }}, '$..nestedfield' , 42 , {'outer' : {'nestedfield' : 42 }}),
341+ ({'outs' : {'bar' : 1 , 'ins' : {'bar' : 9 }}, 'outs2' : {'bar' : 2 }},
342+ '$..bar' , 42 ,
343+ {'outs' : {'bar' : 42 , 'ins' : {'bar' : 42 }}, 'outs2' : {'bar' : 42 }})
344+ ])
345+
346+ def test_update_index (self ):
347+ self .check_update_cases ([
348+ (['foo' , 'bar' , 'baz' ], '[0]' , 'test' , ['test' , 'bar' , 'baz' ])
349+ ])
350+
351+ def test_update_slice (self ):
352+ self .check_update_cases ([
353+ (['foo' , 'bar' , 'baz' ], '[0:2]' , 'test' , ['test' , 'test' , 'baz' ])
354+ ])
0 commit comments