Skip to content

Commit 3bceb49

Browse files
committed
Restructured tests for ld_container (type conversions).
1 parent 4b8c3b7 commit 3bceb49

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

test/hermes_test/model/types/test_ld_container.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
# SPDX-FileContributor: Sophie Kernchen
6+
# SPDX-FileContributor: Michael Meinel
7+
8+
from datetime import datetime
69

710
import pytest
11+
812
from hermes.model.types.ld_container import ld_container
913

1014
'''we expect user of this class to give the right input data types
@@ -82,47 +86,68 @@ def test_container_str_and_repr(self):
8286
with pytest.raises(NotImplementedError):
8387
str(cont)
8488

85-
def test_to_python(self, mock_context):
86-
# Create container with mock context
89+
def test_to_python_id(self, mock_context):
8790
cont = ld_container([{}], context=[mock_context])
91+
assert cont._to_python("@id", "http://spam.eggs/ham") == "http://spam.eggs/ham"
8892

89-
# Try simple cases of conversion
90-
assert cont._to_python("@id", "ham") == "ham"
93+
def test_to_python_id_with_prefix(self, mock_context):
94+
cont = ld_container([{}], context=[mock_context, {"prefix": self.url}])
95+
assert cont._to_python("@id", f"{self.url}identifier") == "prefix:identifier"
96+
97+
def test_to_python_type(self, mock_context):
98+
cont = ld_container([{}], context=[mock_context])
9199
assert cont._to_python("@type", ["@id"]) == '@id'
92100
assert cont._to_python("@type", ["@id", "http://spam.eggs/Egg"]) == ["@id", "Egg"]
93101

94-
# Try type conversions
95-
assert cont._to_python("http://spam.eggs/ham", [{"@id": "spam"}]) == 'spam'
102+
def test_to_python_id_value(self, mock_context):
103+
cont = ld_container([{}], context=[mock_context])
104+
assert cont._to_python("http://spam.eggs/ham", [{"@id": "http://spam.eggs/spam"}]) == "http://spam.eggs/spam"
105+
assert cont._to_python("http://spam.eggs/ham", [{"@id": "http://spam.eggs/identifier"}]) == "http://spam.eggs/identifier"
96106

107+
def test_to_python_basic_value(self, mock_context):
108+
cont = ld_container([{}], context=[mock_context])
97109
assert cont._to_python("http://soam.eggs/spam", [{"@value": "bacon"}]) == 'bacon'
98110
assert cont._to_python("http://spam.eggs/spam", [{"@value": True}]) == True
99111
assert cont._to_python("http://spam.eggs/spam", [{"@value": 123}]) == 123
100112

113+
def test_to_python_datetime_value(self, mock_context):
114+
cont = ld_container([{}], context=[mock_context])
101115
assert cont._to_python("http://spam.eggs/eggs", [{
102116
"@value": "2022-02-22T00:00:00", "@type": "https://schema.org/DateTime"
103117
}]) == "2022-02-22T00:00:00"
104118

105-
def test_to_python_list(self, mock_context):
119+
def test_to_expanded_id(self, mock_context):
106120
cont = ld_container([{}], context=[mock_context])
107-
list_data = [{"@list": [{"@id": "spam"}, {"@id": "eggs"}]}]
121+
assert cont._to_expanded_json("@id", f"{self.url}identifier") == f"{self.url}identifier"
108122

109-
assert cont._to_python("ham", list_data).to_python() == ["spam", "eggs"]
123+
# Regression test: "ham" is vocabulary and must not be expanded.
124+
assert cont._to_expanded_json("@id", "ham") == "ham"
110125

111-
def test_to_expanded(self, mock_context):
112-
# Create container with mock context
113-
cont = ld_container([{}], context=[mock_context])
126+
def test_to_expanded_id_with_prefix(self, mock_context):
127+
cont = ld_container([{}], context=[mock_context, {"prefix": self.url}])
128+
assert cont._to_expanded_json("@id", "prefix:identifier") == f"{self.url}identifier"
114129

115-
# Try simple cases of expansion
130+
# Regression test: "ham" should still not be expaned, but "prefix:ham" should be.
116131
assert cont._to_expanded_json("@id", "ham") == "ham"
132+
assert cont._to_expanded_json("@id", "prefix:ham") == f"{self.url}ham"
133+
134+
def test_to_expanded_type(self, mock_context):
135+
cont = ld_container([{}], context=[mock_context])
117136
assert cont._to_expanded_json("@type", "Egg") == ["http://spam.eggs/Egg"]
137+
assert cont._to_expanded_json("@type", ["Egg", "@id"]) == ["http://spam.eggs/Egg", "@id"]
118138

119-
# Type conversions
139+
def test_to_expanded_id_value(self, mock_context):
140+
cont = ld_container([{}], context=[mock_context])
120141
assert cont._to_expanded_json("ham", "spam") == [{"@id": "spam"}]
121142

143+
def test_to_expanded_basic_value(self, mock_context):
144+
cont = ld_container([{}], context=[mock_context])
122145
assert cont._to_expanded_json("spam", "bacon") == [{"@value": "bacon"}]
123146
assert cont._to_expanded_json("spam", 123) == [{"@value": 123}]
124147
assert cont._to_expanded_json("spam", True) == [{"@value": True}]
125148

149+
def test_to_expanded_datetime_value(self, mock_context):
150+
cont = ld_container([{}], context=[mock_context])
126151
assert cont._to_expanded_json("eggs", datetime(2022, 2,22)) == [
127152
{"@value": "2022-02-22T00:00:00", "@type": "http://schema.org/DateTime"}
128153
]

0 commit comments

Comments
 (0)