forked from openMetadataInitiative/openMINDS_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_regressions.py
More file actions
302 lines (255 loc) · 11.4 KB
/
test_regressions.py
File metadata and controls
302 lines (255 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from datetime import date
import json
import os
import pytest
from openminds import Collection, IRI
import openminds.latest
import openminds.v4
from utils import build_fake_node
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue_0002(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/2
# @type should not be given as a list but as a string
node = build_fake_node(om.core.Person)
data = node.to_jsonld()
assert data["@type"] == "https://openminds.om-i.org/types/Person"
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue_0003(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/3
# validate() does not complain about direct entries that should be lists
# we address this by always wrapping a single item in a list in such cases
some_file = om.core.File(
iri=IRI("http://example.com/some_file.txt"),
name="some_file.txt",
)
node1 = om.core.FileArchive(
iri=IRI("http://example.com/archive.zip"),
format=om.core.ContentType(name="application/zip"),
source_data=[some_file], # multiple=True, min_items=1
)
node2 = om.core.FileArchive(
iri=IRI("http://example.com/archive.zip"),
format=om.core.ContentType(name="application/zip"),
source_data=some_file, # multiple=True, min_items=1
)
# on export, a single item should be wrapped in a list, where the property expects an array
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@type": "https://openminds.om-i.org/types/FileArchive",
"IRI": "http://example.com/archive.zip",
"format": {
"@type": "https://openminds.om-i.org/types/ContentType",
"name": "application/zip",
},
"sourceData": [
{
"@type": "https://openminds.om-i.org/types/File",
"IRI": "http://example.com/some_file.txt",
"name": "some_file.txt",
}
],
}
assert (
node1.to_jsonld(include_empty_properties=False) == node2.to_jsonld(include_empty_properties=False) == expected
)
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0005(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/5
# validate() does not complain about list/tuple entries that should be a direct single entry
uni1 = om.core.Organization(full_name="University of This Place")
person = om.core.Person(
given_name="A",
family_name="Professor",
affiliations=[om.core.Affiliation(member_of=uni1, end_date=(2023, 9, 30))],
)
failures = person.validate()
assert len(failures) == 1
person.affiliations[0].end_date = date(2023, 9, 30)
failures = person.validate()
assert len(failures) == 0
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0007(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/7
# Instances of embedded types with value type "array" are not correctly resolved for saving and causing an error.
person = om.core.Person(given_name="A", family_name="Professor", id="_:001")
uni1 = om.core.Organization(full_name="University of This Place", id="_:002")
uni2 = om.core.Organization(full_name="University of That Place", id="_:003")
person.affiliations = [
om.core.Affiliation(member_of=uni1),
om.core.Affiliation(member_of=uni2),
]
actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@id": "_:001",
"@type": "https://openminds.om-i.org/types/Person",
"familyName": "Professor",
"givenName": "A",
"affiliation": [
{
"@type": "https://openminds.om-i.org/types/Affiliation",
"memberOf": {"@id": "_:002"},
},
{
"@type": "https://openminds.om-i.org/types/Affiliation",
"memberOf": {"@id": "_:003"},
},
],
}
assert actual == expected
c = Collection(person, uni1, uni2)
output_paths = c.save("issue0007.jsonld", individual_files=False, include_empty_properties=False)
assert output_paths == ["issue0007.jsonld"]
with open(output_paths[0]) as fp:
saved_data = json.load(fp)
os.remove("issue0007.jsonld")
expected_saved_data = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@graph": [
{
"@id": "_:001",
"@type": "https://openminds.om-i.org/types/Person",
"affiliation": [
{
"@type": "https://openminds.om-i.org/types/Affiliation",
"memberOf": {"@id": "_:002"},
},
{
"@type": "https://openminds.om-i.org/types/Affiliation",
"memberOf": {"@id": "_:003"},
},
],
"familyName": "Professor",
"givenName": "A",
},
{
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of This Place",
},
{
"@id": "_:003",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of That Place",
},
],
}
assert saved_data == expected_saved_data
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0008(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/8
# The instance of linked types in instances of embedded types are integrated as embedded not linked
# (example: person -> affiliations (embedded) -> organization (linked))
uni1 = om.core.Organization(full_name="University of This Place", id="_:001")
person = om.core.Person(
id="_:002",
given_name="A",
family_name="Professor",
affiliations=[om.core.Affiliation(member_of=uni1, end_date=date(2023, 9, 30))],
)
actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Person",
"affiliation": [
{
"@type": "https://openminds.om-i.org/types/Affiliation",
"endDate": "2023-09-30",
"memberOf": {"@id": "_:001"},
}
],
"familyName": "Professor",
"givenName": "A",
}
assert actual == expected
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0026(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/26
# When reading a JSON-LD file, the attributes of LinkedMetadata nodes
# inside EmbeddedMetadata nodes are not set properly
uni1 = om.core.Organization(full_name="University of This Place", id="_:uthisp")
person = om.core.Person(
given_name="A", family_name="Professor", affiliations=[om.core.Affiliation(member_of=uni1)], id="_:ap"
)
c = Collection(person)
# uni1 was not added explicitly, but should nevertheless be included in the JSON-LD export
output_paths = c.save("issue0026.jsonld", individual_files=False, include_empty_properties=False)
new_collection = Collection()
new_collection.load(*output_paths, version=om.__name__.split(".")[1])
os.remove("issue0026.jsonld")
person_again = [item for item in new_collection if isinstance(item, om.core.Person)][0]
assert len(person_again.affiliations) == 1
assert person_again.affiliations[0].member_of.full_name == "University of This Place"
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0023(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/23
# If a user adds an instance/node to a collection, and then later adds linked types to the instance,
# currently that is not added to the collection
uni1 = om.core.Organization(full_name="University of This Place", id="_:uthisp")
person = om.core.Person(
given_name="A", family_name="Professor", affiliations=[om.core.Affiliation(member_of=uni1)], id="_:ap"
)
dv = om.core.DatasetVersion(full_name="The name of the dataset version", custodians=[person], id="_:dv")
c = Collection(dv)
# even though we add uni2 and the repository after creating the collection,
# they should be included when we save the collection.
uni2 = om.core.Organization(full_name="University of That Place", id="_:uthatp")
person.affiliations.append(om.core.Affiliation(member_of=uni2))
dv.repository = om.core.FileRepository(iri="http://example.com", id="_:fr")
output_paths = c.save("issue0023.jsonld", individual_files=False, include_empty_properties=False)
new_collection = Collection()
new_collection.load(*output_paths, version=om.__name__.split(".")[1])
os.remove("issue0023.jsonld")
dv_again = [item for item in new_collection if isinstance(item, om.core.DatasetVersion)][0]
assert isinstance(dv_again.repository, om.core.FileRepository)
assert dv_again.repository.iri.value == "http://example.com"
assert len(dv_again.custodians[0].affiliations) == 2
assert dv_again.custodians[0].affiliations[0].member_of.full_name == "University of This Place"
assert dv_again.custodians[0].affiliations[1].member_of.full_name == "University of That Place"
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
def test_issue0056(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/56
# Since we are permissive on object creation, serialization to JSON-LD should work
# even if the object gives validation failures.
# However, under some circumstances, to_jsonld() produces a data structure
# that cannot be saved as a JSON string.
dataset = om.core.Dataset(
digital_identifier=[
om.core.DOI(identifier="abc"),
om.core.DOI(identifier="def")
]
)
failures = dataset.validate(ignore=["required"])
assert len(failures) == 1
assert failures["multiplicity"] == ['digital_identifier does not accept multiple values, but contains 2']
data = dataset.to_jsonld()
json.dumps(data) # this should not raise an Exception
@pytest.mark.parametrize("om", [openminds.v4])
def test_issue0073a(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/73
# Infinite recursion in validate()
ds1 = om.core.DatasetVersion(
short_name="ds1",
is_alternative_version_of=None
)
ds2 = om.core.DatasetVersion(
short_name="ds2",
is_alternative_version_of=ds1
)
ds1.is_alternative_version_of = ds2
failures = ds1.validate()
@pytest.mark.parametrize("om", [openminds.latest])
def test_issue0073b(om):
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/73
# Infinite recursion in validate()
ds1 = om.core.DatasetVersion(
short_name="ds1",
is_variant_of=None
)
ds2 = om.core.DatasetVersion(
short_name="ds2",
is_variant_of=ds1
)
ds1.is_variant_of = ds2
failures = ds1.validate()