Skip to content

Commit

Permalink
dumpers: fix issue with none values in model
Browse files Browse the repository at this point in the history
  • Loading branch information
lnielsen committed Sep 10, 2020
1 parent e469618 commit 2211f2c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions invenio_records/dumpers/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def _serialize(value, dump_type):
:param dump_type: Data type use for serialization (supported: str, int,
bool, float, datetime, date, uuid).
"""
if value is None:
return value
if dump_type in (datetime, ):
return pytz.utc.localize(value).isoformat()
elif dump_type in (UUID, ):
Expand All @@ -110,6 +112,8 @@ def _deserialize(value, dump_type):
:param dump_type: Data type use for deserialization (supported: str,
int, bool, float, datetime, date, uuid).
"""
if value is None:
return value
if dump_type in (datetime, ):
return arrow.get(value).datetime.replace(tzinfo=None)
elif dump_type in (UUID, ):
Expand Down
13 changes: 9 additions & 4 deletions tests/test_field_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class Record1Metadata(db.Model, RecordMetadataBase):
__tablename__ = 'record1_metadata'
# __versioned__ = None

expires_at = db.Column(
db.DateTime(),
nullable=False
)
expires_at = db.Column(db.DateTime())

Record1Metadata.__table__.create(db.engine)

class Record1(Record, SystemFieldsMixin):
Expand Down Expand Up @@ -88,3 +86,10 @@ class Record1(Record, SystemFieldsMixin):
dump = record.dumps()
loaded_record = record.loads(dump)
assert loaded_record.expires_at == record.expires_at

# Test dumping with None
record = Record1.create({})
dump = record.dumps()
print(dump)
loaded_record = record.loads(dump)
assert loaded_record.expires_at is None

0 comments on commit 2211f2c

Please sign in to comment.