-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjsoniter_tests.py
executable file
·196 lines (167 loc) · 5.13 KB
/
jsoniter_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python
#iterate_array
import unittest
from bin.lib import jsoniter
import json
class iterate_array_tests(unittest.TestCase):
def test_empty(self):
self.assertEqual(
list(jsoniter.iterate_array([], 0, None)),
[]
)
def test_simple_no_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, 3], 0, None)),
[['1', '2', '3']]
)
def test_types(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 'a2', True, False], 0, None)),
[['1', 'a2', 'True', 'False']]
)
def test_simple_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, 3, 4], 2, None)),
[['1', '2'], ['3', '4']]
)
def test_simple_uneven_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, 3], 2, None)),
[['1', '2'], ['3']]
)
def test_simple_overlimit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, 3], 4, None)),
[['1', '2', '3']]
)
def test_depth_no_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, [3, 4, [5, 6], 7]], 0, 'depth')),
[['1', '2', '3', '4', '5', '6', '7']]
)
def test_depth_uneven_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, [3, 4, [5, 6], 7]], 3, 'depth')),
[['1', '2', '3'], ['4', '5', '6'], ['7']]
)
def test_width_no_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, [3, 4, [5, 6], 7]], 0, 'width')),
[['1', '2', '3', '4', '7', '5', '6']]
)
def test_width_uneven_limit(self):
self.assertEqual(
list(jsoniter.iterate_array(
[1, 2, [3, 4, [5, 6], 7]], 3, 'width')),
[['1', '2', '3'], ['4', '7', '5'], ['6']]
)
# TODO
# No exception testing (no normal exception in code!)
class SplitOnQueriesTest(unittest.TestCase):
def test_empty(self):
self.assertEqual(
list(jsoniter.split_on_queries('')),
[]
)
def test_simple(self):
self.assertEqual(
list(jsoniter.split_on_queries('a')),
[{'str': 'a'}]
)
def test_one(self):
self.assertEqual(
list(jsoniter.split_on_queries('{query}')),
[{'jpath': 'query'}]
)
def test_escaping(self):
self.assertEqual(
list(jsoniter.split_on_queries('\\\\\\{\\a\}')),
[{'str': '\\{a}'}]
)
def test_complex(self):
self.assertEqual(
list(jsoniter.split_on_queries(
'\\\\_#{$~}-!("){}a1@*{\\\\}')),
[
{'str': '\\_#'}, {'jpath': '$~'},
{'str': '-!(")'}, {'jpath': ''},
{'str': 'a1@*'}, {'jpath': '\\\\'}
]
)
class MakeQueriesTests(unittest.TestCase):
def test_empty(self):
self.assertEqual(
list(jsoniter.make_queries([], [])),
[]
)
def test_simple(self):
self.assertEqual(
list(jsoniter.make_queries([{'str': 'a'}], [])),
['a']
)
def test_empty_query(self):
self.assertEqual(
list(jsoniter.make_queries([{'jpath': ''}], '')),
['']
)
def test_simple_dict(self):
self.assertEqual(
list(jsoniter.make_queries([{'jpath': 'a'}], {'a': 'foo'})),
['foo']
)
def test_mixed(self):
self.assertEqual(
list(jsoniter.make_queries(
[ # input args
{'str': 'a'},
{'jpath': '.one'},
{'str': 'b'},
{'jpath': 'two'}
],
# json to substitute
{'one': '1', 'two': 2} # note 2 is int, not str
)),
['a', '1', 'b', '2'] # result
)
def test_complex(self):
self.assertEqual(
list(jsoniter.make_queries(
[ # input args
{'str': 'su'},
{'jpath': '[1].two'},
{'jpath': '[3].four.five[6]'}
],
#json to substitute
[
'ignore0',
{'two': 'ccee'},
'ignore2',
{
'four': {
'five': [
'ign0',
'ign1',
'ign2',
'ign3',
'ign4',
'ign5',
'ss'
]
}
}
]
)),
['su', 'ccee', 'ss']
)
def main():
unittest.main()
if __name__ == '__main__':
main()