diff --git a/swh/model/model.py b/swh/model/model.py --- a/swh/model/model.py +++ b/swh/model/model.py @@ -848,7 +848,10 @@ @classmethod def from_dict(cls, d): - d["type"] = MetadataAuthorityType(d["type"]) + d = { + **d, + "type": MetadataAuthorityType(d["type"]), + } return super().from_dict(d) def unique_key(self) -> KeyType: 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 @@ -45,6 +45,7 @@ Timestamp, TimestampWithTimezone, ) +from swh.model.tests.swh_model_data import TEST_OBJECTS from swh.model.tests.test_identifiers import ( content_example, directory_example, @@ -78,6 +79,17 @@ assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() +@pytest.mark.parametrize("object_type, objects", TEST_OBJECTS.items()) +def test_swh_model_todict_fromdict(object_type, objects): + """checks model objects in swh_model_data are in correct shape""" + assert objects + for obj in objects: + # Check the composition of from_dict and to_dict is the identity + obj_as_dict = obj.to_dict() + assert obj == type(obj).from_dict(obj_as_dict) + assert obj_as_dict == type(obj).from_dict(obj_as_dict).to_dict() + + def test_unique_key(): url = "http://example.org/" date = datetime.datetime.now(tz=datetime.timezone.utc)