forked from pydantic/pydantic-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_config.py
153 lines (128 loc) · 5.03 KB
/
test_config.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
import math
import re
import pytest
from dirty_equals import FunctionCheck, HasAttributes, IsInstance
from pydantic_core import CoreConfig, SchemaValidator, ValidationError
from .conftest import Err, plain_repr
def test_on_field():
v = SchemaValidator({'type': 'str', 'min_length': 2, 'max_length': 5})
r = plain_repr(v)
assert 'min_length:Some(2)' in r
assert 'max_length:Some(5)' in r
assert v.isinstance_python('test') is True
assert v.isinstance_python('test long') is False
def test_on_config():
v = SchemaValidator({'type': 'str'}, {'str_max_length': 5})
assert 'max_length:Some(5)' in plain_repr(v)
assert v.isinstance_python('test') is True
assert v.isinstance_python('test long') is False
def test_field_priority_arg():
v = SchemaValidator({'type': 'str', 'max_length': 5}, {'str_max_length': 10})
assert 'max_length:Some(5)' in plain_repr(v)
assert v.isinstance_python('test') is True
assert v.isinstance_python('test long') is False
class MyModel:
# this is not required, but it avoids `__pydantic_fields_set__` being included in `__dict__`
__slots__ = '__dict__', '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__'
def test_on_model_class():
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
'config': {'str_max_length': 5},
'schema': {'type': 'model-fields', 'fields': {'f': {'type': 'model-field', 'schema': {'type': 'str'}}}},
}
)
assert 'max_length:Some(5)' in plain_repr(v)
assert v.isinstance_python({'f': 'test'}) is True
assert v.isinstance_python({'f': 'test long'}) is False
def test_field_priority_model():
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
'config': {'str_max_length': 10},
'schema': {
'type': 'model-fields',
'fields': {'f': {'type': 'model-field', 'schema': {'type': 'str', 'max_length': 5}}},
},
}
)
assert 'max_length:Some(5)' in plain_repr(v)
assert v.isinstance_python({'f': 'test'}) is True
assert v.isinstance_python({'f': 'test long'}) is False
@pytest.mark.parametrize(
'config,float_field_schema,input_value,expected',
[
({}, {'type': 'float'}, {'x': 'nan'}, IsInstance(MyModel) & HasAttributes(x=FunctionCheck(math.isnan))),
(
{'allow_inf_nan': True},
{'type': 'float'},
{'x': 'nan'},
IsInstance(MyModel) & HasAttributes(x=FunctionCheck(math.isnan)),
),
(
{'allow_inf_nan': False},
{'type': 'float'},
{'x': 'nan'},
Err('Input should be a finite number [type=finite_number,'),
),
# field `allow_inf_nan` (if set) should have priority over global config
(
{'allow_inf_nan': True},
{'type': 'float', 'allow_inf_nan': False},
{'x': 'nan'},
Err('Input should be a finite number [type=finite_number,'),
),
(
{'allow_inf_nan': False},
{'type': 'float', 'allow_inf_nan': True},
{'x': 'nan'},
IsInstance(MyModel) & HasAttributes(x=FunctionCheck(math.isnan)),
),
],
ids=repr,
)
def test_allow_inf_nan(config: CoreConfig, float_field_schema, input_value, expected):
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
'schema': {'type': 'model-fields', 'fields': {'x': {'type': 'model-field', 'schema': float_field_schema}}},
'config': config,
}
)
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_python(input_value)
else:
output_dict = v.validate_python(input_value)
assert output_dict == expected
@pytest.mark.parametrize(
'config,input_str',
(
({}, 'type=string_type, input_value=123, input_type=int'),
({'hide_input_in_errors': False}, 'type=string_type, input_value=123, input_type=int'),
({'hide_input_in_errors': True}, 'type=string_type'),
),
)
def test_hide_input_in_errors(config, input_str):
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
'schema': {'type': 'model-fields', 'fields': {'f': {'type': 'model-field', 'schema': {'type': 'str'}}}},
},
config,
)
with pytest.raises(ValidationError, match=re.escape(f'Input should be a valid string [{input_str}]')):
assert v.validate_python({'f': 123})
def test_cache_strings():
v = SchemaValidator({'type': 'str'})
assert 'cache_strings=True' in plain_repr(v)
v = SchemaValidator({'type': 'str'}, {'cache_strings': True})
assert 'cache_strings=True' in plain_repr(v)
v = SchemaValidator({'type': 'str'}, {'cache_strings': False})
assert 'cache_strings=False' in plain_repr(v)
v = SchemaValidator({'type': 'str'}, {'cache_strings': 'keys'})
assert "cache_strings='keys'" in plain_repr(v)