-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_cli.py
639 lines (569 loc) · 29.1 KB
/
test_cli.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"""
This test class tests the basic functionalities of dsp-tools, i.e. all commands that can be called from the command
line. The methods are tested in the order in which teh appear in dsp_tools.py. This class only tests that the methods
can be called with the basic configuration that is available via CLI. More thorough testing of each method is done in
separate unit tests/e2e tests."""
# pylint: disable=missing-class-docstring,missing-function-docstring
import copy
import json
from pathlib import Path
import shutil
import subprocess
from typing import Any, Optional, cast
import unittest
import jsonpath_ng
import jsonpath_ng.ext
import pytest
import regex
from dsp_tools.utils.project_create_lists import create_lists
class TestCLI(unittest.TestCase):
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_project_minimal_file = Path("testdata/json-project/test-project-minimal.json")
test_project_hlist_file = Path("testdata/json-project/test-project-hlist-refers-label.json")
test_data_systematic_file = Path("testdata/xml-data/test-data-systematic.xml")
test_data_minimal_file = Path("testdata/xml-data/test-data-minimal.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 _make_cli_call(
self,
cli_call: str,
working_directory: Path = cwd,
) -> None:
"""
Execute a CLI call, capture its stdout and stderr,
and raise an AssertionError with stdout and stderr if it fails.
Args:
cli_call: a command that can be executed by the system shell
working_directory: working directory where to execute the call. Defaults to the class' default_cwd.
Raises:
AssertionError: detailed diagnostic message with stdout and stderr if the call fails
"""
try:
subprocess.run(
cli_call,
check=True,
shell=True,
capture_output=True,
cwd=working_directory,
)
except subprocess.CalledProcessError as e:
msg = (
f"Failed CLI call:\n'{cli_call}'\n\n"
f"Stdout:\n{e.output.decode('utf-8')}\n\n"
f"Stderr:\n{e.stderr.decode('utf-8')}"
)
raise AssertionError(msg) from None
def test_validate_lists_section_with_schema(self) -> None:
cmd = f"poetry run dsp-tools create --lists-only --validate-only {self.test_project_systematic_file.absolute()}"
self._make_cli_call(cmd)
def test_create_lists(self) -> None:
# the project must already exist, so let's create a project without lists
self._make_cli_call(f"poetry run dsp-tools create {self.test_project_minimal_file.absolute()}")
# open a "lists" section and the project that was created
with open("testdata/excel2json/lists-multilingual-output-expected.json", encoding="utf-8") as f:
lists_section = json.load(f)
with open(self.test_project_minimal_file, encoding="utf-8") as f:
test_project_minimal = json.load(f)
# create a copy of the project that was created, and insert the first list into it
tp_minimal_with_list_1 = copy.deepcopy(test_project_minimal)
tp_minimal_with_list_1["project"]["lists"] = [lists_section[0]]
# create another copy of the project that was created, insert the second list into it, and save it as file
tp_minimal_with_list_2 = copy.deepcopy(test_project_minimal)
tp_minimal_with_list_2["project"]["lists"] = [lists_section[1]]
tp_minimal_with_list_2_file = Path("testdata/tmp/test_project_minimal_with_list_2.json")
with open(tp_minimal_with_list_2_file, "x", encoding="utf-8") as f:
json.dump(tp_minimal_with_list_2, f)
# The method to be tested can now be called with both versions of the same project
# (each containing another list).
# The first is a python object and is created with a function call,
# the second is a file and is created with a command line call.
name2iri_mapping1, success1 = create_lists(
server=self.server,
user=self.user,
password=self.password,
project_file_as_path_or_parsed=tp_minimal_with_list_1,
)
self._make_cli_call(f"poetry run dsp-tools create --lists-only {tp_minimal_with_list_2_file.absolute()}")
# In the first case (Python function call), it can be tested if the returned mapping is correct
self.assertTrue(success1)
name2iri_names_1 = [str(m.path) for m in jsonpath_ng.ext.parse("$..* where id").find(name2iri_mapping1)]
node_names_1 = [m.value for m in jsonpath_ng.ext.parse("$.project.lists[*]..name").find(tp_minimal_with_list_1)]
self.assertListEqual(name2iri_names_1, node_names_1)
def test_validate_project(self) -> None:
cli_call = f"poetry run dsp-tools create --validate-only {self.test_project_systematic_file.absolute()}"
# the JSON file contains a reference to an Excel file, which is relative to the root of the repo
cwd = Path(".")
self._make_cli_call(
cli_call=cli_call,
working_directory=cwd,
)
def test_create_project(self) -> None:
cli_call = f"poetry run dsp-tools create {self.test_project_systematic_file.absolute()} --verbose"
# the JSON file contains a reference to an Excel file, which is relative to the root of the repo
cwd = Path(".")
self._make_cli_call(
cli_call=cli_call,
working_directory=cwd,
)
def test_create_project_hlist_refers_label(self) -> None:
self._make_cli_call(f"poetry run dsp-tools create {self.test_project_hlist_file.absolute()} -v")
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 = Path("testdata/tmp/_test-project-systematic.json")
self._make_cli_call(f"poetry run dsp-tools get --project tp {out_file.absolute()}")
project_original = self._get_original_project()
with open("testdata/tmp/_test-project-systematic.json", 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 test_validate_xml_against_schema(self) -> None:
cmd = f"poetry run dsp-tools xmlupload --validate-only --verbose {self.test_data_systematic_file.absolute()}"
self._make_cli_call(cmd)
def test_xml_upload(self) -> None:
self._make_cli_call(f"poetry run dsp-tools xmlupload -v {self.test_data_minimal_file.absolute()}")
def test_xml_upload_incremental(self) -> None:
cli_call_1 = f"poetry run dsp-tools xmlupload {self.test_data_systematic_file.absolute()}"
# the XML file contains references to multimedia files that are relative to the root of the repo
cwd = Path(".")
self._make_cli_call(
cli_call=cli_call_1,
working_directory=cwd,
)
mapping_file = list(Path().glob("test-data-systematic_id2iri_mapping_*.json"))[0]
xml_file_orig = Path("testdata/id2iri/test-id2iri-data.xml")
xml_file_replaced = Path("testdata/tmp/_test-id2iri-replaced.xml")
cli_call_2 = (
f"poetry run dsp-tools id2iri --verbose "
f"--outfile {xml_file_replaced.absolute()} "
f"{xml_file_orig.absolute()} "
f"{mapping_file.absolute()}"
)
self._make_cli_call(cli_call_2)
self._make_cli_call(f"poetry run dsp-tools xmlupload --incremental -v {xml_file_replaced.absolute()}")
self.assertListEqual(list(Path(self.cwd).glob("stashed_*_properties_*.txt")), [])
mapping_file.unlink()
xml_file_replaced.unlink()
def test_excel_to_json_project(self) -> None:
excel_folder = Path("testdata/excel2json/excel2json_files")
out_file = Path("testdata/tmp/_out_project.json")
self._make_cli_call(f"poetry run dsp-tools excel2json {excel_folder.absolute()} {out_file.absolute()}")
with open("testdata/excel2json/excel2json-expected-output.json", encoding="utf-8") as f:
output_expected = json.load(f)
with open(out_file, encoding="utf-8") as f:
output = json.load(f)
self.assertDictEqual(output, output_expected)
out_file.unlink()
def test_excel_to_json_list(self) -> None:
excel_folder = Path("testdata/excel2json/lists-multilingual")
out_file = Path("testdata/tmp/_lists-out.json")
self._make_cli_call(f"poetry run dsp-tools excel2lists {excel_folder.absolute()} {out_file.absolute()}")
with open(out_file, encoding="utf-8") as f:
output_actual = json.load(f)
with open("testdata/excel2json/lists-multilingual-output-expected.json", encoding="utf-8") as f:
output_expected = json.load(f)
self.assertListEqual(output_actual, output_expected)
out_file.unlink()
def test_excel_to_json_resources(self) -> None:
excel_file = Path("testdata/excel2json/excel2json_files/test-name (test_label)/resources.xlsx")
out_file = Path("testdata/tmp/_out_resources.json")
self._make_cli_call(f"poetry run dsp-tools excel2resources '{excel_file.absolute()}' {out_file.absolute()}")
with open(out_file, encoding="utf-8") as f:
output_actual = json.load(f)
with open("testdata/excel2json/resources-output-expected.json", encoding="utf-8") as f:
output_expected = json.load(f)
self.assertListEqual(output_actual, output_expected)
out_file.unlink()
def test_excel_to_json_properties(self) -> None:
excel_file = Path("testdata/excel2json/excel2json_files/test-name (test_label)/properties.xlsx")
out_file = Path("testdata/tmp/_out_properties.json")
self._make_cli_call(f"poetry run dsp-tools excel2properties '{excel_file.absolute()}' {out_file.absolute()}")
with open(out_file, encoding="utf-8") as f:
output_actual = json.load(f)
with open("testdata/excel2json/properties-output-expected.json", encoding="utf-8") as f:
output_expected = json.load(f)
self.assertListEqual(output_actual, output_expected)
out_file.unlink()
def test_id_to_iri(self) -> None:
xml_file = Path("testdata/id2iri/test-id2iri-data.xml")
mapping_file = Path("testdata/id2iri/test-id2iri-mapping.json")
out_file = Path("testdata/tmp/test-id2iri-out.xml")
cli_call = (
f"poetry run dsp-tools id2iri --verbose "
f"--outfile {out_file.absolute()} "
f"{xml_file.absolute()} "
f"{mapping_file.absolute()}"
)
self._make_cli_call(cli_call)
with open(out_file, encoding="utf-8") as f:
output_actual = f.read()
with open("testdata/id2iri/test-id2iri-output-expected.xml", encoding="utf-8") as f:
output_expected = f.read()
self.assertEqual(output_actual, output_expected)
out_file.unlink()
@pytest.mark.filterwarnings("ignore")
def test_excel2xml(self) -> None:
datafile = Path("testdata/excel2xml/excel2xml-testdata.xlsx")
shortcode = "1234"
onto_name = "excel2xml-output"
out_file = self.cwd / f"{onto_name}-data.xml"
self._make_cli_call(f"poetry run dsp-tools excel2xml {datafile.absolute()} {shortcode} {onto_name}")
with open(out_file, encoding="utf-8") as f:
output_actual = f.read()
with open("testdata/excel2xml/excel2xml-expected-output.xml", encoding="utf-8") as f:
output_expected = f.read()
self.assertEqual(output_actual, output_expected)
out_file.unlink()
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", so we have to 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 []
# The original might have names in the form "currentonto:hasSimpleText".
# The returned file will have ":hasSimpleText", so we have to 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]
# 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,
# so we need to 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 = [x for x in resources_returned if x["name"] == res["name"]][0]
if res_returned.get("cardinalities"):
del res_returned["cardinalities"]
# sort both lists
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"]))
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.",
)
if __name__ == "__main__":
pytest.main([__file__])