11import unittest
22
3+ from jsonpath_rw import jsonpath # For setting the global auto_id_field flag
4+
35from jsonpath_rw .parser import parse
46from jsonpath_rw .jsonpath import *
57
@@ -23,27 +25,27 @@ def check_cases(self, test_cases):
2325 else :
2426 assert result .value == target
2527
26- def test_fields (self ):
28+ def test_fields_value (self ):
2729 self .check_cases ([ ('foo' , {'foo' : 'baz' }, ['baz' ]),
2830 ('foo,baz' , {'foo' : 1 , 'baz' : 2 }, [1 , 2 ]),
2931 ('*' , {'foo' : 1 , 'baz' : 2 }, [1 , 2 ]) ])
3032
31- def test_index (self ):
33+ def test_index_value (self ):
3234 self .check_cases ([('[0]' , [42 ], [42 ]),
3335 ('[2]' , [34 , 65 , 29 , 59 ], [29 ])])
3436
35- def test_slice (self ):
37+ def test_slice_value (self ):
3638 self .check_cases ([('[*]' , [1 , 2 , 3 ], [1 , 2 , 3 ]),
3739 ('[*]' , xrange (1 , 4 ), [1 , 2 , 3 ]),
3840 ('[1:]' , [1 , 2 , 3 , 4 ], [2 , 3 , 4 ]),
3941 ('[:2]' , [1 , 2 , 3 , 4 ], [1 , 2 ])])
4042
41- def test_child (self ):
43+ def test_child_value (self ):
4244 self .check_cases ([('foo.baz' , {'foo' : {'baz' : 3 }}, [3 ]),
4345 ('foo.baz' , {'foo' : {'baz' : [3 ]}}, [[3 ]]),
4446 ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, [5 ])])
4547
46- def test_descendants (self ):
48+ def test_descendants_value (self ):
4749 self .check_cases ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, [1 , 2 ] )])
4850
4951 #
@@ -83,3 +85,32 @@ def test_child_paths(self):
8385 def test_descendants_paths (self ):
8486 self .check_paths ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, ['foo.baz' , 'foo.bing.baz' ] )])
8587
88+
89+ #
90+ # Check the "auto_id_field" feature
91+ #
92+ def test_fields_auto_id (self ):
93+ jsonpath .auto_id_field = "id"
94+ self .check_cases ([ ('foo.id' , {'foo' : 'baz' }, ['foo' ]),
95+ ('foo,baz.id' , {'foo' : 1 , 'baz' : 2 }, ['foo' , 'baz' ]),
96+ ('*.id' , {'foo' :{'id' : 1 }, 'baz' : 2 }, [1 , 'baz' ]) ])
97+
98+ def test_index_auto_id (self ):
99+ jsonpath .auto_id_field = "id"
100+ self .check_cases ([('[0].id' , [42 ], ['[0]' ]),
101+ ('[2].id' , [34 , 65 , 29 , 59 ], ['[2]' ])])
102+
103+ def test_slice_auto_id (self ):
104+ jsonpath .auto_id_field = "id"
105+ self .check_cases ([ ('[*].id' , [1 , 2 , 3 ], ['[0]' , '[1]' , '[2]' ]),
106+ ('[1:].id' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ]) ])
107+
108+ def test_child_auto_id (self ):
109+ jsonpath .auto_id_field = "id"
110+ self .check_cases ([('foo.baz.id' , {'foo' : {'baz' : 3 }}, ['foo.baz' ]),
111+ ('foo.baz.id' , {'foo' : {'baz' : [3 ]}}, ['foo.baz' ]),
112+ ('foo.baz.bizzle.id' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])])
113+
114+ def test_descendants_auto_id (self ):
115+ jsonpath .auto_id_field = "id"
116+ self .check_cases ([('foo..baz.id' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, ['foo.baz' , 'foo.bing.baz' ] )])
0 commit comments