forked from pydantic/pydantic-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_build.py
141 lines (110 loc) · 4.73 KB
/
test_build.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
import pickle
import pytest
from pydantic_core import SchemaError, SchemaValidator, validate_core_schema
from pydantic_core import core_schema as cs
def test_build_error_type():
with pytest.raises(SchemaError, match="Input tag 'foobar' found using 'type' does not match any of the"):
validate_core_schema({'type': 'foobar', 'title': 'TestModel'})
def test_build_error_internal():
with pytest.raises(SchemaError, match='Input should be a valid integer, unable to parse string as an integer'):
validate_core_schema({'type': 'str', 'min_length': 'xxx', 'title': 'TestModel'})
def test_build_error_deep():
with pytest.raises(SchemaError, match='Input should be a valid integer, unable to parse string as an integer'):
validate_core_schema(
{
'title': 'MyTestModel',
'type': 'typed-dict',
'fields': {'age': {'schema': {'type': 'int', 'ge': 'not-int'}}},
}
)
def test_schema_as_string():
v = SchemaValidator({'type': 'bool'})
assert v.validate_python('tRuE') is True
def test_schema_wrong_type(pydantic_version):
with pytest.raises(SchemaError) as exc_info:
validate_core_schema(1)
assert str(exc_info.value) == (
'Invalid Schema:\n Input should be a valid dictionary or object to'
' extract fields from [type=model_attributes_type, input_value=1, input_type=int]\n'
f' For further information visit https://errors.pydantic.dev/{pydantic_version}/v/model_attributes_type'
)
assert exc_info.value.errors() == [
{
'input': 1,
'loc': (),
'msg': 'Input should be a valid dictionary or object to extract fields from',
'type': 'model_attributes_type',
}
]
assert exc_info.value.error_count() == 1
@pytest.mark.parametrize('pickle_protocol', range(1, pickle.HIGHEST_PROTOCOL + 1))
def test_pickle(pickle_protocol: int) -> None:
v1 = SchemaValidator({'type': 'bool'})
assert v1.validate_python('tRuE') is True
p = pickle.dumps(v1, protocol=pickle_protocol)
v2 = pickle.loads(p)
assert v2.validate_python('tRuE') is True
assert repr(v1) == repr(v2)
@pytest.mark.skip
def test_schema_definition_error():
schema = {'type': 'union', 'choices': []}
schema['choices'].append({'type': 'nullable', 'schema': schema})
with pytest.raises(SchemaError, match='Recursion error - cyclic reference detected'):
validate_core_schema(schema)
def test_not_schema_definition_error():
schema = {
'type': 'typed-dict',
'fields': {
f'f_{i}': {'type': 'typed-dict-field', 'schema': {'type': 'nullable', 'schema': {'type': 'int'}}}
for i in range(101)
},
}
v = SchemaValidator(schema)
assert repr(v).count('TypedDictField') == 101
def test_no_type():
with pytest.raises(SchemaError, match="Unable to extract tag using discriminator 'type'"):
validate_core_schema({})
def test_wrong_type():
with pytest.raises(SchemaError, match="Input tag 'unknown' found using 'type' does not match any of the"):
validate_core_schema({'type': 'unknown'})
def test_function_no_mode():
with pytest.raises(SchemaError, match="Input tag 'function' found using 'type' does not match any of the"):
validate_core_schema({'type': 'function'})
def test_try_self_schema_discriminator():
"""Trying to use self-schema when it shouldn't be used"""
v = SchemaValidator({'type': 'tagged-union', 'choices': {'int': {'type': 'int'}}, 'discriminator': 'self-schema'})
assert 'discriminator: LookupKey' in repr(v)
def test_build_recursive_schema_from_defs() -> None:
"""
Validate a schema representing mutually recursive models, analogous to the following JSON schema:
```json
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"oneOf": [{"$ref": "#/$defs/a"}],
"$defs": {
"a": {
"type": "object",
"properties": {"b": {"type": "array", "items": {"$ref": "#/$defs/a"}}},
"required": ["b"],
},
"b": {
"type": "object",
"properties": {"a": {"type": "array", "items": {"$ref": "#/$defs/b"}}},
"required": ["a"],
},
},
}
```
"""
s = cs.definitions_schema(
cs.definition_reference_schema(schema_ref='a'),
[
cs.typed_dict_schema(
{'b': cs.typed_dict_field(cs.list_schema(cs.definition_reference_schema('b')))}, ref='a'
),
cs.typed_dict_schema(
{'a': cs.typed_dict_field(cs.list_schema(cs.definition_reference_schema('a')))}, ref='b'
),
],
)
SchemaValidator(s)