Skip to content

Commit

Permalink
raise exc if trying to set a dict with no id as prop value
Browse files Browse the repository at this point in the history
  • Loading branch information
simleo committed Sep 5, 2024
1 parent 1419ddc commit df66def
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rocrate/model/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def __setitem__(self, key: str, value):
if key.startswith("@"):
raise KeyError(f"cannot set '{key}'")
values = value if isinstance(value, list) else [value]
for v in values:
if isinstance(v, dict) and "@id" not in v:
raise ValueError(f"no @id in {v}")
ref_values = [{"@id": _.id} if isinstance(_, Entity) else _ for _ in values]
self._jsonld[key] = ref_values if isinstance(value, list) else ref_values[0]

Expand Down
6 changes: 6 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ def test_entity_as_mapping(tmpdir, helpers):
}
crate_dir = tmpdir / "in_crate"
crate_dir.mkdir()
with open(crate_dir / helpers.METADATA_FILE_NAME, "wt") as f:
json.dump(metadata, f, indent=4)
with pytest.raises(ValueError):
crate = ROCrate(crate_dir)
del metadata["@graph"][2]["badProp"]
with open(crate_dir / helpers.METADATA_FILE_NAME, "wt") as f:
json.dump(metadata, f, indent=4)
crate = ROCrate(crate_dir)
Expand Down Expand Up @@ -411,6 +416,7 @@ def test_entity_as_mapping(tmpdir, helpers):
"application/json",
"https://www.json.org",
}
correction._jsonld["badProp"] = {"k": "v"}
with pytest.raises(ValueError):
correction["badProp"]

Expand Down

0 comments on commit df66def

Please sign in to comment.