diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -104,15 +104,22 @@ SWH loader.""" origin = attr.ib(type=Origin) date = attr.ib(type=datetime.datetime) + status = attr.ib( + type=str, + validator=attr.validators.in_(['ongoing', 'full', 'partial'])) + type = attr.ib(type=str) + snapshot = attr.ib(type=Sha1Git) + metadata = attr.ib(type=Optional[Dict[str, object]], + default=None) + visit = attr.ib(type=Optional[int], - validator=attr.validators.optional([])) + default=None) """Should not be set before calling 'origin_visit_add()'.""" def to_dict(self): """Serializes the date as a string and omits the visit id if it is `None`.""" ov = super().to_dict() - ov['date'] = str(self.date) if ov['visit'] is None: del ov['visit'] return ov @@ -120,9 +127,13 @@ @classmethod def from_dict(cls, d): """Parses the date from a string, and accepts missing visit ids.""" + d = d.copy() + date = d.pop('date') return cls( - origin=Origin.from_dict(d['origin']), - date=dateutil.parser.parse(d['date']), + origin=Origin.from_dict(d.pop('origin')), + date=(date + if isinstance(date, datetime.datetime) + else dateutil.parser.parse(date)), visit=d.get('visit')) diff --git a/swh/model/tests/test_hypothesis_strategies.py b/swh/model/tests/test_hypothesis_strategies.py --- a/swh/model/tests/test_hypothesis_strategies.py +++ b/swh/model/tests/test_hypothesis_strategies.py @@ -3,6 +3,8 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import datetime + import attr from hypothesis import given @@ -30,7 +32,8 @@ elif isinstance(obj, list): for value in obj: assert_nested_dict(value) - elif isinstance(obj, (int, float, str, bytes, bool, type(None))): + elif isinstance(obj, (int, float, str, bytes, bool, type(None), + datetime.datetime)): pass else: assert False, obj