From 50952889ae2c75130478724d5a1d6246533a415b Mon Sep 17 00:00:00 2001 From: simleo Date: Wed, 18 May 2022 14:55:29 +0200 Subject: [PATCH] allow additional types for root dataset --- rocrate/rocrate.py | 4 ++-- test/test_read.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py index 8ed15de..c0fa0b8 100644 --- a/rocrate/rocrate.py +++ b/rocrate/rocrate.py @@ -151,8 +151,8 @@ def __check_metadata(self, metadata, entities): root = entities[metadata["about"]["@id"]] except (KeyError, TypeError): raise ValueError("metadata descriptor does not reference the root entity") - if root["@type"] != "Dataset": - raise ValueError('root entity must be of type "Dataset"') + if ("Dataset" not in root["@type"] if isinstance(root["@type"], list) else root["@type"] != "Dataset"): + raise ValueError('root entity must have "Dataset" among its types') return metadata["@id"], root["@id"] def find_root_entity_id(self, entities): diff --git a/test/test_read.py b/test/test_read.py index e3f2820..6497a76 100644 --- a/test/test_read.py +++ b/test/test_read.py @@ -530,7 +530,7 @@ def test_find_root_bad_entities(): # root type is not Dataset entities = deepcopy(orig_entities) entities["./"]["@type"] = "Thing" - with pytest.raises(ValueError, match="must be of type"): + with pytest.raises(ValueError, match="must have"): crate.find_root_entity_id(entities) @@ -599,3 +599,30 @@ def check_picks_one(entities): mod_entities = deepcopy(orig_entities) mod_entities["http://example.com/"]["@type"] = "Thing" check_finds_org(mod_entities) + + +def test_find_root_multiple_types(): + entities = {_["@id"]: _ for _ in [ + { + "@id": "ro-crate-metadata.json", + "@type": "CreativeWork", + "about": {"@id": "./"}, + "conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"}, + }, + { + "@id": "./", + "@type": ["Dataset", "RepositoryCollection"], + }, + ]} + crate = ROCrate() + m_id, r_id = crate.find_root_entity_id(entities) + assert m_id == "ro-crate-metadata.json" + assert r_id == "./" + # "Dataset" not included + del entities["./"]["@type"][0] + with pytest.raises(ValueError): + crate.find_root_entity_id(entities) + # Check we're not trying to be too clever + entities["./"]["@type"] = "NotADataset" + with pytest.raises(ValueError): + crate.find_root_entity_id(entities)