|
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | 5 | # SPDX-FileContributor: Sophie Kernchen |
| 6 | +# SPDX-FileContributor: Michael Meinel |
| 7 | + |
| 8 | +from datetime import datetime |
6 | 9 |
|
7 | 10 | import pytest |
| 11 | + |
8 | 12 | from hermes.model.types.ld_container import ld_container |
9 | 13 |
|
10 | 14 | '''we expect user of this class to give the right input data types |
@@ -82,47 +86,68 @@ def test_container_str_and_repr(self): |
82 | 86 | with pytest.raises(NotImplementedError): |
83 | 87 | str(cont) |
84 | 88 |
|
85 | | - def test_to_python(self, mock_context): |
86 | | - # Create container with mock context |
| 89 | + def test_to_python_id(self, mock_context): |
87 | 90 | cont = ld_container([{}], context=[mock_context]) |
| 91 | + assert cont._to_python("@id", "http://spam.eggs/ham") == "http://spam.eggs/ham" |
88 | 92 |
|
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]) |
91 | 99 | assert cont._to_python("@type", ["@id"]) == '@id' |
92 | 100 | assert cont._to_python("@type", ["@id", "http://spam.eggs/Egg"]) == ["@id", "Egg"] |
93 | 101 |
|
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" |
96 | 106 |
|
| 107 | + def test_to_python_basic_value(self, mock_context): |
| 108 | + cont = ld_container([{}], context=[mock_context]) |
97 | 109 | assert cont._to_python("http://soam.eggs/spam", [{"@value": "bacon"}]) == 'bacon' |
98 | 110 | assert cont._to_python("http://spam.eggs/spam", [{"@value": True}]) == True |
99 | 111 | assert cont._to_python("http://spam.eggs/spam", [{"@value": 123}]) == 123 |
100 | 112 |
|
| 113 | + def test_to_python_datetime_value(self, mock_context): |
| 114 | + cont = ld_container([{}], context=[mock_context]) |
101 | 115 | assert cont._to_python("http://spam.eggs/eggs", [{ |
102 | 116 | "@value": "2022-02-22T00:00:00", "@type": "https://schema.org/DateTime" |
103 | 117 | }]) == "2022-02-22T00:00:00" |
104 | 118 |
|
105 | | - def test_to_python_list(self, mock_context): |
| 119 | + def test_to_expanded_id(self, mock_context): |
106 | 120 | 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" |
108 | 122 |
|
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" |
110 | 125 |
|
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" |
114 | 129 |
|
115 | | - # Try simple cases of expansion |
| 130 | + # Regression test: "ham" should still not be expaned, but "prefix:ham" should be. |
116 | 131 | 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]) |
117 | 136 | 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"] |
118 | 138 |
|
119 | | - # Type conversions |
| 139 | + def test_to_expanded_id_value(self, mock_context): |
| 140 | + cont = ld_container([{}], context=[mock_context]) |
120 | 141 | assert cont._to_expanded_json("ham", "spam") == [{"@id": "spam"}] |
121 | 142 |
|
| 143 | + def test_to_expanded_basic_value(self, mock_context): |
| 144 | + cont = ld_container([{}], context=[mock_context]) |
122 | 145 | assert cont._to_expanded_json("spam", "bacon") == [{"@value": "bacon"}] |
123 | 146 | assert cont._to_expanded_json("spam", 123) == [{"@value": 123}] |
124 | 147 | assert cont._to_expanded_json("spam", True) == [{"@value": True}] |
125 | 148 |
|
| 149 | + def test_to_expanded_datetime_value(self, mock_context): |
| 150 | + cont = ld_container([{}], context=[mock_context]) |
126 | 151 | assert cont._to_expanded_json("eggs", datetime(2022, 2,22)) == [ |
127 | 152 | {"@value": "2022-02-22T00:00:00", "@type": "http://schema.org/DateTime"} |
128 | 153 | ] |
0 commit comments