Skip to content

Commit 287d37a

Browse files
author
Michael Fritzsche
committed
added tests and fixed small bug and fixed typos
1 parent bcc233d commit 287d37a

4 files changed

Lines changed: 96 additions & 26 deletions

File tree

src/hermes/model/types/ld_container.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def _to_expanded_json(
224224
:param value: The value that is to be expanded. Different types are expected based on the type of self:
225225
<ul><li>If type(self) == ld_dict: value must be a dict</li>
226226
<li>If type(self) == ld_list: value must be a list</li></ul>
227-
value will be exapnded as if it was the data_dict/ the item_list of self.
227+
value will be expanded as if it was the data_dict/ the item_list of self.
228228
:type value: JSON_LD_VALUE
229229
230230
:return: The expanded version of value i.e. the data_dict/ item_list of self if it had been value.
@@ -293,14 +293,8 @@ def _to_expanded_json(
293293
for index in range(len(path) - 1, 0, -1):
294294
current_data = current_data[path[index]]
295295
# replace the data_dict/ item_list so that value is now inside of the ld_value of parent and store the old value
296-
if current_data == []:
297-
# itemlist of an empty ld_list:
298-
# The item_list can't be replaced like in all other cases
299-
self_data = None
300-
current_data.append(value)
301-
else:
302-
self_data = current_data[path[0]]
303-
current_data[path[0]] = value
296+
self_data = current_data[path[0]]
297+
current_data[path[0]] = value
304298

305299
# expand the ld_value of parent to implicitly expand value
306300
# important the ld_value of parent is not modified because the processor makes a deep copy
@@ -310,10 +304,7 @@ def _to_expanded_json(
310304
)
311305

312306
# restore the data_dict/ item_list to its former state
313-
if self_data is not None:
314-
current_data[path[0]] = self_data
315-
else:
316-
current_data.clear()
307+
current_data[path[0]] = self_data
317308

318309
# use the path to get the expansion of value
319310
for index in range(len(path) - 1, -1, -1):

src/hermes/model/types/ld_list.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ def __init__(
5959
:return:
6060
:rtype: None
6161
62-
:raises ValueError: bla
63-
:raises ValueError: bla
62+
:raises ValueError: If the given key is not a string or None was given.
63+
:raises ValueError: If the given data is not a list.
64+
:raises ValueError: If the data represents an unexpanded @set. I.e. is of the form [{"@set": [...]}]
65+
:raises ValueError: If the given key is "@type" but the container_type not "@set"
66+
or a value in the item_list not a string.
67+
:raises ValueError: If the given key is not "@type" and any value in the item_list not a dict.
6468
"""
6569
# check for validity of data
6670
if not isinstance(key, str):
@@ -272,8 +276,16 @@ def __eq__(
272276
other = [other]
273277
if isinstance(other, list):
274278
if ld_list.is_ld_list(other):
275-
other = ld_list.get_item_list_from_container(other[0])
276-
other = self.from_list(other, parent=self.parent, key=self.key, context=self.context)
279+
if "@list" in other[0]:
280+
cont = "@list"
281+
elif "@graph" in other[0]:
282+
cont = "@graph"
283+
else:
284+
cont = "@set"
285+
other = other[0][cont]
286+
else:
287+
cont = "@set"
288+
other = self.from_list(other, parent=self.parent, key=self.key, context=self.context, container_type=cont)
277289

278290
# check if the length matches
279291
if len(self.item_list) != len(other.item_list):
@@ -338,7 +350,7 @@ def __eq__(
338350
# swap order if first try returned NotImplemented
339351
res = other_item.__eq__(item)
340352
# if one of both comparisons returned true the elements are equal
341-
if res:
353+
if res is not NotImplemented and res:
342354
equality_pairs[index] += [other_index]
343355
if len(equality_pairs[index]) == 0:
344356
# there exists no element in other that is equal to item
@@ -624,7 +636,7 @@ def from_list(
624636
:type value: list[JSON_LD_VALUE | BASIC_TYPE | TIME_TYPE]
625637
:param parent: The parent container of the new ld_list.<br>If value is assimilated by parent druing JSON-LD
626638
expansion parent is extended by value and parent is returned.
627-
:type parent: ls_container | None
639+
:type parent: ld_container | None
628640
:param key: The key into the inner most parent container representing a dict of the new ld_list.
629641
:type: key: str | None
630642
:param context: The context for the new list (is will also inherit the context of parent).<br>

test/hermes_test/model/types/test_ld_container.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# SPDX-FileContributor: Michael Meinel
77
# SPDX-FileContributor: Michael Fritzsche
88

9-
from datetime import datetime
9+
from datetime import datetime, time
1010

1111
import pytest
1212

@@ -121,6 +121,11 @@ def test_to_python_datetime_value(self, mock_context):
121121
"@value": "2022-02-22T00:00:00", "@type": "https://schema.org/DateTime"
122122
}) == "2022-02-22T00:00:00" # TODO: #434 typed date is returned as string instead of date
123123

124+
def test_to_python_error(self, mock_context):
125+
cont = ld_container([{}], context=[mock_context])
126+
with pytest.raises(TypeError):
127+
cont._to_python("http://spam.eggs/eggs", set())
128+
124129
def test_to_expanded_id(self, mock_context):
125130
cont = ld_dict([{}], context=[mock_context])
126131
assert cont._to_expanded_json({"@id": f"{self.url}identifier"}) == {"@id": f"{self.url}identifier"}
@@ -156,6 +161,35 @@ def test_to_expanded_datetime_value(self, mock_context):
156161
assert cont._to_expanded_json({"eggs": datetime(2022, 2, 22)}) == {"http://spam.eggs/eggs": [{"@list": [
157162
{"@value": "2022-02-22T00:00:00", "@type": "https://schema.org/DateTime"}
158163
]}]}
164+
cont = ld_dict([{}], context=[mock_context])
165+
assert cont._to_expanded_json({"eggs": time(5, 4, 3)}) == {"http://spam.eggs/eggs": [{"@list": [
166+
{"@value": "05:04:03", "@type": "https://schema.org/Time"}
167+
]}]}
168+
169+
def test_compact(self, mock_context):
170+
cont = ld_container([{"http://spam.eggs/eggs": [{"@list": [{"@value": "a"}]}],
171+
"http://spam.eggs/spam": [{"@value": "bacon"}]}])
172+
assert cont.compact([mock_context]) == {"@context": mock_context, "spam": "bacon", "eggs": ["a"]}
173+
174+
def test_is_ld_id(self):
175+
assert ld_container.is_ld_id([{"@id": "foo"}])
176+
assert not ld_container.is_ld_id([{"@id": "foo", "bar": "barfoo"}])
177+
assert not ld_container.is_ld_id({"@id": "foo"})
178+
assert not ld_container.is_ld_id([{"bar": "foo"}])
179+
180+
def test_is_ld_value(self):
181+
assert ld_container.is_ld_value([{"@value": "foo"}])
182+
assert ld_container.is_ld_value([{"@value": "foo", "bar": "barfoo"}])
183+
assert not ld_container.is_ld_value({"@value": "foo"})
184+
assert not ld_container.is_ld_value([{"bar": "foo"}])
185+
186+
def test_is_typed_ld_value(self):
187+
assert ld_container.is_typed_ld_value([{"@value": "foo", "@type": "bar"}])
188+
assert ld_container.is_typed_ld_value([{"@value": "foo", "@type": "bar", "bar": "barfoo"}])
189+
assert not ld_container.is_typed_ld_value([{"@type": "bar"}])
190+
assert not ld_container.is_typed_ld_value([{"@value": "foo"}])
191+
assert not ld_container.is_typed_ld_value({"@value": "foo", "@type": "bar"})
192+
assert not ld_container.is_typed_ld_value([{"bar": "foo"}])
159193

160194
def test_are_values_equal(self):
161195
assert ld_container.are_values_equal({"@id": "foo"}, {"@id": "foo"})

test/hermes_test/model/types/test_ld_list.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@
1414

1515
def test_undefined_list():
1616
with pytest.raises(ValueError):
17-
ld_list([{}])
17+
ld_list({}, key="foo")
1818
with pytest.raises(ValueError):
19-
ld_list([{"spam": [{"@value": "bacon"}]}])
19+
ld_list([{"@set": [{"@value": "bacon"}]}], key="foo")
2020
with pytest.raises(ValueError):
21-
ld_list([{"@list": [0], "spam": [{"@value": "bacon"}]}])
21+
ld_list([{"@value": "bacon"}], key="@type")
2222
with pytest.raises(ValueError):
23-
ld_list([{"@list": ["a", "b"], "@set": ["foo", "bar"]}])
23+
ld_list(["bacon"], key="eggs")
2424
with pytest.raises(ValueError):
2525
ld_list([{"@list": ["a", "b"]}]) # no given key
26-
with pytest.raises(ValueError):
27-
ld_list([{"@list": ["a", "b"]}, {"@set": ["foo", "bar"]}])
2826

2927

3028
def test_list_basics():
3129
li_data = [{"@list": [{"@value": "bar"}]}]
3230
li = ld_list(li_data, key="foo")
3331
assert li._data is li_data
3432
assert li.item_list is li_data[0]["@list"]
33+
li_data = [{"@graph": [{"@value": "bar"}]}]
34+
li = ld_list(li_data, key="foo")
35+
assert li._data is li_data
36+
assert li.item_list is li_data[0]["@graph"]
37+
li_data = [{"@value": "bar"}]
38+
li = ld_list(li_data, key="foo")
39+
assert li._data is li_data
40+
assert li.item_list is li_data
41+
assert li.container_type == "@set"
3542

3643

3744
def test_build_in_get():
@@ -135,6 +142,12 @@ def test_build_in_iter():
135142

136143

137144
def test_append():
145+
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
146+
li.append(ld_list([{"@value": "foo"}], key="https://schema.org/name"))
147+
assert isinstance(li[0], ld_list) and li[0].container_type == "@list"
148+
li = ld_list([{"@graph": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
149+
li.append({"schema:name": "foo"})
150+
assert li[0] == {"https://schema.org/name": "foo"} and len(li) == 1
138151
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
139152
li.append("foo")
140153
assert li[0] == "foo" and li.item_list[0] == {"@value": "foo"} and len(li) == 1
@@ -153,6 +166,7 @@ def test_append():
153166

154167
def test_build_in_contains():
155168
li = ld_list([], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
169+
assert [] in li
156170
li.append("foo")
157171
li.append({"@type": "A", "schema:name": "a"})
158172
assert "foo" in li and {"@type": "A", "schema:name": "a"} in li
@@ -162,9 +176,18 @@ def test_build_in_contains():
162176
li.append({"@id": "schema:foo", "schema:name": "foo"})
163177
assert {"@id": "schema:foo"} in li and {"@id": "schema:foo", "schema:name": "foobar"} in li
164178
assert {"schema:name": "foo"} in li
179+
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
180+
li.append("foo")
181+
assert "foo" in li
165182

166183

167184
def test_build_in_comparison():
185+
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
186+
li.append({"@id": "foo", "schema:bar": "foobar"})
187+
assert [{"@list": [{"@id": "foo", "schema:bar": "barfoo"}]}] == li
188+
assert [{"@list": [{"@id": "bar", "schema:bar": "foobar"}]}] != li
189+
assert [{"@set": [{"@id": "foo", "schema:bar": "barfoo"}]}] == li
190+
assert [{"@graph": [{"@id": "foo", "schema:bar": "barfoo"}]}] == li
168191
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
169192
li2 = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema2": "https://schema.org/"}])
170193
assert li == [] and [] == li
@@ -269,6 +292,10 @@ def test_is_container():
269292

270293

271294
def test_from_list():
295+
with pytest.raises(ValueError):
296+
ld_list.from_list([], key="@type", container_type="@list")
297+
with pytest.raises(ValueError):
298+
ld_list.from_list([], container_type="foo")
272299
li = ld_list.from_list([], key="schema:foo")
273300
assert li.item_list == li.context == [] and li.parent is li.index is None and li.key == "schema:foo"
274301
assert li._data == [] and li.container_type == "@set"
@@ -290,3 +317,9 @@ def test_get_item_list_from_container():
290317
assert ld_list.get_item_list_from_container({"@graph": ["a"]}) == ["a"]
291318
with pytest.raises(ValueError):
292319
ld_list.get_item_list_from_container(["a"])
320+
with pytest.raises(ValueError):
321+
ld_list.get_item_list_from_container({"@list": [], "@set": []})
322+
with pytest.raises(ValueError):
323+
ld_list.get_item_list_from_container({"@list": {}})
324+
with pytest.raises(ValueError):
325+
ld_list.get_item_list_from_container({"foo": []})

0 commit comments

Comments
 (0)