Skip to content

Commit 6f039e8

Browse files
author
notactuallyfinn
committed
slightly adjusted tests and fixed miniature bugs in ld_container and ld_dict
1 parent 7adb02f commit 6f039e8

File tree

4 files changed

+53
-42
lines changed

4 files changed

+53
-42
lines changed

src/hermes/model/types/ld_container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def _to_expanded_json(
237237
# while searching build a path such that it leads from the found ld_dicts ld_value to selfs data_dict/ item_list
238238
parent = self
239239
path = []
240-
while parent.__class__.__name__ != "ld_dict":
240+
while parent.__class__.__name__ not in {"ld_dict", "SoftwareMetadata"}:
241241
if parent.container_type == "@list":
242242
path.extend(["@list", 0])
243243
elif parent.container_type == "@graph":
@@ -250,7 +250,7 @@ def _to_expanded_json(
250250
# if neither self nor any of its parents is a ld_dict:
251251
# create a dict with the key of the outer most parent of self and this parents ld_value as a value
252252
# this dict is stored in an ld_container and simulates the most minimal JSON-LD object possible
253-
if parent.__class__.__name__ != "ld_dict":
253+
if parent.__class__.__name__ not in {"ld_dict", "SoftwareMetadata"}:
254254
key = self.ld_proc.expand_iri(parent.active_ctx, parent.key)
255255
parent = ld_container([{key: parent._data}])
256256
path.append(0)

src/hermes/model/types/ld_dict.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ def __init__(self, data, *, parent=None, key=None, index=None, context=None):
2222

2323
def __getitem__(self, key):
2424
full_iri = self.ld_proc.expand_iri(self.active_ctx, key)
25-
try:
26-
ld_value = self.data_dict[full_iri]
27-
except KeyError:
28-
self.data_dict.update({full_iri: [{"@list": []}]})
29-
ld_value = self.data_dict[full_iri]
25+
if full_iri not in self.data_dict:
26+
self[full_iri] = []
27+
ld_value = self.data_dict[full_iri]
3028
return self._to_python(full_iri, ld_value)
3129

3230
def __setitem__(self, key, value):
3331
full_iri = self.ld_proc.expand_iri(self.active_ctx, key)
32+
if value is None:
33+
del self[full_iri]
34+
return
3435
ld_value = self._to_expanded_json({full_iri: value})
3536
self.data_dict.update(ld_value)
3637

@@ -40,7 +41,8 @@ def __delitem__(self, key):
4041

4142
def __contains__(self, key):
4243
full_iri = self.ld_proc.expand_iri(self.active_ctx, key)
43-
return len(self[full_iri]) != 0
44+
# FIXME: is that good?
45+
return full_iri in self.data_dict
4446

4547
def __eq__(self, other):
4648
if not isinstance(other, (dict, ld_dict)):
@@ -79,9 +81,12 @@ def __ne__(self, other):
7981
return not x
8082

8183
def get(self, key, default=_NO_DEFAULT):
82-
if key not in self and default is not ld_dict._NO_DEFAULT:
84+
try:
85+
return self[key]
86+
except KeyError as e:
87+
if default is self._NO_DEFAULT:
88+
raise e
8389
return default
84-
return self[key]
8590

8691
def update(self, other):
8792
for key, value in other.items():

test/hermes_test/model/test_api.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,32 @@ def test_init_nested_object():
4444
"author": [{"name": "Foo"}, {"name": "Bar"}],
4545
}
4646
data = SoftwareMetadata(my_software, extra_vocabs={"foo": "https://foo.bar"})
47-
assert data["schema:softwareName"][0] == "MySoftware"
48-
assert data["maintainer"][0]["name"][0] == "Some Name"
47+
assert data["schema:softwareName"] == ["MySoftware"]
48+
assert len(data["maintainer"]) == 1 and data["maintainer"][0]["name"] == ["Some Name"]
4949
for author in data["author"]:
5050
for name in author["name"]:
5151
assert name in ["Foo", "Bar"]
5252

5353

5454
def test_append():
5555
data = SoftwareMetadata()
56-
data["foo"].append("a")
57-
assert type(data["foo"]) is ld_list and data["foo"][0] == "a" and data["foo"].item_list == [{"@value": "a"}]
58-
data["foo"].append("b")
59-
assert type(data["foo"]) is ld_list and data["foo"].item_list == [{"@value": "a"}, {"@value": "b"}]
60-
data["foo"].append("c")
61-
assert data["foo"].item_list == [{"@value": "a"}, {"@value": "b"}, {"@value": "c"}]
56+
data["schema:foo"].append("a")
57+
assert type(data["schema:foo"]) is ld_list
58+
assert data["schema:foo"][0] == "a" and data["schema:foo"].item_list == [{"@value": "a"}]
59+
data["schema:foo"].append("b")
60+
assert type(data["schema:foo"]) is ld_list
61+
assert data["schema:foo"] == [{"@value": "a"}, {"@value": "b"}]
62+
data["schema:foo"].append("c")
63+
assert data["schema:foo"] == [{"@value": "a"}, {"@value": "b"}, {"@value": "c"}]
6264
data = SoftwareMetadata()
63-
data["foo"].append({"schema:name": "foo"})
64-
assert type(data["foo"]) is ld_list and type(data["foo"][0]) is ld_dict
65-
assert data["foo"][0].data_dict == {"http://schema.org/name": [{"@value": "foo"}]}
66-
data["foo"].append({"schema:name": "foo"})
67-
assert type(data["foo"]) is ld_list and data["foo"].item_list == 2*[{"http://schema.org/name": [{"@value": "foo"}]}]
68-
data["foo"].append({"schema:name": "foo"})
69-
assert data["foo"].item_list == 3 * [{"http://schema.org/name": [{"@value": "foo"}]}]
65+
data["schema:foo"].append({"schema:name": "bar"})
66+
assert type(data["schema:foo"]) is ld_list and type(data["schema:foo"][0]) is ld_dict
67+
assert data["schema:foo"] == [{"http://schema.org/name": [{"@value": "bar"}]}]
68+
data["schema:foo"].append({"schema:name": "bar"})
69+
assert type(data["schema:foo"]) is ld_list
70+
assert data["schema:foo"] == 2 * [{"http://schema.org/name": [{"@value": "bar"}]}]
71+
data["schema:foo"].append({"schema:name": "bar"})
72+
assert data["schema:foo"] == 3 * [{"http://schema.org/name": [{"@value": "bar"}]}]
7073

7174

7275
def test_iterative_assignment():
@@ -78,9 +81,10 @@ def test_iterative_assignment():
7881
assert isinstance(authors, ld_list)
7982
author1 = authors[0]
8083
author1["email"] = "[email protected]"
81-
authors[0] = author1
8284
authors.append({"name": "Bar", "email": "[email protected]"})
8385
assert len(authors) == 2
86+
del authors[0]
87+
assert len(authors) == 1
8488

8589

8690
def test_usage():
@@ -95,38 +99,38 @@ def test_usage():
9599
harvest = {
96100
"authors": [
97101
{"name": "Foo", "affiliation": ["Uni A", "Lab B"], "kw": ["a", "b", "c"]},
98-
{"name": "Bar", "affiliation": ["Uni C"], "email": "[email protected]"},
102+
{"name": "Bar", "affiliation": ["Uni C"], "email": "[email protected]", "kw": "egg"},
99103
{"name": "Baz", "affiliation": ["Lab E"]},
100104
]
101105
}
102106
for author in harvest["authors"]:
103107
for exist_author in data["author"]:
104-
if author["name"] == exist_author["name"][0]:
105-
exist_author["affiliation"] = author["affiliation"]
106-
if "email" in author:
107-
exist_author["email"].append(author["email"])
108-
if "kw" in author:
109-
exist_author["schema:knowsAbout"].extend(author["kw"])
108+
if author["name"] in exist_author["name"]:
109+
exist_author["affiliation"] = author.get("affiliation", [])
110+
exist_author["email"].extend(email if isinstance((email := author.get("email", [])), list) else [email])
111+
exist_author["schema:knowsAbout"].extend(kw if isinstance((kw := author.get("kw", [])), list) else [kw])
110112
break
111113
else:
112114
data["author"].append(author)
113115
assert len(data["author"]) == 3
114116
foo, bar, baz = data["author"]
115117
assert foo["name"][0] == "Foo"
116-
assert foo["affiliation"].to_python() == ["Uni A", "Lab B"]
117-
assert foo["schema:knowsAbout"].to_python() == ["a", "b", "c"]
118-
assert foo["email"].to_python() == ["[email protected]", "[email protected]"]
118+
assert foo["affiliation"] == ["Uni A", "Lab B"]
119+
assert foo["schema:knowsAbout"] == ["a", "b", "c"]
120+
assert foo["email"] == ["[email protected]", "[email protected]"]
119121
assert bar["name"][0] == "Bar"
120-
assert bar["affiliation"].to_python() == ["Uni C"]
121-
assert bar["email"].to_python() == ["[email protected]"]
122+
assert bar["affiliation"] == ["Uni C"]
123+
assert bar["email"] == ["[email protected]"]
122124
assert baz["name"][0] == "Baz"
123-
assert baz["affiliation"].to_python() == ["Lab E"]
125+
assert baz["affiliation"] == ["Lab E"]
124126
assert len(baz["schema:knowsAbout"]) == 0
125127
assert len(baz["email"]) == 0
126128
for author in data["author"]:
127129
assert "name" in author
128130
assert "email" in author
129-
if "schema:knowsAbout" not in author:
131+
if author["schema:knowsAbout"] == ["egg"]:
130132
# FIXME: None has to be discussed
133+
# json-ld processor just removes it in expansion
131134
author["schema:knowsAbout"] = None
132135
author["schema:pronouns"] = "they/them"
136+
assert len(bar["schema:knowsAbout"]) == 0

test/hermes_test/model/types/test_ld_dict.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,10 @@ def test_get():
189189
context=[{"schema": "https://schema.org/"}])
190190
assert di.get("https://schema.org/name") == ["Manu Sporny"]
191191
assert di.get("schema:name") == ["Manu Sporny"]
192-
assert di.get("bar", None) is None
193-
assert isinstance(di["bar"], ld_list) and len(di["bar"]) == 0
192+
assert di.get("bar", None) is None # invalid key
193+
with pytest.raises(KeyError):
194+
di.get("bar")
195+
assert isinstance(di.get("schema:bar", None), ld_list) and len(di.get("schema:bar", None)) == 0
194196

195197

196198
def test_update():

0 commit comments

Comments
 (0)