Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix entity getitem when value is None (JSON null) #102

Merged
merged 1 commit into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rocrate/model/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _empty(self):

def __getitem__(self, key):
v = self._jsonld[key]
if isinstance(v, str) or key.startswith("@"):
if v is None or isinstance(v, str) or key.startswith("@"):
return v
values = v if isinstance(v, list) else [v]
deref_values = [self.crate.dereference(_["@id"], _["@id"]) for _ in values]
Expand Down
23 changes: 16 additions & 7 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def test_entity_as_mapping(tmpdir, helpers):
"author": {"@id": orcid}},
{"@id": orcid,
"@type": "Person",
"name": None,
"givenName": "Josiah",
"familyName": "Carberry"}
]
Expand All @@ -305,18 +306,24 @@ def test_entity_as_mapping(tmpdir, helpers):
json.dump(metadata, f, indent=4)
crate = ROCrate(crate_dir)
person = crate.dereference(orcid)
assert len(person) == 4
assert len(list(person)) == 4
assert set(person) == set(person.keys()) == {"@id", "@type", "givenName", "familyName"}
assert set(person.values()) == {orcid, "Person", "Josiah", "Carberry"}
exp_len = len(metadata["@graph"][2])
assert len(person) == exp_len
assert len(list(person)) == exp_len
assert set(person) == set(person.keys()) == {"@id", "@type", "name", "givenName", "familyName"}
assert set(person.values()) == {orcid, "Person", None, "Josiah", "Carberry"}
assert set(person.items()) == set(zip(person.keys(), person.values()))
assert person.id == orcid
assert person.type == "Person"
assert person["name"] is None
assert person["givenName"] == "Josiah"
assert person["familyName"] == "Carberry"
assert person.setdefault("givenName", "foo") == "Josiah"
assert len(person) == 4
assert len(person) == exp_len
assert person.setdefault("award", "Oscar") == "Oscar"
assert len(person) == 5
assert len(person) == exp_len + 1
assert "award" in person
assert person.pop("award") == "Oscar"
assert len(person) == 4
assert len(person) == exp_len
assert "award" not in person
for key in "@id", "@type":
with pytest.raises(KeyError):
Expand All @@ -326,8 +333,10 @@ def test_entity_as_mapping(tmpdir, helpers):
with pytest.raises(KeyError):
person.pop(key)
twin = Person(crate, orcid, properties={
"name": None,
"givenName": "Josiah",
"familyName": "Carberry"
})
assert twin == person
assert Person(crate, orcid) != person
assert crate.root_dataset["author"] is person