Skip to content

Commit

Permalink
Remove redundant code and fix tests accordingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Argueta committed Mar 4, 2018
1 parent 868103f commit 28b7784
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 0 additions & 2 deletions binobj/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ def dump(self, stream, data=DEFAULT, context=None):
if data in (UNDEFINED, DEFAULT):
raise errors.MissingRequiredValueError(field=self)
elif data is None:
if not self.allow_null:
raise errors.UnserializableValueError(field=self, value=data)
data = self._get_null_value()

self._do_dump(stream, data, context)
Expand Down
13 changes: 8 additions & 5 deletions tests/fields_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
def test_load__null_with_null_value():
null_value = b' :( '
field = fields.Bytes(name='field', size=4, null_value=null_value)
assert field.allow_null is True
assert field.load(io.BytesIO(null_value)) is None


Expand All @@ -26,6 +27,7 @@ def test_loads__field_insufficient_data():
def test_dump__null_with_null_value():
"""Dumping None should use null_value"""
field = fields.Bytes(name='field', size=4, null_value=b' :( ')
assert field.allow_null is True
assert field.dumps(None) == b' :( '


Expand All @@ -35,11 +37,11 @@ def test_dump__null_with_default_null():
assert field.dumps(None) == b'\0\0\0\0'


def test_dump__null_with_no_def_and_varlen():
"""Crash if trying to write ``None`` when ``null_value`` is undefined and
column is variable-length."""
field = fields.VariableLengthInteger(name='field',
vli_format=varints.VarIntEncoding.ZIGZAG)
def test_dump__null_with_default_and_varlen():
"""Crash if trying to write ``None`` when using the default null_value and
column is of variable length."""
field = fields.StringZ(name='field', null_value=DEFAULT)
assert field.allow_null is True

with pytest.raises(errors.UnserializableValueError):
field.dumps(None)
Expand All @@ -48,6 +50,7 @@ def test_dump__null_with_no_def_and_varlen():
def test_dump__no_null_value_crashes():
"""Crash if we try dumping None with no null_value set."""
field = fields.Bytes(name='field', size=4)
assert not field.allow_null

with pytest.raises(errors.UnserializableValueError) as errinfo:
field.dumps(None)
Expand Down

0 comments on commit 28b7784

Please sign in to comment.