Commit 4607ec5b authored by mashony's avatar mashony
Browse files

fix: Allow drop field on import_data if value is None

parent 2272a26f
......@@ -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 Undefined:
if value is None or 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 Undefined:
if value is None or value is Undefined:
return value
return field.validate(value, context)
......
......@@ -223,6 +223,8 @@ class ListType(CompoundType):
data = []
errors = {}
for index, item in enumerate(value):
if self.field.is_compound and item is None:
raise ConversionError(_('Could not convert NoneType value'))
try:
data.append(context.field_converter(self.field, item, context))
except BaseError as exc:
......
......@@ -271,7 +271,7 @@ def test_none_types_in_list():
class Card(Model):
id = StringType(default='test')
users = ListType(ModelType(User))
with pytest.raises(DataError, match="Input must be a mapping or 'User' instance"):
with pytest.raises(DataError, match='Could not convert NoneType value'):
Card({'users': [None]})
card = Card({'users': [{'name': 'test'}]})
......
......@@ -26,6 +26,53 @@ def test_simple_embedded_models():
assert p.location.country_code == "IS"
def test_simple_embedded_models_with_none():
class Location(Model):
country_code = StringType(required=True)
class Player(Model):
id = IntType()
location = ModelType(Location, required=False)
p = Player(dict(id=1, location={"country_code": "US"}))
assert p.id == 1
assert p.location.country_code == "US"
p.import_data({'location': None})
assert not p.location
def test_simple_embedded_models_error_with_none():
class Location(Model):
country_code = StringType(required=True)
class Player(Model):
id = IntType()
location = ModelType(Location, required=False)
with pytest.raises(DataError):
Player(dict(id=1, location=None))
def test_simple_embedded_models_error_with_required_field():
class Location(Model):
country_code = StringType(required=True)
class Player(Model):
id = IntType()
location = ModelType(Location, required=True)
p = Player(dict(id=1, location={"country_code": "US"}))
assert p.id == 1
assert p.location.country_code == "US"
with pytest.raises(DataError):
p.import_data({'location': None})
def test_simple_embedded_models_is_none():
class Location(Model):
country_code = StringType()
......
......@@ -86,13 +86,13 @@ def test_validate_list_type():
def test_none_list_type():
with pytest.raises(ValueError, match='Value must be mapping, but found NoneType'):
with pytest.raises(DataError, match='Could not convert NoneType value'):
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"
error = 'Could not convert NoneType value'
with pytest.raises(DataError, match=error):
_obj.import_data({'poly': [None]})
......
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