forked from pydantic/pydantic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_set.py
189 lines (166 loc) · 7.19 KB
/
test_set.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
import re
from typing import Any, Dict
import pytest
from pydantic_core import SchemaValidator, ValidationError
from ..conftest import Err, PyAndJson
@pytest.mark.parametrize(
'input_value,expected',
[
([], set()),
([1, 2, 3], {1, 2, 3}),
([1, 2, '3'], {1, 2, 3}),
([1, 2, 3, 2, 3], {1, 2, 3}),
(5, Err('Value must be a valid set [kind=set_type, input_value=5, input_type=int]')),
],
)
def test_set_ints_both(py_and_json: PyAndJson, input_value, expected):
v = py_and_json({'type': 'set', 'items_schema': {'type': 'int'}})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_test(input_value)
else:
assert v.validate_test(input_value) == expected
@pytest.mark.parametrize('input_value,expected', [([1, 2.5, '3'], {1, 2.5, '3'})])
def test_set_no_validators_both(py_and_json: PyAndJson, input_value, expected):
v = py_and_json({'type': 'set'})
assert v.validate_test(input_value) == expected
@pytest.mark.parametrize(
'input_value,expected',
[
([1, 2.5, '3'], {1, 2.5, '3'}),
('foo', Err('Value must be a valid set')),
(1, Err('Value must be a valid set')),
(1.0, Err('Value must be a valid set')),
(False, Err('Value must be a valid set')),
],
)
def test_frozenset_no_validators_both(py_and_json: PyAndJson, input_value, expected):
v = py_and_json({'type': 'set'})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_test(input_value)
else:
assert v.validate_test(input_value) == expected
@pytest.mark.parametrize(
'input_value,expected',
[
({1, 2, 3}, {1, 2, 3}),
(set(), set()),
([1, 2, 3, 2, 3], {1, 2, 3}),
([], set()),
((1, 2, 3, 2, 3), {1, 2, 3}),
((), set()),
(frozenset([1, 2, 3, 2, 3]), {1, 2, 3}),
({'abc'}, Err('0\n Value must be a valid integer')),
({1: 2}, Err('1 validation error for set[int]\n Value must be a valid set')),
('abc', Err('Value must be a valid set')),
# Technically correct, but does anyone actually need this? I think needs a new type in pyo3
pytest.param({1: 10, 2: 20, 3: 30}.keys(), {1, 2, 3}, marks=pytest.mark.xfail(raises=ValidationError)),
],
)
def test_set_ints_python(input_value, expected):
v = SchemaValidator({'type': 'set', 'items_schema': {'type': 'int'}})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_python(input_value)
else:
assert v.validate_python(input_value) == expected
@pytest.mark.parametrize('input_value,expected', [([1, 2.5, '3'], {1, 2.5, '3'}), ([(1, 2), (3, 4)], {(1, 2), (3, 4)})])
def test_set_no_validators_python(input_value, expected):
v = SchemaValidator({'type': 'set'})
assert v.validate_python(input_value) == expected
def test_set_multiple_errors():
v = SchemaValidator({'type': 'set', 'items_schema': {'type': 'int'}})
with pytest.raises(ValidationError) as exc_info:
v.validate_python(['a', (1, 2), []])
assert exc_info.value.errors() == [
{
'kind': 'int_parsing',
'loc': [0],
'message': 'Value must be a valid integer, unable to parse string as an integer',
'input_value': 'a',
},
{'kind': 'int_type', 'loc': [1], 'message': 'Value must be a valid integer', 'input_value': (1, 2)},
{'kind': 'int_type', 'loc': [2], 'message': 'Value must be a valid integer', 'input_value': []},
]
@pytest.mark.parametrize(
'kwargs,input_value,expected',
[
({'strict': True}, {1, 2, 3}, {1, 2, 3}),
({'strict': True}, set(), set()),
({'strict': True}, [1, 2, 3, 2, 3], Err('Value must be a valid set [kind=set_type,')),
({'strict': True}, [], Err('Value must be a valid set [kind=set_type,')),
({'strict': True}, (), Err('Value must be a valid set [kind=set_type,')),
({'strict': True}, (1, 2, 3), Err('Value must be a valid set [kind=set_type,')),
({'strict': True}, frozenset([1, 2, 3]), Err('Value must be a valid set [kind=set_type,')),
({'strict': True}, 'abc', Err('Value must be a valid set [kind=set_type,')),
({'min_items': 3}, {1, 2, 3}, {1, 2, 3}),
({'min_items': 3}, {1, 2}, Err('Input must have at least 3 items [kind=too_short,')),
({'max_items': 3}, {1, 2, 3}, {1, 2, 3}),
({'max_items': 3}, {1, 2, 3, 4}, Err('Input must have at most 3 items [kind=too_long,')),
],
)
def test_set_kwargs(kwargs: Dict[str, Any], input_value, expected):
v = SchemaValidator({'type': 'set', **kwargs})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_python(input_value)
else:
assert v.validate_python(input_value) == expected
@pytest.mark.parametrize('input_value,expected', [({1, 2, 3}, {1, 2, 3}), ([1, 2, 3], [1, 2, 3])])
def test_union_set_list(input_value, expected):
v = SchemaValidator({'type': 'union', 'choices': [{'type': 'set'}, {'type': 'list'}]})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_python(input_value)
else:
assert v.validate_python(input_value) == expected
@pytest.mark.parametrize(
'input_value,expected',
[
({1, 2, 3}, {1, 2, 3}),
({'a', 'b', 'c'}, {'a', 'b', 'c'}),
(
[1, 'a'],
Err(
'2 validation errors for union',
errors=[
{
'kind': 'int_type',
'loc': ['set[int]', 1],
'message': 'Value must be a valid integer',
'input_value': 'a',
},
# second because validation on the string choice comes second
{
'kind': 'str_type',
'loc': ['set[str]', 0],
'message': 'Value must be a valid string',
'input_value': 1,
},
],
),
),
],
)
def test_union_set_int_set_str(input_value, expected):
v = SchemaValidator(
{
'type': 'union',
'choices': [
{'type': 'set', 'items_schema': {'type': 'int', 'strict': True}},
{'type': 'set', 'items_schema': {'type': 'str', 'strict': True}},
],
}
)
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)) as exc_info:
v.validate_python(input_value)
if expected.errors is not None:
assert exc_info.value.errors() == expected.errors
else:
assert v.validate_python(input_value) == expected
def test_set_as_dict_keys(py_and_json: PyAndJson):
v = py_and_json({'type': 'dict', 'keys_schema': {'type': 'set'}, 'values_schema': 'int'})
with pytest.raises(ValidationError, match=re.escape('Value must be a valid set')):
v.validate_test({'foo': 'bar'})