Commit b013f881 authored by Andrey Veltischev's avatar Andrey Veltischev
Browse files

Merge branch 'andery/none_type' into 'master'

Update validation of None types in arrays

See merge request !45
parents cef60194 a0da485c
......@@ -49,6 +49,8 @@ def atoms(schema, mapping, keys=tuple(Atom._fields), filter=None):
:rtype: Iterable[Atom]
"""
if mapping is None:
mapping = {}
for name, field in iteritems(schema.fields):
value = mapping.get(name, Undefined)
atom_tuple = Atom(name=name, field=field, value=value)
......
......@@ -78,7 +78,9 @@ class ModelMeta(type):
class Model(metaclass=ModelMeta):
def __init__(self, data={}, **kwargs):
def __init__(self, data=None, **kwargs):
if data is None:
data = {}
self.__data = {}
self._errors = {}
context = kwargs.get('context')
......
......@@ -411,7 +411,7 @@ def to_primitive_converter(field, value, context):
@BasicConverter
def import_converter(field, value, context):
field.check_required(value, context)
if value is None or value is Undefined:
if value is Undefined:
return value
return field.convert(value, context)
......@@ -419,7 +419,7 @@ def import_converter(field, value, context):
@BasicConverter
def validation_converter(field, value, context):
field.check_required(value, context)
if value is None or value is Undefined:
if value is Undefined:
return value
return field.validate(value, context)
......
......@@ -381,9 +381,6 @@ class PolyModelType(CompoundType):
def _convert(self, value, context):
if value is None:
return None
if not context.validate:
if self.is_allowed_model(value):
return value
......
......@@ -262,3 +262,18 @@ def test_issue_453_list_model_field_recursive_import():
card.import_data({'id': '2', 'users': [User({'name': 'xyz'})]},
recursive=True)
assert card.serialize() == {'id': 2, 'users': [{'name': 'xyz'}]}
def test_none_types_in_list():
class User(Model):
name = StringType()
class Card(Model):
id = StringType(default='test')
users = ListType(ModelType(User))
with pytest.raises(DataError, match="Input must be a mapping or 'User' instance"):
Card({'users': [None]})
card = Card({'users': [{'name': 'test'}]})
assert card.users[0].name == 'test'
......@@ -42,6 +42,10 @@ class Base(Model):
poly = types.PolyModelType([RealEstate, Vehicle, Props], claim_function=claim_func)
class ListBase(Model):
poly = types.ListType(types.PolyModelType([RealEstate, Vehicle, Props], claim_function=claim_func))
def test_create_real_estate():
with pytest.raises(DataError):
Base({'poly': {'item_type': 'realState', 'p_prop': 'failed_test'}})
......@@ -70,23 +74,29 @@ def test_type_validation():
def test_list_type():
class ListBase(Model):
poly = types.ListType(types.PolyModelType([RealEstate, Vehicle, Props], claim_function=claim_func))
with pytest.raises(DataError, match='{"poly": {"0": {"v_prop": "Unknown field"}}}'):
ListBase({'poly': [{'item_type': 'prop', 'v_prop': 'suc_test'}]})
ListBase({'poly': [{'item_type': 'prop', 'p_prop': 'suc_test'}]})
def test_validate_list_type():
class ListBase(Model):
poly = types.ListType(types.PolyModelType([RealEstate, Vehicle, Props], claim_function=claim_func))
with pytest.raises(DataError, match='{"poly": {"1": {"r_prop": "Unknown field"}}}'):
ListBase({'poly': [{'item_type': 'prop', 'p_prop': 'suc_test'}, {'item_type': 'vehicle', 'r_prop': 'suc_test'}]})
ListBase({'poly': [{'item_type': 'prop', 'p_prop': 'suc_test'}, {'item_type': 'vehicle', 'v_prop': 'suc_test'}]})
def test_none_list_type():
with pytest.raises(ValueError, match='Value must be mapping, but found NoneType'):
ListBase({'poly': [None]})
def test_update_with_none_list_type():
_obj = ListBase({'poly': [{'item_type': 'prop', 'p_prop': 'suc_test'}]})
error = "Please use a mapping for this field or an instance of one of: RealEstate, Vehicle, Props"
with pytest.raises(DataError, match=error):
_obj.import_data({'poly': [None]})
def test_validation():
class PropTest(Model):
item_type = types.StringType(choices=['propTest'], required=True)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment