From b54adf79da6b4bd845b2d88808f130faa62bff83 Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Thu, 30 Jan 2020 15:38:44 +0100
Subject: [PATCH] model: Update revision date types to be optional

Related to P589
---
 swh/model/model.py            | 18 +++++++++++++-----
 swh/model/tests/test_model.py | 16 ++++++++++++++++
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/swh/model/model.py b/swh/model/model.py
index ec644fa3..4a308a40 100644
--- a/swh/model/model.py
+++ b/swh/model/model.py
@@ -296,8 +296,8 @@ class Revision(BaseModel, HashableObject):
     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 @@ class Revision(BaseModel, HashableObject):
     @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
index a5719ca0..4acc3be2 100644
--- a/swh/model/tests/test_model.py
+++ b/swh/model/tests/test_model.py
@@ -88,6 +88,22 @@ def test_revision_model_id_computation():
     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']
-- 
GitLab