diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -296,8 +296,8 @@ message = attr.ib(type=bytes) author = attr.ib(type=Person) committer = attr.ib(type=Person) - date = attr.ib(type=TimestampWithTimezone) - committer_date = attr.ib(type=TimestampWithTimezone) + date = attr.ib(type=Optional[TimestampWithTimezone]) + committer_date = attr.ib(type=Optional[TimestampWithTimezone]) type = attr.ib(type=RevisionType) directory = attr.ib(type=Sha1Git) synthetic = attr.ib(type=bool) @@ -314,12 +314,20 @@ @classmethod def from_dict(cls, d): d = d.copy() + date = d.pop('date') + if date: + date = TimestampWithTimezone.from_dict(date) + + committer_date = d.pop('committer_date') + if committer_date: + committer_date = TimestampWithTimezone.from_dict( + committer_date) + return cls( author=Person.from_dict(d.pop('author')), committer=Person.from_dict(d.pop('committer')), - date=TimestampWithTimezone.from_dict(d.pop('date')), - committer_date=TimestampWithTimezone.from_dict( - d.pop('committer_date')), + date=date, + committer_date=committer_date, type=RevisionType(d.pop('type')), **d) diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -88,6 +88,22 @@ assert rev_model.id == hash_to_bytes(revision_identifier(rev_dict)) +def test_revision_model_id_computation_with_no_date(): + """We can have revision with date to None + + """ + rev_dict = dict(revision_example) + rev_dict['date'] = None + rev_dict['committer_date'] = None + del rev_dict['id'] + + rev_id = hash_to_bytes(revision_identifier(rev_dict)) + for rev_model in [Revision(**rev_dict), Revision.from_dict(rev_dict)]: + assert rev_model.date is None + assert rev_model.committer_date is None + assert rev_model.id == rev_id + + def test_release_model_id_computation(): rel_dict = dict(release_example) del rel_dict['id']