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
|
import re
import pytest
from dirty_equals import HasRepr, IsStr
from pydantic_core import CoreConfig, SchemaValidator, ValidationError
from pydantic_core import core_schema as cs
from ..conftest import Err, PyAndJson
@pytest.mark.parametrize(
'input_value,expected',
[
([1, 2, 3], [1, 2, 3]),
([1, 2, '3'], [1, 2, 3]),
({1: 2, 3: 4}, [1, 3]),
('123', [1, 2, 3]),
(5, Err('[type=iterable_type, input_value=5, input_type=int]')),
([1, 'wrong'], Err("[type=int_parsing, input_value='wrong', input_type=str]")),
],
ids=repr,
)
def test_generator_json_int(py_and_json: PyAndJson, input_value, expected):
v = py_and_json({'type': 'generator', 'items_schema': {'type': 'int'}})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
list(v.validate_test(input_value))
else:
assert list(v.validate_test(input_value)) == expected
@pytest.mark.parametrize(
'config,input_str',
(
(CoreConfig(), 'type=iterable_type, input_value=5, input_type=int'),
(CoreConfig(hide_input_in_errors=False), 'type=iterable_type, input_value=5, input_type=int'),
(CoreConfig(hide_input_in_errors=True), 'type=iterable_type'),
),
)
def test_generator_json_hide_input(py_and_json: PyAndJson, config, input_str):
v = py_and_json({'type': 'generator', 'items_schema': {'type': 'int'}}, config)
with pytest.raises(ValidationError, match=re.escape(f'[{input_str}]')):
list(v.validate_test(5))
@pytest.mark.parametrize(
'input_value,expected',
[
([1, 2, 3], [1, 2, 3]),
([1, 2, '3'], [1, 2, '3']),
({'1': 2, '3': 4}, ['1', '3']),
('123', ['1', '2', '3']),
(5, Err('[type=iterable_type, input_value=5, input_type=int]')),
([1, 'wrong'], [1, 'wrong']),
],
ids=repr,
)
def test_generator_json_any(py_and_json: PyAndJson, input_value, expected):
v = py_and_json({'type': 'generator'})
if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
list(v.validate_test(input_value))
else:
assert list(v.validate_test(input_value)) == expected
def test_error_index(py_and_json: PyAndJson):
v = py_and_json({'type': 'generator', 'items_schema': {'type': 'int'}})
gen = v.validate_test(['wrong'])
assert gen.index == 0
with pytest.raises(ValidationError) as exc_info:
next(gen)
assert gen.index == 1
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.title == 'ValidatorIterator'
assert str(exc_info.value).startswith('1 validation error for ValidatorIterator\n')
assert exc_info.value.errors(include_url=False) == [
{
'type': 'int_parsing',
'loc': (0,),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'input': 'wrong',
}
]
gen = v.validate_test([1, 2, 3, 'wrong', 4])
assert gen.index == 0
assert next(gen) == 1
assert gen.index == 1
assert next(gen) == 2
assert gen.index == 2
assert next(gen) == 3
assert gen.index == 3
with pytest.raises(ValidationError) as exc_info:
next(gen)
assert gen.index == 4
assert exc_info.value.errors(include_url=False) == [
{
'type': 'int_parsing',
'loc': (3,),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'input': 'wrong',
}
]
assert next(gen) == 4
assert gen.index == 5
def test_too_long(py_and_json: PyAndJson):
v = py_and_json({'type': 'generator', 'items_schema': {'type': 'int'}, 'max_length': 2})
assert list(v.validate_test([1])) == [1]
assert list(v.validate_test([1, 2])) == [1, 2]
with pytest.raises(ValidationError) as exc_info:
list(v.validate_test([1, 2, 3]))
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_long',
'loc': (),
'msg': 'Generator should have at most 2 items after validation, not more',
'input': [1, 2, 3],
'ctx': {'field_type': 'Generator', 'max_length': 2, 'actual_length': None},
}
]
def test_too_short(py_and_json: PyAndJson):
v = py_and_json({'type': 'generator', 'items_schema': {'type': 'int'}, 'min_length': 2})
assert list(v.validate_test([1, 2, 3])) == [1, 2, 3]
assert list(v.validate_test([1, 2])) == [1, 2]
with pytest.raises(ValidationError) as exc_info:
list(v.validate_test([1]))
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'too_short',
'loc': (),
'msg': 'Generator should have at least 2 items after validation, not 1',
'input': [1],
'ctx': {'field_type': 'Generator', 'min_length': 2, 'actual_length': 1},
}
]
def gen():
yield 1
yield 2
yield 3
def test_generator_too_long():
v = SchemaValidator(cs.generator_schema(items_schema=cs.int_schema(), max_length=2))
validating_iterator = v.validate_python(gen())
# Ensure the error happens at exactly the right step:
assert next(validating_iterator) == 1
assert next(validating_iterator) == 2
with pytest.raises(ValidationError) as exc_info:
next(validating_iterator)
errors = exc_info.value.errors(include_url=False)
# insert_assert(errors)
assert errors == [
{
'type': 'too_long',
'loc': (),
'input': HasRepr(IsStr(regex='<generator object gen at .+>')),
'msg': 'Generator should have at most 2 items after validation, not more',
'ctx': {'field_type': 'Generator', 'max_length': 2, 'actual_length': None},
}
]
def test_generator_too_short():
v = SchemaValidator(cs.generator_schema(items_schema=cs.int_schema(), min_length=4))
validating_iterator = v.validate_python(gen())
# Ensure the error happens at exactly the right step:
assert next(validating_iterator) == 1
assert next(validating_iterator) == 2
assert next(validating_iterator) == 3
with pytest.raises(ValidationError) as exc_info:
next(validating_iterator)
errors = exc_info.value.errors(include_url=False)
# insert_assert(errors)
assert errors == [
{
'type': 'too_short',
'input': HasRepr(IsStr(regex='<generator object gen at .+>')),
'loc': (),
'msg': 'Generator should have at least 4 items after validation, not 3',
'ctx': {'field_type': 'Generator', 'min_length': 4, 'actual_length': 3},
}
]
|