@@ -8,6 +8,9 @@ class TestJsonPath(unittest.TestCase):
8
8
def setup_class (cls ):
9
9
logging .basicConfig ()
8000
10
10
11
+ #
12
+ # Check that the data value returned is good
13
+ #
11
14
def check_cases (self , test_cases ):
12
15
# Note that just manually building an AST would avoid this dep and isolate the tests, but that would suck a bit
13
16
# Also, we coerce iterables, etc, into the desired target type
@@ -25,17 +28,6 @@ def test_fields(self):
25
28
('foo,baz' , {'foo' : 1 , 'baz' : 2 }, [1 , 2 ]),
26
29
('*' , {'foo' : 1 , 'baz' : 2 }, [1 , 2 ]) ])
27
30
28
- def test_magic_id (self ):
29
- self .check_cases ([
30
- ('id' , {'id' : 'baz' }, ['baz' ]),
31
- # ('id', {}, '@'),
32
- #('id', {}, '@'),
33
- # ('foo.id', {'foo': {}}, ['foo']),
34
- # ('foo[*].id', {'foo': {}}, 'foo[0]'),
35
- # ('foo.baz.id', {'foo': {'baz': {}}}, ['foo.baz']),
36
- # ('foo.id', [{'foo': {}}, {'foo': {}}], ['foo[0]', 'foo[1]'])
37
- ])
38
-
39
31
def test_index (self ):
40
32
self .check_cases ([('[0]' , [42 ], [42 ]),
41
33
('[2]' , [34 , 65 , 29 , 59 ], [29 ])])
@@ -53,3 +45,54 @@ def test_child(self):
53
45
54
46
def test_descendants (self ):
55
47
self .check_cases ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, [1 , 2 ] )])
48
+
49
+ #
50
+ # Check that the paths for the data are correct.
51
+ # FIXME: merge these tests with the above, since the inputs are the same anyhow
52
+ #
53
+ def check_paths (self , test_cases ):
54
+ # Note that just manually building an AST would avoid this dep and isolate the tests, but that would suck a bit
55
+ # Also, we coerce iterables, etc, into the desired target type
56
+
57
+ for string , data , target in test_cases :
58
+ print 'parse("%s").find(%s).paths =?= %s' % (string , data , target )
59
+ result = parse (string ).find (data )
60
+ if isinstance (target , list ):
61
+ assert [str (r .path ) for r in result ] == target
62
+ else :
63
+ assert str (result .path ) == target
64
+
65
+ def test_fields_paths (self ):
66
+ self .check_paths ([ ('foo' , {'foo' : 'baz' }, ['foo' ]),
67
+ ('foo,baz' , {'foo' : 1 , 'baz' : 2 }, ['foo' , 'baz' ]),
68
+ ('*' , {'foo' : 1 , 'baz' : 2 }, ['foo' , 'baz' ]) ])
69
+
70
+ def test_index_paths (self ):
71
+ self .check_paths ([('[0]' , [42 ], ['[0]' ]),
72
+ ('[2]' , [34 , 65 , 29 , 59 ], ['[2]' ])])
73
+
74
+ def test_slice_paths (self ):
75
+ self .check_paths ([ ('[*]' , [1 , 2 , 3 ], ['[0]' , '[1]' , '[2]' ]),
76
+ ('[1:]' , [1 , 2 , 3 , 4 ], ['[1]' , '[2]' , '[3]' ]) ])
77
+
78
+ def test_child_paths (self ):
79
+ self .check_paths ([('foo.baz' , {'foo' : {'baz' : 3 }}, ['foo.baz' ]),
80
+ ('foo.baz' , {'foo' : {'baz' : [3 ]}}, ['foo.baz' ]),
81
+ ('foo.baz.bizzle' , {'foo' : {'baz' : {'bizzle' : 5 }}}, ['foo.baz.bizzle' ])])
82
+
83
+ def test_descendants_paths (self ):
84
+ self .check_paths ([('foo..baz' , {'foo' : {'baz' : 1 , 'bing' : {'baz' : 2 }}}, ['foo.baz' , 'foo.bing.baz' ] )]
6D40
)
85
+
86
+ #
87
+ # And a final hack... the field "id" should always return the path of the queryset if it is not present in the object...
88
+ #
89
+ #def test_magic_id(self):
90
+ # self.check_cases([
91
+ # ('id', {'id': 'baz'}, ['baz']),
92
+ # ('id', {}, '@'),
93
+ #('id', {}, '@'),
94
+ # ('foo.id', {'foo': {}}, ['foo']),
95
+ # ('foo[*].id', {'foo': {}}, 'foo[0]'),
96
+ # ('foo.baz.id', {'foo': {'baz': {}}}, ['foo.baz']),
97
+ # ('foo.id', [{'foo': {}}, {'foo': {}}], ['foo[0]', 'foo[1]'])
98
+ # ])
0 commit comments