mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 14:50:19 +00:00
a96794ddc1
* move xfail from module to individual failing tests * missed a test. fixed formatting. * removed xfail mark from a passing test
101 lines
2.9 KiB
Python
101 lines
2.9 KiB
Python
from typing import Dict, List, Mapping, Union
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, ValidationError
|
|
from pydantic.dataclasses import dataclass
|
|
from pydantic.tools import parse_obj_as, schema_json_of, schema_of
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
@pytest.mark.parametrize('obj,type_,parsed', [('1', int, 1), (['1'], List[int], [1])])
|
|
def test_parse_obj(obj, type_, parsed):
|
|
assert parse_obj_as(type_, obj) == parsed
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parse_obj_as_model():
|
|
class Model(BaseModel):
|
|
x: int
|
|
y: bool
|
|
z: str
|
|
|
|
model_inputs = {'x': '1', 'y': 'true', 'z': 'abc'}
|
|
assert parse_obj_as(Model, model_inputs) == Model(**model_inputs)
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parse_obj_preserves_subclasses():
|
|
class ModelA(BaseModel):
|
|
a: Mapping[int, str]
|
|
|
|
class ModelB(ModelA):
|
|
b: int
|
|
|
|
model_b = ModelB(a={1: 'f'}, b=2)
|
|
|
|
parsed = parse_obj_as(List[ModelA], [model_b])
|
|
assert parsed == [model_b]
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parse_obj_fails():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a')
|
|
assert exc_info.value.errors() == [
|
|
{'loc': ('__root__',), 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}
|
|
]
|
|
assert exc_info.value.model.__name__ == 'ParsingModel[int]'
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parsing_model_naming():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a')
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for ParsingModel[int]'
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a', type_name='ParsingModel')
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for ParsingModel'
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
parse_obj_as(int, 'a', type_name=lambda type_: type_.__name__)
|
|
assert str(exc_info.value).split('\n')[0] == '1 validation error for int'
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parse_as_dataclass():
|
|
@dataclass
|
|
class PydanticDataclass:
|
|
x: int
|
|
|
|
inputs = {'x': '1'}
|
|
assert parse_obj_as(PydanticDataclass, inputs) == PydanticDataclass(1)
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_parse_mapping_as():
|
|
inputs = {'1': '2'}
|
|
assert parse_obj_as(Dict[int, int], inputs) == {1: 2}
|
|
|
|
|
|
@pytest.mark.xfail(reason='working on V2')
|
|
def test_schema():
|
|
assert schema_of(Union[int, str], title='IntOrStr') == {
|
|
'title': 'IntOrStr',
|
|
'anyOf': [{'type': 'integer'}, {'type': 'string'}],
|
|
}
|
|
assert schema_json_of(Union[int, str], title='IntOrStr', indent=2) == (
|
|
'{\n'
|
|
' "title": "IntOrStr",\n'
|
|
' "anyOf": [\n'
|
|
' {\n'
|
|
' "type": "integer"\n'
|
|
' },\n'
|
|
' {\n'
|
|
' "type": "string"\n'
|
|
' }\n'
|
|
' ]\n'
|
|
'}'
|
|
)
|