mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
96e3e74262
fix #378 * Tracking for setting attributes * Fixes accidental leak of fields * Allows defaults fields to be recursively set * Docs and history for skip_defaults * Mypy fix on calculate keys * Update pydantic/main.py Co-Authored-By: dgasmith <dgasmith@icloud.com> * Update pydantic/main.py Co-Authored-By: dgasmith <dgasmith@icloud.com> * Update HISTORY.rst Co-Authored-By: dgasmith <dgasmith@icloud.com> * Cleanup pass based off review * Simplifies constructors based on feedback * Makes mypy happy with exlicit KeysView * SetOrKeys and faster key search * Formats files once more * add tests for dict, pickle and construct * fixes for dict, pickle and construct * correct field_set for extra.ignore * Fixes format
86 lines
2.4 KiB
Python
86 lines
2.4 KiB
Python
import pickle
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, Protocol, ValidationError
|
|
|
|
|
|
class Model(BaseModel):
|
|
a: float = ...
|
|
b: int = 10
|
|
|
|
|
|
def test_obj():
|
|
m = Model.parse_obj(dict(a=10.2))
|
|
assert str(m) == 'Model a=10.2 b=10'
|
|
|
|
|
|
def test_fails():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
Model.parse_obj([1, 2, 3])
|
|
assert exc_info.value.errors() == [
|
|
{'loc': ('__obj__',), 'msg': 'Model expected dict not list', 'type': 'type_error'}
|
|
]
|
|
|
|
|
|
def test_json():
|
|
assert Model.parse_raw('{"a": 12, "b": 8}') == Model(a=12, b=8)
|
|
|
|
|
|
def test_json_ct():
|
|
assert Model.parse_raw('{"a": 12, "b": 8}', content_type='application/json') == Model(a=12, b=8)
|
|
|
|
|
|
def test_pickle_ct():
|
|
data = pickle.dumps(dict(a=12, b=8))
|
|
assert Model.parse_raw(data, content_type='application/pickle', allow_pickle=True) == Model(a=12, b=8)
|
|
|
|
|
|
def test_pickle_proto():
|
|
data = pickle.dumps(dict(a=12, b=8))
|
|
assert Model.parse_raw(data, proto=Protocol.pickle, allow_pickle=True) == Model(a=12, b=8)
|
|
|
|
|
|
def test_pickle_not_allowed():
|
|
data = pickle.dumps(dict(a=12, b=8))
|
|
with pytest.raises(RuntimeError):
|
|
Model.parse_raw(data, proto=Protocol.pickle)
|
|
|
|
|
|
def test_bad_ct():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
Model.parse_raw('{"a": 12, "b": 8}', content_type='application/missing')
|
|
assert exc_info.value.errors() == [
|
|
{'loc': ('__obj__',), 'msg': 'Unknown content-type: application/missing', 'type': 'type_error'}
|
|
]
|
|
|
|
|
|
def test_bad_proto():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
Model.parse_raw('{"a": 12, "b": 8}', proto='foobar')
|
|
assert exc_info.value.errors() == [{'loc': ('__obj__',), 'msg': 'Unknown protocol: foobar', 'type': 'type_error'}]
|
|
|
|
|
|
def test_file_json(tmpdir):
|
|
p = tmpdir.join('test.json')
|
|
p.write('{"a": 12, "b": 8}')
|
|
assert Model.parse_file(str(p)) == Model(a=12, b=8)
|
|
|
|
|
|
def test_file_json_no_ext(tmpdir):
|
|
p = tmpdir.join('test')
|
|
p.write('{"a": 12, "b": 8}')
|
|
assert Model.parse_file(str(p)) == Model(a=12, b=8)
|
|
|
|
|
|
def test_file_pickle(tmpdir):
|
|
p = tmpdir.join('test.pkl')
|
|
p.write_binary(pickle.dumps(dict(a=12, b=8)))
|
|
assert Model.parse_file(str(p), allow_pickle=True) == Model(a=12, b=8)
|
|
|
|
|
|
def test_file_pickle_no_ext(tmpdir):
|
|
p = tmpdir.join('test')
|
|
p.write_binary(pickle.dumps(dict(a=12, b=8)))
|
|
assert Model.parse_file(str(p), content_type='application/pickle', allow_pickle=True) == Model(a=12, b=8)
|