Files
pydantic/tests/test_construction.py
T
pyup.io bot 32bc0083d9 Scheduled monthly dependency update for July (#633)
* Update sphinx from 2.0.1 to 2.1.2

* Update typing-extensions from 3.7.2 to 3.7.4

* Update cython from 0.29.9 to 0.29.11

* Update isort from 4.3.20 to 4.3.21

* Update mypy from 0.701 to 0.711

* Update pytest from 4.6.0 to 5.0.0

* pytest and mypy fixes

* prevent deprication warnings
2019-07-06 09:39:39 +01:00

168 lines
3.7 KiB
Python

import pickle
import pytest
from pydantic import BaseModel
class Model(BaseModel):
a: float
b: int = 10
def test_simple_construct():
m = Model.construct(dict(a=40, b=10), {'a', 'b'})
assert m.a == 40
assert m.b == 10
def test_construct_missing():
m = Model.construct(dict(a='not a float'), {'a'})
assert m.a == 'not a float'
with pytest.raises(AttributeError) as exc_info:
print(m.b)
assert "'Model' object has no attribute 'b'" in exc_info.value.args[0]
def test_large_any_str():
class Model(BaseModel):
a: bytes
b: str
content_bytes = b'x' * (2 ** 16 + 1)
content_str = 'x' * (2 ** 16 + 1)
m = Model(a=content_bytes, b=content_str)
assert m.a == content_bytes
assert m.b == content_str
def test_simple_copy():
m = Model(a=24)
m2 = m.copy()
assert m.a == m2.a == 24
assert m.b == m2.b == 10
assert m == m2
assert m.__fields__ == m2.__fields__
class ModelTwo(BaseModel):
a: float
b: int = 10
c: str = 'foobar'
d: Model
def test_deep_copy():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy(deep=True)
assert m.a == m2.a == 24
assert m.b == m2.b == 10
assert m.c == m2.c == 'foobar'
assert m.d is not m2.d
assert m == m2
assert m.__fields__ == m2.__fields__
def test_copy_exclude():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy(exclude={'b'})
assert m.a == m2.a == 24
assert isinstance(m2.d, Model)
assert m2.d.a == 12
assert hasattr(m2, 'c')
assert not hasattr(m2, 'b')
assert set(m.dict().keys()) == {'a', 'b', 'c', 'd'}
assert set(m2.dict().keys()) == {'a', 'c', 'd'}
assert m != m2
def test_copy_include():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy(include={'a'})
assert m.a == m2.a == 24
assert set(m.dict().keys()) == {'a', 'b', 'c', 'd'}
assert set(m2.dict().keys()) == {'a'}
assert m != m2
def test_copy_include_exclude():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy(include={'a', 'b', 'c'}, exclude={'c'})
assert set(m.dict().keys()) == {'a', 'b', 'c', 'd'}
assert set(m2.dict().keys()) == {'a', 'b'}
def test_copy_update():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy(update={'a': 'different'})
assert m.a == 24
assert m2.a == 'different'
assert set(m.dict().keys()) == set(m2.dict().keys()) == {'a', 'b', 'c', 'd'}
assert m != m2
def test_copy_set_fields():
m = ModelTwo(a=24, d=Model(a='12'))
m2 = m.copy()
assert m.dict(skip_defaults=True) == {'a': 24.0, 'd': {'a': 12}}
assert m.dict(skip_defaults=True) == m2.dict(skip_defaults=True)
def test_simple_pickle():
m = Model(a='24')
b = pickle.dumps(m)
m2 = pickle.loads(b)
assert m.a == m2.a == 24
assert m.b == m2.b == 10
assert m == m2
assert m is not m2
assert tuple(m) == (('a', 24.0), ('b', 10))
assert tuple(m2) == (('a', 24.0), ('b', 10))
assert m.__fields__ == m2.__fields__
def test_recursive_pickle():
m = ModelTwo(a=24, d=Model(a='123.45'))
m2 = pickle.loads(pickle.dumps(m))
assert m == m2
assert m.d.a == 123.45
assert m2.d.a == 123.45
assert m.__fields__ == m2.__fields__
def test_immutable_copy():
class Model(BaseModel):
a: int
b: int
class Config:
allow_mutation = False
m = Model(a=40, b=10)
assert m == m.copy()
m2 = m.copy(update={'b': 12})
assert str(m2) == 'Model a=40 b=12'
with pytest.raises(TypeError):
m2.b = 13
def test_pickle_fields_set():
m = Model(a=24)
assert m.dict(skip_defaults=True) == {'a': 24}
m2 = pickle.loads(pickle.dumps(m))
assert m2.dict(skip_defaults=True) == {'a': 24}