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
|
import pytest
from pydantic_core import SchemaError, SchemaValidator, ValidationError, core_schema
class Foo:
pass
class Foobar(Foo):
pass
class Bar:
pass
def test_is_subclass_basic():
v = SchemaValidator(core_schema.is_subclass_schema(Foo))
assert v.validate_python(Foo) == Foo
with pytest.raises(ValidationError) as exc_info:
v.validate_python(Bar)
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'is_subclass_of',
'loc': (),
'msg': 'Input should be a subclass of Foo',
'input': Bar,
'ctx': {'class': 'Foo'},
}
]
@pytest.mark.parametrize(
'input_value,valid',
[
(Foo, True),
(Foobar, True),
(Bar, False),
(type, False),
(1, False),
('foo', False),
(Foo(), False),
(Foobar(), False),
(Bar(), False),
],
)
def test_is_subclass(input_value, valid):
v = SchemaValidator(core_schema.is_subclass_schema(Foo))
assert v.isinstance_python(input_value) == valid
def test_not_parent():
v = SchemaValidator(core_schema.is_subclass_schema(Foobar))
assert v.isinstance_python(Foobar)
assert not v.isinstance_python(Foo)
def test_invalid_type():
with pytest.raises(SchemaError, match="TypeError: 'Foo' object cannot be converted to 'PyType"):
SchemaValidator(core_schema.is_subclass_schema(Foo()))
def test_custom_repr():
v = SchemaValidator(core_schema.is_subclass_schema(Foo, cls_repr='Spam'))
assert v.validate_python(Foo) == Foo
with pytest.raises(ValidationError) as exc_info:
v.validate_python(Bar)
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'is_subclass_of',
'loc': (),
'msg': 'Input should be a subclass of Spam',
'input': Bar,
'ctx': {'class': 'Spam'},
}
]
def test_is_subclass_json() -> None:
v = SchemaValidator(core_schema.is_subclass_schema(Foo))
with pytest.raises(ValidationError) as exc_info:
v.validate_json("'Foo'")
assert exc_info.value.errors()[0]['type'] == 'needs_python_object'
|