Skip to content

Commit

Permalink
Merge pull request #126 from ResearchObject/root_extra_types
Browse files Browse the repository at this point in the history
Allow additional types for root dataset
  • Loading branch information
simleo committed May 18, 2022
2 parents 9c01065 + 5095288 commit 8e93cd1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions rocrate/rocrate.py
Expand Up @@ -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):
Expand Down
29 changes: 28 additions & 1 deletion test/test_read.py
Expand Up @@ -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)


Expand Down Expand Up @@ -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)

0 comments on commit 8e93cd1

Please sign in to comment.