forked from pydantic/pydantic-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_json.py
367 lines (280 loc) · 12.2 KB
/
test_json.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import json
import platform
import re
from typing import List
import pytest
from dirty_equals import IsFloatNan, IsList
import pydantic_core
from pydantic_core import (
PydanticSerializationError,
SchemaSerializer,
SchemaValidator,
ValidationError,
core_schema,
to_json,
to_jsonable_python,
)
from .conftest import Err
@pytest.mark.parametrize(
'input_value,output_value',
[('false', False), ('true', True), ('0', False), ('1', True), ('"yes"', True), ('"no"', False)],
)
def test_bool(input_value, output_value):
v = SchemaValidator({'type': 'bool'})
assert v.validate_json(input_value) == output_value
@pytest.mark.parametrize('input_value', ['[1, 2, 3]', b'[1, 2, 3]', bytearray(b'[1, 2, 3]')])
def test_input_types(input_value):
v = SchemaValidator({'type': 'list', 'items_schema': {'type': 'int'}})
assert v.validate_json(input_value) == [1, 2, 3]
def test_input_type_invalid():
v = SchemaValidator({'type': 'list', 'items_schema': {'type': 'int'}})
with pytest.raises(ValidationError, match=r'JSON input should be string, bytes or bytearray \[type=json_type,'):
v.validate_json([])
def test_null():
assert SchemaValidator({'type': 'none'}).validate_json('null') is None
def test_str():
s = SchemaValidator({'type': 'str'})
assert s.validate_json('"foobar"') == 'foobar'
with pytest.raises(ValidationError, match=r'Input should be a valid string \[type=string_type,'):
s.validate_json('false')
with pytest.raises(ValidationError, match=r'Input should be a valid string \[type=string_type,'):
s.validate_json('123')
def test_bytes():
s = SchemaValidator({'type': 'bytes'})
assert s.validate_json('"foobar"') == b'foobar'
with pytest.raises(ValidationError, match=r'Input should be a valid bytes \[type=bytes_type,'):
s.validate_json('false')
with pytest.raises(ValidationError, match=r'Input should be a valid bytes \[type=bytes_type,'):
s.validate_json('123')
# A number well outside of i64 range
_BIG_NUMBER_STR = '1' + ('0' * 40)
@pytest.mark.parametrize(
'input_value,expected',
[
('123', 123),
('"123"', 123),
('123.0', 123),
('"123.0"', 123),
(_BIG_NUMBER_STR, int(_BIG_NUMBER_STR)),
('123.4', Err('Input should be a valid integer, got a number with a fractional part [type=int_from_float,')),
('"123.4"', Err('Input should be a valid integer, unable to parse string as an integer [type=int_parsing,')),
('"string"', Err('Input should be a valid integer, unable to parse string as an integer [type=int_parsing,')),
],
)
def test_int(input_value, expected):
v = SchemaValidator({'type': 'int'})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_json(input_value)
else:
assert v.validate_json(input_value) == expected
@pytest.mark.parametrize(
'input_value,expected',
[
('123.4', 123.4),
('123.0', 123.0),
('123', 123.0),
('"123.4"', 123.4),
('"123.0"', 123.0),
('"123"', 123.0),
('"string"', Err('Input should be a valid number, unable to parse string as a number [type=float_parsing,')),
],
)
def test_float(input_value, expected):
v = SchemaValidator({'type': 'float'})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_json(input_value)
else:
assert v.validate_json(input_value) == expected
def test_typed_dict():
v = SchemaValidator(
{
'type': 'typed-dict',
'fields': {
'field_a': {'type': 'typed-dict-field', 'schema': {'type': 'str'}},
'field_b': {'type': 'typed-dict-field', 'schema': {'type': 'int'}},
},
}
)
# language=json
input_str = '{"field_a": "abc", "field_b": 1}'
assert v.validate_json(input_str) == {'field_a': 'abc', 'field_b': 1}
# language=json
input_str = '{"field_a": "a", "field_a": "b", "field_b": 1}'
assert v.validate_json(input_str) == {'field_a': 'b', 'field_b': 1}
assert v.validate_json(input_str) == {'field_a': 'b', 'field_b': 1}
def test_float_no_remainder():
v = SchemaValidator({'type': 'int'})
assert v.validate_json('123.0') == 123
def test_error_loc():
v = SchemaValidator(
{
'type': 'typed-dict',
'fields': {
'field_a': {'type': 'typed-dict-field', 'schema': {'type': 'list', 'items_schema': {'type': 'int'}}}
},
'extras_schema': {'type': 'int'},
'extra_behavior': 'allow',
}
)
# assert v.validate_json('{"field_a": [1, 2, "3"]}') == ({'field_a': [1, 2, 3]}, {'field_a'})
with pytest.raises(ValidationError) as exc_info:
v.validate_json('{"field_a": [1, 2, "wrong"]}')
assert exc_info.value.errors(include_url=False) == [
{
'type': 'int_parsing',
'loc': ('field_a', 2),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'input': 'wrong',
}
]
def test_dict():
v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'int'}, 'values_schema': {'type': 'int'}})
assert v.validate_json('{"1": 2, "3": 4}') == {1: 2, 3: 4}
# duplicate keys, the last value wins, like with python
assert json.loads('{"1": 1, "1": 2}') == {'1': 2}
assert v.validate_json('{"1": 1, "1": 2}') == {1: 2}
def test_dict_any_value():
v = SchemaValidator({'type': 'dict', 'keys_schema': {'type': 'str'}})
assert v.validate_json('{"1": 1, "2": "a", "3": null}') == {'1': 1, '2': 'a', '3': None}
def test_json_invalid():
v = SchemaValidator({'type': 'bool'})
with pytest.raises(ValidationError) as exc_info:
v.validate_json('"foobar')
assert exc_info.value.errors(include_url=False) == [
{
'type': 'json_invalid',
'loc': (),
'msg': 'Invalid JSON: EOF while parsing a string at line 1 column 7',
'input': '"foobar',
'ctx': {'error': 'EOF while parsing a string at line 1 column 7'},
}
]
with pytest.raises(ValidationError) as exc_info:
v.validate_json('[1,\n2,\n3,]')
assert exc_info.value.errors(include_url=False) == [
{
'type': 'json_invalid',
'loc': (),
'msg': 'Invalid JSON: trailing comma at line 3 column 3',
'input': '[1,\n2,\n3,]',
'ctx': {'error': 'trailing comma at line 3 column 3'},
}
]
class Foobar:
def __str__(self):
return 'Foobar.__str__'
def fallback_func(v):
return f'fallback:{type(v).__name__}'
def test_to_json():
assert to_json([1, 2]) == b'[1,2]'
assert to_json([1, 2], indent=2) == b'[\n 1,\n 2\n]'
assert to_json([1, b'x']) == b'[1,"x"]'
# kwargs required
with pytest.raises(TypeError, match=r'to_json\(\) takes 1 positional arguments but 2 were given'):
to_json([1, 2], 2)
def test_to_json_fallback():
with pytest.raises(PydanticSerializationError, match=r'Unable to serialize unknown type: <.+\.Foobar'):
to_json(Foobar())
assert to_json(Foobar(), serialize_unknown=True) == b'"Foobar.__str__"'
assert to_json(Foobar(), serialize_unknown=True, fallback=fallback_func) == b'"fallback:Foobar"'
assert to_json(Foobar(), fallback=fallback_func) == b'"fallback:Foobar"'
def test_to_jsonable_python():
assert to_jsonable_python([1, 2]) == [1, 2]
assert to_jsonable_python({1, 2}) == IsList(1, 2, check_order=False)
assert to_jsonable_python([1, b'x']) == [1, 'x']
assert to_jsonable_python([0, 1, 2, 3, 4], exclude={1, 3}) == [0, 2, 4]
def test_to_jsonable_python_fallback():
with pytest.raises(PydanticSerializationError, match=r'Unable to serialize unknown type: <.+\.Foobar'):
to_jsonable_python(Foobar())
assert to_jsonable_python(Foobar(), serialize_unknown=True) == 'Foobar.__str__'
assert to_jsonable_python(Foobar(), serialize_unknown=True, fallback=fallback_func) == 'fallback:Foobar'
assert to_jsonable_python(Foobar(), fallback=fallback_func) == 'fallback:Foobar'
def test_to_jsonable_python_schema_serializer():
class Foobar:
def __init__(self, my_foo: int, my_inners: List['Foobar']):
self.my_foo = my_foo
self.my_inners = my_inners
# force a recursive model to ensure we exercise the transfer of definitions from the loaded
# serializer
c = core_schema.definitions_schema(
core_schema.definition_reference_schema(schema_ref='foobar'),
[
core_schema.model_schema(
Foobar,
core_schema.typed_dict_schema(
{
'my_foo': core_schema.typed_dict_field(core_schema.int_schema(), serialization_alias='myFoo'),
'my_inners': core_schema.typed_dict_field(
core_schema.list_schema(core_schema.definition_reference_schema('foobar')),
serialization_alias='myInners',
),
}
),
ref='foobar',
)
],
)
v = SchemaValidator(c)
s = SchemaSerializer(c)
Foobar.__pydantic_validator__ = v
Foobar.__pydantic_serializer__ = s
instance = Foobar(my_foo=1, my_inners=[Foobar(my_foo=2, my_inners=[])])
assert to_jsonable_python(instance) == {'myFoo': 1, 'myInners': [{'myFoo': 2, 'myInners': []}]}
assert to_jsonable_python(instance, by_alias=False) == {'my_foo': 1, 'my_inners': [{'my_foo': 2, 'my_inners': []}]}
assert to_json(instance) == b'{"myFoo":1,"myInners":[{"myFoo":2,"myInners":[]}]}'
assert to_json(instance, by_alias=False) == b'{"my_foo":1,"my_inners":[{"my_foo":2,"my_inners":[]}]}'
def test_cycle_same():
def fallback_func_passthrough(obj):
return obj
f = Foobar()
with pytest.raises(ValueError, match=r'Circular reference detected \(id repeated\)'):
to_jsonable_python(f, fallback=fallback_func_passthrough)
with pytest.raises(ValueError, match=r'Circular reference detected \(id repeated\)'):
to_json(f, fallback=fallback_func_passthrough)
@pytest.mark.skipif(
platform.python_implementation() == 'PyPy' and pydantic_core._pydantic_core.build_profile == 'debug',
reason='PyPy does not have enough stack space for Rust debug builds to recurse very deep',
)
def test_cycle_change():
def fallback_func_change_id(obj):
return Foobar()
f = Foobar()
with pytest.raises(ValueError, match=r'Circular reference detected \(depth exceeded\)'):
to_jsonable_python(f, fallback=fallback_func_change_id)
with pytest.raises(ValueError, match=r'Circular reference detected \(depth exceeded\)'):
to_json(f, fallback=fallback_func_change_id)
class FoobarHash:
def __str__(self):
return 'Foobar.__str__'
def __hash__(self):
return 1
def test_json_key_fallback():
x = {FoobarHash(): 1}
assert to_jsonable_python(x, serialize_unknown=True) == {'Foobar.__str__': 1}
assert to_jsonable_python(x, fallback=fallback_func) == {'fallback:FoobarHash': 1}
assert to_json(x, serialize_unknown=True) == b'{"Foobar.__str__":1}'
assert to_json(x, fallback=fallback_func) == b'{"fallback:FoobarHash":1}'
class BedReprMeta(type):
def __repr__(self):
raise ValueError('bad repr')
class BadRepr(metaclass=BedReprMeta):
def __repr__(self):
raise ValueError('bad repr')
def __hash__(self):
return 1
def test_bad_repr():
b = BadRepr()
error_msg = '^Unable to serialize unknown type: <unprintable BedReprMeta object>$'
with pytest.raises(PydanticSerializationError, match=error_msg):
to_jsonable_python(b)
assert to_jsonable_python(b, serialize_unknown=True) == '<Unserializable BadRepr object>'
with pytest.raises(PydanticSerializationError, match=error_msg):
to_json(b)
assert to_json(b, serialize_unknown=True) == b'"<Unserializable BadRepr object>"'
def test_inf_nan_allow():
v = SchemaValidator(core_schema.float_schema(allow_inf_nan=True))
assert v.validate_json('Infinity') == float('inf')
assert v.validate_json('-Infinity') == float('-inf')
assert v.validate_json('NaN') == IsFloatNan()