-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_create_get_xmlupload.py
515 lines (461 loc) · 22.4 KB
/
test_create_get_xmlupload.py
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import glob
import json
import shutil
import unittest
from pathlib import Path
from typing import Any, Optional, Union, cast
import regex
from dsp_tools.commands.id2iri import id2iri
from dsp_tools.commands.project.create.project_create import create_project
from dsp_tools.commands.project.get import get_project
from dsp_tools.commands.xmlupload.xmlupload import xmlupload
# ruff: noqa: PT009 (pytest-unittest-assertion) (remove this line when pytest is used instead of unittest)
class TestCreateGetXMLUpload(unittest.TestCase):
"""Test if the commands "create", "get", and "xmlupload" work together as expected."""
server = "http://0.0.0.0:3333"
user = "root@example.com"
password = "test"
imgdir = "."
sipi = "http://0.0.0.0:1024"
test_project_systematic_file = Path("testdata/json-project/test-project-systematic.json")
test_data_systematic_file = Path("testdata/xml-data/test-data-systematic.xml")
cwd = Path("cwd")
testdata_tmp = Path("testdata/tmp")
@classmethod
def setUpClass(cls) -> None:
"""Is executed once before the methods of this class are run"""
cls.testdata_tmp.mkdir(exist_ok=True)
cls.cwd.mkdir(exist_ok=True)
@classmethod
def tearDownClass(cls) -> None:
"""Is executed after the methods of this class have all run through"""
shutil.rmtree(cls.testdata_tmp)
shutil.rmtree(cls.cwd)
for f in Path().glob("*id2iri_*.json"):
f.unlink()
def test_create_project(self) -> None:
"""Test if the systematic JSON project file can be uploaded without producing an error on its way"""
success = create_project(
project_file_as_path_or_parsed=self.test_project_systematic_file.absolute(),
server=self.server,
user_mail=self.user,
password=self.password,
verbose=True,
)
self.assertTrue(success)
def test_xml_upload_incremental(self) -> None:
"""
Test if the systematic XML data file can be uploaded without producing an error on its way,
and if the 'id2iri' replacement works, so that the 2nd upload works.
"""
success = xmlupload(
input_file=self.test_data_systematic_file,
server=self.server,
user=self.user,
password=self.password,
imgdir=self.imgdir,
sipi=self.sipi,
)
self.assertTrue(success)
mapping_file = self._get_most_recent_glob_match("test-data-systematic_id2iri_mapping_*.json")
second_xml_file_orig = Path("testdata/id2iri/test-id2iri-data.xml")
success = id2iri(
xml_file=str(second_xml_file_orig),
json_file=str(mapping_file),
)
mapping_file.unlink()
self.assertTrue(success)
second_xml_file_replaced = self._get_most_recent_glob_match(f"{second_xml_file_orig.stem}_replaced_*.xml")
success = xmlupload(
input_file=second_xml_file_replaced,
server=self.server,
user=self.user,
password=self.password,
imgdir=self.imgdir,
sipi=self.sipi,
)
second_xml_file_replaced.unlink()
self.assertListEqual(list(Path(self.cwd).glob("stashed_*_properties_*.txt")), [])
self.assertTrue(success)
def test_get_project(self) -> None:
"""
Retrieve the systematic JSON project file with the "get" command,
and check if the result is identical to the original file.
"""
out_file = self.testdata_tmp / "_test-project-systematic.json"
success = get_project(
project_identifier="systematic-tp",
outfile_path=str(out_file),
server=self.server,
user=self.user,
password=self.password,
verbose=True,
)
self.assertTrue(success)
project_original = self._get_original_project()
with open(out_file, encoding="utf-8") as f:
project_returned = json.load(f)
self._compare_project(project_original, project_returned)
self._compare_groups(
groups_original=project_original["project"].get("groups"),
groups_returned=project_returned["project"].get("groups"),
)
self._compare_users(
users_original=project_original["project"].get("users"),
users_returned=project_returned["project"].get("users"),
project_shortname=project_original["project"]["shortname"],
)
self._compare_lists(
lists_original=project_original["project"].get("lists"),
lists_returned=project_returned["project"].get("lists"),
)
for file in [project_original, project_returned]:
file["project"]["ontologies"] = sorted(file["project"]["ontologies"], key=lambda x: cast(str, x["name"]))
for onto_original, onto_returned in zip(
project_original["project"]["ontologies"],
project_returned["project"]["ontologies"],
):
self._compare_properties(
properties_original=onto_original["properties"],
properties_returned=onto_returned["properties"],
onto_name=onto_original["name"],
)
self._compare_resources(
resources_original=onto_original["resources"],
resources_returned=onto_returned["resources"],
onto_name=onto_original["name"],
)
def _get_original_project(self) -> dict[str, Any]:
"""
Open the systematic JSON project file, expand all prefixes, and return the resulting Python object.
The prefixes must be expanded because the "get" command returns full IRIs instead of prefixed IRIs,
so the original prefixes won't be found in the returned file.
Returns:
the original project as a Python dictionary
"""
with open(self.test_project_systematic_file, encoding="utf-8") as f:
project_original_str = f.read()
project_original = json.loads(project_original_str)
for prefix, iri in project_original["prefixes"].items():
project_original_str = regex.sub(rf'"{prefix}:(\w+?)"', rf'"{iri}\1"', project_original_str)
project_original = json.loads(project_original_str)
return cast(dict[str, Any], project_original)
def _compare_project(
self,
project_original: dict[str, Any],
project_returned: dict[str, Any],
) -> None:
"""
Compare the basic metadata of 2 JSON project definitions.
Fails with a message if the metadata are not identical.
Args:
project_original: original file
project_returned: returned file
"""
for field in ["shortcode", "shortname", "longname", "descriptions"]:
orig = project_original["project"].get(field)
ret = project_returned["project"].get(field)
self.assertEqual(orig, ret, msg=f"Field '{field}' is not identical: original='{orig}', returned='{ret}'")
orig_keywords = sorted(project_original["project"]["keywords"])
ret_keywords = sorted(project_returned["project"]["keywords"])
self.assertEqual(
orig_keywords,
ret_keywords,
msg=f"Field keywords is not identical: original={orig_keywords}, returned={ret_keywords}",
)
def _compare_groups(
self,
groups_original: Optional[list[dict[str, Any]]],
groups_returned: Optional[list[dict[str, Any]]],
) -> None:
"""
Compare the "groups" section of the original JSON project definition
with the "groups" section of the JSON returned by the "get" command.
Fails with a message if the sections are not identical.
Args:
groups_original: "groups" section of the original file.
groups_returned: "groups" section of the returned file.
"""
# avoid mutable default argument
groups_original = groups_original or []
groups_returned = groups_returned or []
# write default values into original, in case they were omitted (the "get" command writes all default values)
for group in groups_original or []:
if group.get("status") is None:
group["status"] = True
if group.get("selfjoin") is None:
group["selfjoin"] = False
# sort both lists
groups_original = sorted(groups_original, key=lambda x: cast(str, x.get("name", "")))
groups_returned = sorted(groups_returned, key=lambda x: cast(str, x.get("name", "")))
if len(groups_original) != len(groups_returned):
self.assertEqual(
groups_original,
groups_returned,
msg="Returned number of groups is different from original number of groups.",
)
else:
for orig, ret in zip(groups_original, groups_returned):
self.assertEqual(
orig,
ret,
msg=f"Group with original name '{orig['name']}' and returned name '{ret['name']}' failed.",
)
def _compare_users(
self,
users_original: Optional[list[dict[str, Any]]],
users_returned: Optional[list[dict[str, Any]]],
project_shortname: str,
) -> None:
"""
Compare the "users" section of the original JSON project definition
with the "users" section of the JSON returned by the "get" command.
Fails with a message if the sections are not identical.
In order to do so, this method modifies the original as follows:
- remove the users that are not in the current project
- set all passwords to ""
- add default values, in case they were omitted,
- expand ":xyz" to "project_shortname:xyz"
Args:
users_original: "users" section of the original file.
users_returned: "users" section of the returned file.
project_shortname: shortname of the project
"""
# avoid mutable default argument
users_original = users_original or []
users_returned = users_returned or []
# remove users that are not in current project (they won't be returned by the "get" command)
if users_original:
current_project_membership_strings = [
":admin",
":member",
f"{project_shortname}:admin",
f"{project_shortname}:member",
]
users_original = [
u for u in users_original if any(p in u.get("projects", []) for p in current_project_membership_strings)
]
# bring the "users" section of original into the form that is returned by the "get" command
for user in users_original:
index = users_original.index(user)
# remove password
users_original[index]["password"] = ""
# write default values, in case they were omitted
if user.get("groups") is None:
users_original[index]["groups"] = []
if user.get("status") is None:
users_original[index]["status"] = True
# expand ":xyz" to "project_shortname:xyz"
if user.get("groups"):
users_original[index]["groups"] = [regex.sub("^:", f"{project_shortname}:", g) for g in user["groups"]]
if user.get("projects"):
users_original[index]["projects"] = [
regex.sub("^:", f"{project_shortname}:", p) for p in user["projects"]
]
# sort both lists
users_original = sorted(users_original or [], key=lambda x: cast(str, x["username"]))
users_returned = sorted(users_returned or [], key=lambda x: cast(str, x["username"]))
if len(users_original) != len(users_returned):
self.assertEqual(
users_original,
users_returned,
msg="Returned number of users is different from original number of users.",
)
else:
for orig, ret in zip(users_original, users_returned):
self.assertEqual(
orig,
ret,
msg=f"User with original name '{orig['username']}' and returned name '{ret['username']}' failed.",
)
def _compare_lists(
self,
lists_original: Optional[list[dict[str, Any]]],
lists_returned: Optional[list[dict[str, Any]]],
) -> None:
"""
Compare the "lists" section of the original JSON project definition
with the "lists" section of the JSON returned by the "get" command.
Fails with a message if the sections are not identical.
In order to do so,
this method removes all lists of which the nodes are defined in an Excel file,
because they cannot be compared.
Args:
lists_original: "lists" section of the original file.
lists_returned: "lists" section of the returned file.
"""
# avoid mutable default argument
lists_original = lists_original or []
lists_returned = lists_returned or []
# remove lists of which the nodes were defined in an Excel file
for list_original in lists_original:
if (
isinstance(list_original["nodes"], dict)
and len(list_original["nodes"]) == 1
and "folder" in list_original["nodes"]
):
lists_original.remove(list_original)
lists_returned = [x for x in lists_returned if x["name"] != list_original["name"]]
# sort both lists
lists_original = sorted(lists_original, key=lambda x: cast(str, x.get("name", "")))
lists_returned = sorted(lists_returned, key=lambda x: cast(str, x.get("name", "")))
if len(lists_original) != len(lists_returned):
self.assertEqual(
lists_original,
lists_returned,
msg="Returned number of lists is different from original number of lists.",
)
else:
for orig, ret in zip(lists_original, lists_returned):
self.assertEqual(
orig,
ret,
msg=f"List with original name '{orig['name']}' and returned name '{ret['name']}' failed.",
)
def _compare_properties(
self,
properties_original: Optional[list[dict[str, Any]]],
properties_returned: Optional[list[dict[str, Any]]],
onto_name: str,
) -> None:
"""
Compare the "properties" section of an onto of the original JSON project definition
with the "properties" section of the respective onto of the JSON returned by the "get" command.
Fails with a message if the sections are not identical.
Args:
properties_original: "properties" section of the original file.
properties_returned: "properties" section of the returned file.
onto_name: name of the ontology
"""
# avoid mutable default argument
properties_original = properties_original or []
properties_returned = properties_returned or []
# The original might have names in the form "currentonto:hasSimpleText".
# The returned file will have ":hasSimpleText" --> remove the onto name.
for prop in properties_original:
if any(sup.startswith(onto_name) for sup in prop["super"]):
prop["super"] = [regex.sub(rf"^{onto_name}:", ":", sup) for sup in prop["super"]]
# sort both lists
properties_original = sorted(properties_original, key=lambda x: cast(str, x.get("name", "")))
properties_returned = sorted(properties_returned, key=lambda x: cast(str, x.get("name", "")))
for proplist in [properties_original, properties_returned]:
for prop in proplist:
if isinstance(prop["super"], list):
prop["super"] = sorted(prop["super"])
if len(properties_original) != len(properties_returned):
self.assertEqual(
properties_original,
properties_returned,
msg=f"Onto {onto_name}: Returned number of properties is different from original number of properties.",
)
else:
for orig, ret in zip(properties_original, properties_returned):
self.assertEqual(
orig,
ret,
msg=f"Onto '{onto_name}': Property with original name '{orig['name']}' "
f"and returned name '{ret['name']}' failed.",
)
def _compare_resources(
self,
resources_original: Optional[list[dict[str, Any]]],
resources_returned: Optional[list[dict[str, Any]]],
onto_name: str,
) -> None:
"""
Compare the "resources" section of an onto of the original JSON project definition
with the "resources" section of the respective onto of the JSON returned by the "get" command.
Fails with a message if the sections are not identical.
Args:
resources_original: "resources" section of the original file.
resources_returned: "resources" section of the returned file.
onto_name: name of the ontology
"""
# avoid mutable default argument
resources_original = resources_original or []
resources_returned = resources_returned or []
resources_original = self._remove_onto_names_in_original_resources(onto_name, resources_original)
resources_original, resources_returned = self._remove_cardinalities_in_original_resources(
onto_name, resources_original, resources_returned
)
resources_original, resources_returned = self._sort_resources(resources_original, resources_returned)
if len(resources_original) != len(resources_returned):
self.assertEqual(
resources_original,
resources_returned,
msg=f"Onto {onto_name}: Returned number of resources is different from original number of resources.",
)
else:
for orig, ret in zip(resources_original, resources_returned):
if orig.get("cardinalities") != ret.get("cardinalities"):
self.assertEqual(
orig.get("cardinalities"),
ret.get("cardinalities"),
msg=f"Onto '{onto_name}': The cardinalities of resource with original name '{orig['name']}' "
f"and returned name '{ret['name']}' failed.",
)
else:
self.assertEqual(
orig,
ret,
msg=f"Onto '{onto_name}': Resource with original name '{orig['name']}' and returned name "
"'{ret['name']}' failed. The reason of the error lies OUTSIDE of the 'cardinalities' section.",
)
@staticmethod
def _sort_resources(
resources_original: list[dict[str, Any]], resources_returned: list[dict[str, Any]]
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
resources_original = sorted(resources_original, key=lambda x: cast(str, x.get("name", "")))
resources_returned = sorted(resources_returned, key=lambda x: cast(str, x.get("name", "")))
for reslist in [resources_original, resources_returned]:
for res in reslist:
if isinstance(res["super"], list):
res["super"] = sorted(res["super"])
if res.get("cardinalities"):
res["cardinalities"] = sorted(res["cardinalities"], key=lambda x: cast(str, x["propname"]))
return resources_original, resources_returned
@staticmethod
def _remove_cardinalities_in_original_resources(
onto_name: str, resources_original: list[dict[str, Any]], resources_returned: list[dict[str, Any]]
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
# If a subclass doesn't explicitly define all cardinalities of its superclass
# (or a subproperty of a cardinality of its superclass),
# these cardinalities are implicitly added, so the "get" command will return them.
# It would be too complicated to test this behaviour,
# --> remove the cardinalities of all subclasses from both original file and returned file.
for res in resources_original:
supers_as_list = [res["super"]] if isinstance(res["super"], str) else res["super"]
for sup in supers_as_list:
if regex.search(rf"^({onto_name})?:\w+$", sup):
if res.get("cardinalities"):
del res["cardinalities"]
# remove from returned file, too
res_returned = next(x for x in resources_returned if x["name"] == res["name"])
if res_returned.get("cardinalities"):
del res_returned["cardinalities"]
return resources_original, resources_returned
@staticmethod
def _remove_onto_names_in_original_resources(
onto_name: str, resources_original: list[dict[str, Any]]
) -> list[dict[str, Any]]:
# The original might have names in the form "currentonto:hasSimpleText".
# The returned file will have ":hasSimpleText" --> remove the onto name.
for res in resources_original:
for card in res.get("cardinalities", []):
if card["propname"].startswith(onto_name):
card["propname"] = regex.sub(rf"^{onto_name}:", ":", card["propname"])
supers_as_list = [res["super"]] if isinstance(res["super"], str) else res["super"]
if any(sup.startswith(onto_name) for sup in supers_as_list):
res["super"] = [regex.sub(rf"^{onto_name}:", ":", sup) for sup in supers_as_list]
return resources_original
@staticmethod
def _get_most_recent_glob_match(glob_pattern: Union[str, Path]) -> Path:
"""
Find the most recently created file that matches a glob pattern.
Args:
glob_pattern: glob pattern, either absolute or relative to the cwd of the caller
Returns:
the most recently created file that matches the glob pattern
"""
candidates = [Path(x) for x in glob.glob(str(glob_pattern))]
return max(candidates, key=lambda item: item.stat().st_ctime)