-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_jsonstream.py
More file actions
268 lines (224 loc) · 6.54 KB
/
test_jsonstream.py
File metadata and controls
268 lines (224 loc) · 6.54 KB
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
import pytest
from decimal import Decimal
from io import StringIO, BytesIO
from json import JSONDecodeError
import math
from jsonstream import load, loads
LOADERS = [
pytest.param(loads),
pytest.param(
lambda s, **kw: load(StringIO(s), **kw),
id='load'
),
pytest.param(
lambda s, **kw: load(StringIO(s), bufsize=1, **kw),
id='load with bufsize=1'
),
]
@pytest.mark.parametrize(
'test_input,expected',
[
pytest.param(
'''
{
"a"
:
1
}
[
1
,
2
]{"squashed":"together"}''',
[{"a": 1}, [1, 2], {"squashed": "together"}],
id='document spans multiple lines'
),
pytest.param(
'[] {} [1, 2]',
[[], {}, [1, 2]],
id='multiple documents on one line'
),
pytest.param(
'null true false',
[None, True, False],
id='json common constants'
),
pytest.param(
'Infinity -Infinity',
[float('inf'), float('-inf')],
id='python json module special constants' # excluding NaN
),
pytest.param(
'1 234',
[1, 234],
id='split integers'
),
pytest.param(
'[1.5] 0.5',
[[1.5], 0.5],
id='split floats'
# a split float in an array/object produces a different error if the number
# is the root of the document
),
pytest.param(
'"some string"',
["some string"],
id='split string'
),
],
)
@pytest.mark.parametrize('loader', LOADERS)
def test_loaders(test_input, expected, loader):
assert list(loader(test_input)) == expected
@pytest.mark.parametrize(
'test_input,expected,msg',
[
pytest.param(
'{"a"',
JSONDecodeError,
"Expecting ':' delimiter",
id='missing object key/value delimiter'
),
pytest.param(
'{',
JSONDecodeError,
'Expecting property name enclosed in double quotes',
id='missing property'
),
pytest.param(
'"a',
JSONDecodeError,
'Unterminated string starting at',
id='malformed string'
),
pytest.param(
'nul',
JSONDecodeError,
'Expecting value',
id='malformed keyword'
),
pytest.param(
'[,1]',
JSONDecodeError,
'Expecting value',
id='missing array item'
),
pytest.param(
'[1 2]',
JSONDecodeError,
"Expecting ',' delimiter",
id='missing array/object item delimiter'
),
pytest.param(
'1.',
JSONDecodeError,
"Expecting value",
id='malformed decimal'
),
]
)
@pytest.mark.parametrize('loader', LOADERS)
def test_failed_decodes(test_input, expected, msg, loader):
with pytest.raises(expected) as cm:
list(loader(test_input))
assert cm.value.msg == msg
@pytest.mark.parametrize('loader', LOADERS)
def test_decode_nan(loader):
doc = 'NaN'
[result] = list(loader(doc))
assert math.isnan(result)
def test_load_buffer_maxed_out():
stream = StringIO('"tiny" "some very long string"')
stream.seek(0)
gen = iter(load(stream, bufsize=1, max_bufsize=6))
assert next(gen) == 'tiny'
with pytest.raises(ValueError) as cm:
next(gen)
assert cm.match('max buffer size exceeded')
def test_preceding_whitespace_discarded_when_buffer_overfull():
doc = '"small"'
max_buf = len(doc)
stream = StringIO(' ' * max_buf + doc)
stream.seek(0)
assert list(load(stream, bufsize=1, max_bufsize=max_buf)) == ['small']
def test_stream_offset_in_error():
offset = 10
doc = ' ' * offset + 'cannot_decode'
stream = StringIO(doc)
stream.seek(0)
with pytest.raises(JSONDecodeError) as cm:
list(load(stream, bufsize=1))
assert cm.value.stream_offset == offset
assert cm.match(r'\(stream offset 10\)$')
@pytest.mark.parametrize(
'expected,loader_args',
[
pytest.param(
[Decimal('123'), 5.67],
dict(parse_int=Decimal),
id='parse_int'
),
pytest.param(
[123, Decimal('5.67')],
dict(parse_float=Decimal),
id='parse_float'
),
pytest.param(
[123.0, (5.67+0j)],
dict(parse_int=float, parse_float=complex),
id='parse_both'
),
],
)
@pytest.mark.parametrize('loader', LOADERS)
def test_parse_number_hooks(loader, expected, loader_args):
test_input = '123 5.67'
result = list(loader(test_input, **loader_args))
assert result == expected
assert list(map(type, result)) == list(map(type, expected))
@pytest.mark.parametrize(
'loader',
[
pytest.param(loads),
pytest.param(lambda s: load(BytesIO(s)), id='load'),
pytest.param(lambda s: load(BytesIO(s), bufsize=1), id='load with bufsize=1'),
]
)
def test_decode_bytes(loader):
test_input = '"$$$ != £££"'.encode('utf8')
expected = ['$$$ != £££']
assert list(loader(test_input)) == expected
@pytest.mark.parametrize('loader', LOADERS)
def test_custom_delimiter_works(loader):
doc ='[1,2],[3, 4]'
assert list(loader(doc, separator=',')) == [[1, 2], [3, 4]]
@pytest.mark.parametrize('loader', LOADERS)
def test_custom_delimiter_with_wrong_delimiter(loader):
doc ='[1,2],[3, 4]'
with pytest.raises(ValueError) as cm:
list(loader(doc, separator='/'))
assert str(cm.value) == "Expected '/' delimiter"
@pytest.mark.parametrize('loader', LOADERS)
def test_custom_delimiter_with_empty_value(loader):
doc = '[],,[]'
with pytest.raises(JSONDecodeError) as cm:
list(loader(doc, separator=','))
assert cm.value.msg == 'Expecting value'
assert cm.value.pos + getattr(cm.value, 'stream_offset', 0) == 3
@pytest.mark.parametrize(
'test_input,args',
[
pytest.param('', {}, id='simple'),
pytest.param('', dict(separator=','), id='with separator'),
pytest.param(' \t\n ', {}, id='only whitespace'),
]
)
@pytest.mark.parametrize('loader', LOADERS)
def test_empty_document(test_input, loader, args):
assert list(loader(test_input, **args)) == []
@pytest.mark.xfail
def test_async_load():
pytest.fail('not implemented')
@pytest.mark.xfail
def test_optimised_newline_separator(loader):
pytest.fail('not implemented')