forked from openMetadataInitiative/openMINDS_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_collections.py
More file actions
130 lines (102 loc) · 4.36 KB
/
test_collections.py
File metadata and controls
130 lines (102 loc) · 4.36 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
"""
Tests of the Collection class.
"""
import os.path
import shutil
import json
from openminds.collection import Collection
import openminds.latest.controlled_terms
import openminds.latest.core as omcore
import openminds.latest.publications
import openminds.latest.computation
import openminds.latest.sands
from utils import build_fake_node
test_output_dir = "test_tmp"
def test_save_collection_single_file():
shutil.rmtree(test_output_dir, ignore_errors=True)
person = build_fake_node(omcore.Person)
collection = Collection(person)
test_file_path = os.path.join(test_output_dir, "test_collection.jsonld")
collection.save(test_file_path, individual_files=False, include_empty_properties=False)
def test_round_trip_single_file():
shutil.rmtree(test_output_dir, ignore_errors=True)
person = build_fake_node(omcore.Person)
collection = Collection(person)
test_file_path = os.path.join(test_output_dir, "test_collection.jsonld")
collection.save(test_file_path, individual_files=False, include_empty_properties=False)
new_collection = Collection()
new_collection.load(test_file_path)
assert len(collection) == len(new_collection)
for node in new_collection:
if node.id == person.id:
new_person = person
break
p = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
np = new_person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
assert p == np
def test_save_collection_multi_file():
shutil.rmtree(test_output_dir, ignore_errors=True)
person = build_fake_node(omcore.Person)
collection = Collection(person)
collection.save(test_output_dir, individual_files=True, include_empty_properties=False)
def test_round_trip_multi_file():
shutil.rmtree(test_output_dir, ignore_errors=True)
person = build_fake_node(omcore.Person)
collection = Collection(person)
collection.save(test_output_dir, individual_files=True, include_empty_properties=False)
new_collection = Collection()
new_collection.load(test_output_dir)
assert len(collection) == len(new_collection)
for node in new_collection:
if node.id == person.id:
new_person = person
break
p = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
np = new_person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
assert p == np
def test_collection_sort_by_id():
person = omcore.Person(given_name="A", family_name="Professor", id="_:004")
uni1 = omcore.Organization(full_name="University of This Place", id="_:002")
uni2 = omcore.Organization(full_name="University of That Place", id="_:001")
person.affiliations = [
omcore.Affiliation(member_of=uni1),
omcore.Affiliation(member_of=uni2),
]
c = Collection(person, uni1, uni2)
output_paths = c.save("test_collection_sort_by_id.jsonld", individual_files=False, include_empty_properties=False)
assert output_paths == ["test_collection_sort_by_id.jsonld"]
with open(output_paths[0]) as fp:
saved_data = json.load(fp)
os.remove("test_collection_sort_by_id.jsonld")
expected_saved_data = {
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"schema": "https://schema.org/",
},
"@graph": [
{
"@id": "_:001",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of That Place",
"schema:schemaVersion": "latest"
},
{
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of This Place",
"schema:schemaVersion": "latest"
},
{
"@id": "_:004",
"@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": "_:001"}},
],
"familyName": "Professor",
"givenName": "A",
"schema:schemaVersion": "latest"
},
],
}
assert saved_data == expected_saved_data