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
|
from collections.abc import Mapping
from typing import Union
import pytest
from pydantic import BaseModel, PydanticDeprecatedSince20, ValidationError
from pydantic.dataclasses import dataclass
from pydantic.deprecated.tools import parse_obj_as, schema_json_of, schema_of
pytestmark = pytest.mark.filterwarnings('ignore::DeprecationWarning')
@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
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)
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]
def test_parse_obj_fails():
with pytest.raises(ValidationError) as exc_info:
parse_obj_as(int, 'a')
assert exc_info.value.errors(include_url=False) == [
{
'input': 'a',
'loc': (),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
}
]
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 int'
with pytest.raises(ValidationError) as exc_info:
with pytest.warns(PydanticDeprecatedSince20, match='The type_name parameter is deprecated'):
parse_obj_as(int, 'a', type_name='ParsingModel')
assert str(exc_info.value).split('\n')[0] == '1 validation error for int'
def test_parse_as_dataclass():
@dataclass
class PydanticDataclass:
x: int
inputs = {'x': '1'}
assert parse_obj_as(PydanticDataclass, inputs) == PydanticDataclass(1)
def test_parse_mapping_as():
inputs = {'1': '2'}
assert parse_obj_as(dict[int, int], inputs) == {1: 2}
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'
' "anyOf": [\n'
' {\n'
' "type": "integer"\n'
' },\n'
' {\n'
' "type": "string"\n'
' }\n'
' ],\n'
' "title": "IntOrStr"\n'
'}'
)
|