|
| 1 | +from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes |
1 | 2 | import unittest |
2 | 3 |
|
3 | 4 | from jsonpath_rw import jsonpath # For setting the global auto_id_field flag |
@@ -89,21 +90,23 @@ def check_cases(self, test_cases): |
89 | 90 | # Also, we coerce iterables, etc, into the desired target type |
90 | 91 |
|
91 | 92 | for string, data, target in test_cases: |
92 | | - print 'parse("%s").find(%s) =?= %s' % (string, data, target) |
| 93 | + print('parse("%s").find(%s) =?= %s' % (string, data, target)) |
93 | 94 | result = parse(string).find(data) |
94 | 95 | if isinstance(target, list): |
95 | 96 | assert [r.value for r in result] == target |
| 97 | + elif isinstance(target, set): |
| 98 | + assert {r.value for r in result} == target |
96 | 99 | else: |
97 | 100 | assert result.value == target |
98 | 101 |
|
99 | 102 | def test_fields_value(self): |
100 | 103 | jsonpath.auto_id_field = None |
101 | 104 | self.check_cases([ ('foo', {'foo': 'baz'}, ['baz']), |
102 | 105 | ('foo,baz', {'foo': 1, 'baz': 2}, [1, 2]), |
103 | | - ('*', {'foo': 1, 'baz': 2}, [1, 2]) ]) |
| 106 | + ('*', {'foo': 1, 'baz': 2}, {1, 2}) ]) |
104 | 107 |
|
105 | 108 | jsonpath.auto_id_field = 'id' |
106 | | - self.check_cases([ ('*', {'foo': 1, 'baz': 2}, [1, 2, '@']) ]) |
| 109 | + self.check_cases([ ('*', {'foo': 1, 'baz': 2}, {1, 2, '@'}) ]) |
107 | 110 |
|
108 | 111 | def test_index_value(self): |
109 | 112 | self.check_cases([ |
@@ -146,21 +149,23 @@ def check_paths(self, test_cases): |
146 | 149 | # Also, we coerce iterables, etc, into the desired target type |
147 | 150 |
|
148 | 151 | for string, data, target in test_cases: |
149 | | - print 'parse("%s").find(%s).paths =?= %s' % (string, data, target) |
| 152 | + print('parse("%s").find(%s).paths =?= %s' % (string, data, target)) |
150 | 153 | result = parse(string).find(data) |
151 | 154 | if isinstance(target, list): |
152 | 155 | assert [str(r.full_path) for r in result] == target |
| 156 | + elif isinstance(target, set): |
| 157 | + assert {str(r.full_path) for r in result} == target |
153 | 158 | else: |
154 | 159 | assert str(result.path) == target |
155 | 160 |
|
156 | 161 | def test_fields_paths(self): |
157 | 162 | jsonpath.auto_id_field = None |
158 | 163 | self.check_paths([ ('foo', {'foo': 'baz'}, ['foo']), |
159 | 164 | ('foo,baz', {'foo': 1, 'baz': 2}, ['foo', 'baz']), |
160 | | - ('*', {'foo': 1, 'baz': 2}, ['foo', 'baz']) ]) |
| 165 | + ('*', {'foo': 1, 'baz': 2}, {'foo', 'baz'}) ]) |
161 | 166 |
|
162 | 167 | jsonpath.auto_id_field = 'id' |
163 | | - self.check_paths([ ('*', {'foo': 1, 'baz': 2}, ['foo', 'baz', 'id']) ]) |
| 168 | + self.check_paths([ ('*', {'foo': 1, 'baz': 2}, {'foo', 'baz', 'id'}) ]) |
164 | 169 |
|
165 | 170 | def test_index_paths(self): |
166 | 171 | self.check_paths([('[0]', [42], ['[0]']), |
@@ -190,7 +195,7 @@ def test_fields_auto_id(self): |
190 | 195 | ('*.id', |
191 | 196 | {'foo':{'id': 1}, |
192 | 197 | 'baz': 2}, |
193 | | - ['1', 'baz']) ]) |
| 198 | + {'1', 'baz'}) ]) |
194 | 199 |
|
195 | 200 | def test_index_auto_id(self): |
196 | 201 | jsonpath.auto_id_field = "id" |
|
0 commit comments