Skip to content

Commit a795972

Browse files
Update tests to latest/v5 (#89)
* Update test_collections.py to latest/v5 * * Update DEFAULT_VERSION global variable * Update test_collections.py * * Add regression test test_issue0005b * Temporarily remove latest from tests that are currently failing * * Add regression test test_issue0007b * Add updated tests to test_regressions.py * Cleanup of unused code
1 parent 5c0f962 commit a795972

3 files changed

Lines changed: 262 additions & 40 deletions

File tree

pipeline/src/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .base import Link
1515

1616

17-
DEFAULT_VERSION = "v4"
17+
DEFAULT_VERSION = "v5"
1818

1919

2020
class Collection:

pipeline/tests/test_collections.py

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,14 @@ def test_round_trip_multi_file_group_by_schema():
9898

9999

100100
def test_collection_sort_by_id():
101-
person = omcore.Person(given_name="A", family_name="Professor", id="_:004")
102-
uni1 = omcore.Organization(full_name="University of This Place", id="_:002")
103-
uni2 = omcore.Organization(full_name="University of That Place", id="_:001")
104-
person.affiliations = [
105-
omcore.Affiliation(member_of=uni1),
106-
omcore.Affiliation(member_of=uni2),
101+
person = omcore.Person(preferred_name="A", family_name="Professor", id="_:004")
102+
uni1 = omcore.Organization(name="University of This Place", id="_:002")
103+
uni2 = omcore.Organization(name="University of That Place", id="_:001")
104+
uni1.memberships = [
105+
omcore.Membership(member=person),
106+
]
107+
uni2.memberships = [
108+
omcore.Membership(member=person),
107109
]
108110

109111
c = Collection(person, uni1, uni2)
@@ -116,29 +118,43 @@ def test_collection_sort_by_id():
116118
os.remove("test_collection_sort_by_id.jsonld")
117119

118120
expected_saved_data = {
119-
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
120-
"@graph": [
121-
{
122-
"@id": "_:001",
123-
"@type": "https://openminds.om-i.org/types/Organization",
124-
"fullName": "University of That Place",
125-
},
126-
{
127-
"@id": "_:002",
128-
"@type": "https://openminds.om-i.org/types/Organization",
129-
"fullName": "University of This Place",
130-
},
131-
{
132-
"@id": "_:004",
133-
"@type": "https://openminds.om-i.org/types/Person",
134-
"affiliation": [
135-
{"@type": "https://openminds.om-i.org/types/Affiliation", "memberOf": {"@id": "_:002"}},
136-
{"@type": "https://openminds.om-i.org/types/Affiliation", "memberOf": {"@id": "_:001"}},
137-
],
138-
"familyName": "Professor",
139-
"givenName": "A",
140-
},
141-
],
121+
"@context":{
122+
"@vocab":"https://openminds.om-i.org/props/"
123+
},
124+
"@graph":[
125+
{
126+
"@id":"_:001",
127+
"@type":"https://openminds.om-i.org/types/Organization",
128+
"membership":[
129+
{
130+
"@type":"https://openminds.om-i.org/types/Membership",
131+
"member":{
132+
"@id":"_:004"
133+
}
134+
}
135+
],
136+
"name":"University of That Place"
137+
},
138+
{
139+
"@id":"_:002",
140+
"@type":"https://openminds.om-i.org/types/Organization",
141+
"membership":[
142+
{
143+
"@type":"https://openminds.om-i.org/types/Membership",
144+
"member":{
145+
"@id":"_:004"
146+
}
147+
}
148+
],
149+
"name":"University of This Place"
150+
},
151+
{
152+
"@id":"_:004",
153+
"@type":"https://openminds.om-i.org/types/Person",
154+
"familyName":"Professor",
155+
"preferredName":"A"
156+
}
157+
]
142158
}
143159

144160
assert saved_data == expected_saved_data

pipeline/tests/test_regressions.py

Lines changed: 216 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from openminds import Collection, IRI
88
import openminds.latest
99
import openminds.v4
10+
import openminds.v5
1011
from utils import build_fake_node
1112

1213

@@ -65,8 +66,8 @@ def test_issue_0003(om):
6566
)
6667

6768

68-
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
69-
def test_issue0005(om):
69+
@pytest.mark.parametrize("om", [openminds.v4])
70+
def test_issue0005a(om):
7071
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/5
7172
# validate() does not complain about list/tuple entries that should be a direct single entry
7273
uni1 = om.core.Organization(full_name="University of This Place")
@@ -82,9 +83,26 @@ def test_issue0005(om):
8283
failures = person.validate()
8384
assert len(failures) == 0
8485

86+
@pytest.mark.parametrize("om", [openminds.v5, openminds.latest])
87+
def test_issue0005b(om):
88+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/5
89+
# validate() does not complain about list/tuple entries that should be a direct single entry
90+
person = om.core.Person(
91+
preferred_name="A",
92+
family_name="Professor"
93+
)
94+
uni1 = om.core.Organization(name="University of This Place", country_of_formation=om.controlled_terms.sovereign_state.SovereignState.by_name("Germany"), type=om.controlled_terms.organization_type.OrganizationType.by_name('organizational unit'), memberships=[om.core.Membership(member=person, end_date=(2023, 9, 30))])
95+
failures = uni1.validate()
96+
assert len(failures) == 1
8597

86-
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
87-
def test_issue0007(om):
98+
uni1.memberships[0].end_date = date(2023, 9, 30)
99+
print(uni1.type)
100+
failures = uni1.validate()
101+
assert len(failures) == 0
102+
103+
104+
@pytest.mark.parametrize("om", [openminds.v4])
105+
def test_issue0007a(om):
88106
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/7
89107
# Instances of embedded types with value type "array" are not correctly resolved for saving and causing an error.
90108

@@ -157,8 +175,93 @@ def test_issue0007(om):
157175
assert saved_data == expected_saved_data
158176

159177

160-
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
161-
def test_issue0008(om):
178+
@pytest.mark.parametrize("om", [openminds.v5, openminds.latest])
179+
def test_issue0007b(om):
180+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/7
181+
# Instances of embedded types with value type "array" are not correctly resolved for saving and causing an error.
182+
183+
person = om.core.Person(preferred_name="A", family_name="Professor", id="_:001")
184+
uni1 = om.core.Organization(name="University of This Place", id="_:002")
185+
uni2 = om.core.Organization(name="University of That Place", id="_:003")
186+
person2 = om.core.Person(preferred_name="B", family_name="Professor", id="_:004")
187+
uni1.memberships = [
188+
om.core.Membership(member=person),
189+
]
190+
uni2.memberships = [
191+
om.core.Membership(member=person),
192+
om.core.Membership(member=person2)
193+
]
194+
195+
actual = uni1.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
196+
expected = {
197+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
198+
"@id": "_:002",
199+
"@type": "https://openminds.om-i.org/types/Organization",
200+
"membership": [
201+
{
202+
"@type": "https://openminds.om-i.org/types/Membership",
203+
"member": {"@id": "_:001"}
204+
}
205+
],
206+
"name": "University of This Place"
207+
}
208+
assert actual == expected
209+
210+
c = Collection(person, uni1, uni2)
211+
output_paths = c.save("issue0007.jsonld", individual_files=False, include_empty_properties=False)
212+
assert output_paths == ["issue0007.jsonld"]
213+
214+
with open(output_paths[0]) as fp:
215+
saved_data = json.load(fp)
216+
os.remove("issue0007.jsonld")
217+
expected_saved_data = {
218+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
219+
"@graph": [
220+
{
221+
"@id": "_:001",
222+
"@type": "https://openminds.om-i.org/types/Person",
223+
"familyName": "Professor",
224+
"preferredName": "A",
225+
},
226+
{
227+
"@id": "_:002",
228+
"@type": "https://openminds.om-i.org/types/Organization",
229+
"membership": [
230+
{
231+
"@type": "https://openminds.om-i.org/types/Membership",
232+
"member": {"@id": "_:001"},
233+
}
234+
],
235+
"name": "University of This Place",
236+
},
237+
{
238+
"@id": "_:003",
239+
"@type": "https://openminds.om-i.org/types/Organization",
240+
"membership": [
241+
{
242+
"@type": "https://openminds.om-i.org/types/Membership",
243+
"member": {"@id": "_:001"},
244+
},
245+
{
246+
"@type": "https://openminds.om-i.org/types/Membership",
247+
"member": {"@id": "_:004"},
248+
},
249+
],
250+
"name": "University of That Place",
251+
},
252+
{
253+
"@id": "_:004",
254+
"@type": "https://openminds.om-i.org/types/Person",
255+
"familyName": "Professor",
256+
"preferredName": "B",
257+
}
258+
],
259+
}
260+
assert saved_data == expected_saved_data
261+
262+
263+
@pytest.mark.parametrize("om", [openminds.v4])
264+
def test_issue0008a(om):
162265
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/8
163266
# The instance of linked types in instances of embedded types are integrated as embedded not linked
164267
# (example: person -> affiliations (embedded) -> organization (linked))
@@ -188,8 +291,42 @@ def test_issue0008(om):
188291
assert actual == expected
189292

190293

191-
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
192-
def test_issue0026(om):
294+
@pytest.mark.parametrize("om", [openminds.v5, openminds.latest])
295+
def test_issue0008b(om):
296+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/8
297+
# The instance of linked types in instances of embedded types are integrated as embedded not linked
298+
# (example: organization -> memberships (embedded) -> person (linked))
299+
300+
person = om.core.Person(
301+
id="_:002",
302+
preferred_name="A",
303+
family_name="Professor"
304+
)
305+
306+
uni1 = om.core.Organization(
307+
name="University of This Place",
308+
id="_:001",
309+
memberships=om.core.Membership(member=person, end_date=date(2023, 9, 30))
310+
)
311+
actual = uni1.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
312+
expected = {
313+
'@context': {'@vocab': 'https://openminds.om-i.org/props/'},
314+
'@id': '_:001',
315+
'@type': 'https://openminds.om-i.org/types/Organization',
316+
'membership': [
317+
{
318+
'@type': 'https://openminds.om-i.org/types/Membership',
319+
'endDate': '2023-09-30',
320+
'member': {'@id': '_:002'}
321+
}
322+
],
323+
'name': 'University of This Place'
324+
}
325+
assert actual == expected
326+
327+
328+
@pytest.mark.parametrize("om", [openminds.v4])
329+
def test_issue0026a(om):
193330
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/26
194331
# When reading a JSON-LD file, the attributes of LinkedMetadata nodes
195332
# inside EmbeddedMetadata nodes are not set properly
@@ -214,8 +351,35 @@ def test_issue0026(om):
214351
assert person_again.affiliations[0].member_of.full_name == "University of This Place"
215352

216353

217-
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
218-
def test_issue0023(om):
354+
@pytest.mark.parametrize("om", [openminds.v5, openminds.latest])
355+
def test_issue0026b(om):
356+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/26
357+
# When reading a JSON-LD file, the attributes of LinkedMetadata nodes
358+
# inside EmbeddedMetadata nodes are not set properly
359+
360+
person = om.core.Person(
361+
preferred_name="A", family_name="Professor", id="_:ap"
362+
)
363+
uni1 = om.core.Organization(name="University of This Place",
364+
id="_:uthisp",
365+
memberships=[om.core.Membership(member=person)])
366+
c = Collection(uni1)
367+
368+
# person was not added explicitly, but should nevertheless be included in the JSON-LD export
369+
370+
output_paths = c.save("issue0026.jsonld", individual_files=False, include_empty_properties=False)
371+
372+
new_collection = Collection()
373+
new_collection.load(*output_paths, version=om.__name__.split(".")[1])
374+
os.remove("issue0026.jsonld")
375+
376+
uni_again = [item for item in new_collection if isinstance(item, om.core.Organization)][0]
377+
assert len(uni_again.memberships) == 1
378+
assert uni_again.memberships[0].member.family_name == "Professor"
379+
380+
381+
@pytest.mark.parametrize("om", [openminds.v4])
382+
def test_issue0023a(om):
219383
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/23
220384
# If a user adds an instance/node to a collection, and then later adds linked types to the instance,
221385
# currently that is not added to the collection
@@ -248,6 +412,48 @@ def test_issue0023(om):
248412
assert dv_again.custodians[0].affiliations[1].member_of.full_name == "University of That Place"
249413

250414

415+
@pytest.mark.parametrize("om", [openminds.v5])
416+
def test_issue0023b(om):
417+
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/23
418+
# If a user adds an instance/node to a collection, and then later adds linked types to the instance,
419+
# currently that is not added to the collection
420+
421+
person = om.core.Person(
422+
preferred_name="A", family_name="Professor", id="_:ap"
423+
)
424+
uni1 = om.core.Organization(name="University of This Place",
425+
id="_:uthisp",
426+
memberships=[om.core.Membership(member=person)])
427+
dv = om.core.DatasetVersion(full_name="The name of the dataset version",
428+
contributions=[om.core.Contribution(contributors=[uni1],
429+
type=om.controlled_terms.contribution_type.ContributionType.by_name('custodianship'))],
430+
id="_:dv")
431+
432+
c = Collection(dv)
433+
434+
# even though we add uni2 and the repository after creating the collection,
435+
# they should be included when we save the collection.
436+
person2 = om.core.Person(
437+
preferred_name="B", family_name="Professor", id="_:bp"
438+
)
439+
uni1.memberships.append(om.core.Membership(member=person2))
440+
dv.repository = om.core.FileRepository(iri="http://example.com", id="_:fr")
441+
442+
output_paths = c.save("issue0023.jsonld", individual_files=False, include_empty_properties=False)
443+
444+
new_collection = Collection()
445+
new_collection.load(*output_paths, version=om.__name__.split(".")[1])
446+
os.remove("issue0023.jsonld")
447+
448+
dv_again = [item for item in new_collection if isinstance(item, om.core.DatasetVersion)][0]
449+
assert isinstance(dv_again.repository, om.core.FileRepository)
450+
assert dv_again.repository.iri.value == "http://example.com"
451+
assert len(dv_again.contributions[0].contributors[0].memberships) == 2
452+
assert (dv_again.contributions[0].contributors[0].name == "University of This Place")
453+
assert dv_again.contributions[0].contributors[0].memberships[0].member.preferred_name == "A"
454+
assert dv_again.contributions[0].contributors[0].memberships[1].member.preferred_name == "B"
455+
456+
251457
@pytest.mark.parametrize("om", [openminds.latest, openminds.v4])
252458
def test_issue0056(om):
253459
# https://github.com/openMetadataInitiative/openMINDS_Python/issues/56

0 commit comments

Comments
 (0)