diff --git a/rocrate/rocrate.py b/rocrate/rocrate.py index 2944bd5..7e4487a 100644 --- a/rocrate/rocrate.py +++ b/rocrate/rocrate.py @@ -246,8 +246,8 @@ def __add_parts(self, parts, entities, source): if not is_data_entity(entities[id_]): continue entity = entities.pop(id_) - assert id_ == entity.pop('@id') cls = pick_type(entity, type_map, fallback=DataEntity, load_subcrates=self.load_subcrates) + assert id_ == entity.pop('@id') if cls is Subcrate: @@ -288,8 +288,8 @@ def __read_contextual_entities(self, entities): raise ValueError(f"'{id_}' is a data entity but it's not linked to from the root dataset's hasPart") else: warnings.warn(f"'{id_}' looks like a data entity but it's not listed in the root dataset's hasPart") - assert identifier == entity.pop('@id') cls = pick_type(entity, type_map, fallback=ContextEntity) + assert identifier == entity.pop('@id') self.add(cls(self, identifier, entity)) @property diff --git a/test/test_read.py b/test/test_read.py index 7cf04c1..ab56228 100644 --- a/test/test_read.py +++ b/test/test_read.py @@ -851,6 +851,36 @@ def test_read_version(test_data_dir): assert crate.version == "1.2-DRAFT" +@pytest.mark.parametrize(("entity_id", "has_part"), [ + ("missing-type.txt", True), + ("#missing-type", False), +]) +def test_entity_without_type_reports_its_id(entity_id, has_part): + root = { + "@id": "./", + "@type": "Dataset", + } + if has_part: + root["hasPart"] = {"@id": entity_id} + metadata = { + "@context": "https://w3id.org/ro/crate/1.1/context", + "@graph": [ + { + "@id": "ro-crate-metadata.json", + "@type": "CreativeWork", + "about": {"@id": "./"}, + "conformsTo": {"@id": "https://w3id.org/ro/crate/1.1"}, + }, + root, + {"@id": entity_id}, + ], + } + + with pytest.raises(ValueError) as exc_info: + ROCrate(metadata) + assert str(exc_info.value) == f"entity {entity_id!r} has no @type" + + @pytest.mark.filterwarnings("ignore") @pytest.mark.parametrize("version", ["1.0", "1.1", "1.2"]) def test_data_entity_not_linked(version):