-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_jiter.py
293 lines (213 loc) · 9.02 KB
/
test_jiter.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import json
from decimal import Decimal
import jiter
import pytest
from math import inf
from dirty_equals import IsFloatNan
def test_parse_numeric():
parsed = jiter.from_json(
b' { "int": 1, "bigint": 123456789012345678901234567890, "float": 1.2} '
)
assert parsed == {"int": 1, "bigint": 123456789012345678901234567890, "float": 1.2}
def test_parse_other_cached():
parsed = jiter.from_json(
b'["string", true, false, null, NaN, Infinity, -Infinity]',
allow_inf_nan=True,
cache_mode=True,
)
assert parsed == ["string", True, False, None, IsFloatNan(), inf, -inf]
def test_parse_other_no_cache():
parsed = jiter.from_json(
b'["string", true, false, null]',
cache_mode=False,
)
assert parsed == ["string", True, False, None]
def test_disallow_nan():
with pytest.raises(jiter.JsonParseError, match="expected value at line 1 column 2"):
jiter.from_json(b"[NaN]", allow_inf_nan=False)
def test_error():
with pytest.raises(jiter.JsonParseError, match="EOF while parsing a list at line 1 column 9") as exc_info:
jiter.from_json(b'["string"')
assert exc_info.value.kind() == 'EofWhileParsingList'
assert exc_info.value.description() == 'EOF while parsing a list'
assert exc_info.value.path() == []
assert exc_info.value.index() == 9
assert exc_info.value.line() == 1
assert exc_info.value.column() == 9
assert repr(exc_info.value) == 'JsonParseError("EOF while parsing a list at line 1 column 9")'
def test_error_path():
with pytest.raises(jiter.JsonParseError, match="EOF while parsing a string at line 1 column 5") as exc_info:
jiter.from_json(b'["str', error_in_path=True)
assert exc_info.value.kind() == 'EofWhileParsingString'
assert exc_info.value.description() == 'EOF while parsing a string'
assert exc_info.value.path() == [0]
assert exc_info.value.index() == 5
assert exc_info.value.line() == 1
def test_error_path_empty():
with pytest.raises(jiter.JsonParseError) as exc_info:
jiter.from_json(b'"foo', error_in_path=True)
assert exc_info.value.kind() == 'EofWhileParsingString'
assert exc_info.value.path() == []
def test_error_path_object():
with pytest.raises(jiter.JsonParseError) as exc_info:
jiter.from_json(b'{"foo":\n[1,\n2, x', error_in_path=True)
assert exc_info.value.kind() == 'ExpectedSomeValue'
assert exc_info.value.index() == 15
assert exc_info.value.line() == 3
assert exc_info.value.path() == ['foo', 2]
def test_recursion_limit():
with pytest.raises(
jiter.JsonParseError, match="recursion limit exceeded at line 1 column 202"
):
jiter.from_json(b"[" * 10_000)
def test_recursion_limit_incr():
json = b"[" + b", ".join(b"[1]" for _ in range(2000)) + b"]"
v = jiter.from_json(json)
assert len(v) == 2000
v = jiter.from_json(json)
assert len(v) == 2000
def test_extracted_value_error():
with pytest.raises(ValueError, match="expected value at line 1 column 1"):
jiter.from_json(b"xx")
def test_partial_array():
json = b'["string", true, null, 1, "foo'
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == ["string", True, None, 1]
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json[:i], partial_mode=True)
assert isinstance(parsed, list)
def test_partial_array_trailing_strings():
json = b'["string", true, null, 1, "foo'
parsed = jiter.from_json(json, partial_mode='trailing-strings')
assert parsed == ["string", True, None, 1, "foo"]
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json[:i], partial_mode='trailing-strings')
assert isinstance(parsed, list)
def test_partial_array_first():
json = b"["
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == []
with pytest.raises(ValueError, match="EOF while parsing a list at line 1 column 1"):
jiter.from_json(json)
with pytest.raises(ValueError, match="EOF while parsing a list at line 1 column 1"):
jiter.from_json(json, partial_mode='off')
def test_partial_object():
json = b'{"a": 1, "b": 2, "c'
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == {"a": 1, "b": 2}
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json, partial_mode=True)
assert isinstance(parsed, dict)
def test_partial_object_string():
json = b'{"a": 1, "b": 2, "c": "foo'
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == {"a": 1, "b": 2}
parsed = jiter.from_json(json, partial_mode='on')
assert parsed == {"a": 1, "b": 2}
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json, partial_mode=True)
assert isinstance(parsed, dict)
json = b'{"title": "Pride and Prejudice", "author": "Jane A'
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == {"title": "Pride and Prejudice"}
def test_partial_object_string_trailing_strings():
json = b'{"a": 1, "b": 2, "c": "foo'
parsed = jiter.from_json(json, partial_mode='trailing-strings')
assert parsed == {"a": 1, "b": 2, "c": "foo"}
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json, partial_mode=True)
assert isinstance(parsed, dict)
json = b'{"title": "Pride and Prejudice", "author": "Jane A'
parsed = jiter.from_json(json, partial_mode='trailing-strings')
assert parsed == {"title": "Pride and Prejudice", "author": "Jane A"}
def test_partial_nested():
json = b'{"a": 1, "b": 2, "c": [1, 2, {"d": 1, '
parsed = jiter.from_json(json, partial_mode=True)
assert parsed == {"a": 1, "b": 2, "c": [1, 2, {"d": 1}]}
# test that stopping at every points is ok
for i in range(1, len(json)):
parsed = jiter.from_json(json[:i], partial_mode=True)
assert isinstance(parsed, dict)
def test_cache_usage_all():
jiter.cache_clear()
parsed = jiter.from_json(b'{"foo": "bar", "spam": 3}', cache_mode="all")
assert parsed == {"foo": "bar", "spam": 3}
assert jiter.cache_usage() == 3
def test_cache_usage_keys():
jiter.cache_clear()
parsed = jiter.from_json(b'{"foo": "bar", "spam": 3}', cache_mode="keys")
assert parsed == {"foo": "bar", "spam": 3}
assert jiter.cache_usage() == 2
def test_cache_usage_none():
jiter.cache_clear()
parsed = jiter.from_json(
b'{"foo": "bar", "spam": 3}',
cache_mode="none",
)
assert parsed == {"foo": "bar", "spam": 3}
assert jiter.cache_usage() == 0
def test_use_tape():
json = ' "foo\\nbar" '.encode()
jiter.cache_clear()
parsed = jiter.from_json(json, cache_mode=False)
assert parsed == "foo\nbar"
def test_unicode():
json = '{"💩": "£"}'.encode()
jiter.cache_clear()
parsed = jiter.from_json(json, cache_mode=False)
assert parsed == {"💩": "£"}
def test_unicode_cache():
json = '{"💩": "£"}'.encode()
jiter.cache_clear()
parsed = jiter.from_json(json)
assert parsed == {"💩": "£"}
def test_json_float():
f = jiter.LosslessFloat(b'123.45')
assert str(f) == '123.45'
assert repr(f) == 'LosslessFloat(123.45)'
assert float(f) == 123.45
assert f.as_decimal() == Decimal('123.45')
assert bytes(f) == b'123.45'
def test_json_float_scientific():
f = jiter.LosslessFloat(b'123e4')
assert str(f) == '123e4'
assert float(f) == 123e4
assert f.as_decimal() == Decimal('123e4')
def test_json_float_invalid():
with pytest.raises(ValueError, match='trailing characters at line 1 column 6'):
jiter.LosslessFloat(b'123.4x')
def test_lossless_floats():
f = jiter.from_json(b'12.3')
assert isinstance(f, float)
assert f == 12.3
f = jiter.from_json(b'12.3', lossless_floats=True)
assert isinstance(f, jiter.LosslessFloat)
assert str(f) == '12.3'
assert float(f) == 12.3
assert f.as_decimal() == Decimal('12.3')
f = jiter.from_json(b'123.456789123456789e45', lossless_floats=True)
assert isinstance(f, jiter.LosslessFloat)
assert 123e45 < float(f) < 124e45
assert f.as_decimal() == Decimal('1.23456789123456789E+47')
assert bytes(f) == b'123.456789123456789e45'
assert str(f) == '123.456789123456789e45'
assert repr(f) == 'LosslessFloat(123.456789123456789e45)'
def test_lossless_floats_int():
v = jiter.from_json(b'123', lossless_floats=True)
assert isinstance(v, int)
assert v == 123
def test_unicode_roundtrip():
original = ['中文']
json_data = json.dumps(original).encode()
assert jiter.from_json(json_data) == original
assert json.loads(json_data) == original
def test_unicode_roundtrip_ensure_ascii():
original = {'name': '中文'}
json_data = json.dumps(original, ensure_ascii=False).encode()
assert jiter.from_json(json_data, cache_mode=False) == original
assert json.loads(json_data) == original