def test_model_to_dict_nested_list(iterable, model, model2): model_class = type(model) other_model = model_class(foo='foo', bar=iterable([model, model2]), baz=model) other_model.validate() as_dict = to_dict(other_model) expected = { 'foo': 'foo', 'bar': [to_dict(model), to_dict(model2)], 'baz': to_dict(model), 'qux': None } assert as_dict == expected
def test_model_as_dict_validated(model): model.validate() d = model.as_dict() assert d assert isinstance(d, dict) assert d == to_dict(model)
def test_model_to_dict_simple(model): as_dict_model = { 'foo': 'foo', 'bar': 'bar', 'baz': '', 'qux': '', } model.validate() assert to_dict(model) == as_dict_model
def test_model_class_builder(): Birl = model_class_builder('Birl', {'f': 'foo', 'b': 'bar'}) birl = Birl() assert isinstance(birl, Model) keys = ('f', 'b') assert len(Birl._meta.fields) == len(keys) assert set(Birl._meta.fields) == set(keys) assert birl.validate(raise_exception=False) is True assert to_dict(birl) == {'f': None, 'b': None}
def test_model_to_dict_inheritance(model_data): class ChildModel(MyModel): pass model = ChildModel(**model_data) as_dict_model = { 'foo': 'foo', 'bar': 'bar', 'baz': '', 'qux': '', } model.validate() assert to_dict(model) == as_dict_model
def test_model_to_dict_property(): class Foo(Model): a: float b: float @property def b(self): return self.a + 0.1 a = 1.0 expected = { 'a': a, 'b': a + 0.1, } model = Foo(a=a) model.validate() assert to_dict(model) == expected
def test_model_to_dict_invalid_argument(): with pytest.raises(TypeError): to_dict('')
def test_model_to_dict_model_not_validated(model): with pytest.raises(AssertionError): to_dict(model)