-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_check_consistency_with_ontology_high_level.py
106 lines (93 loc) · 3.36 KB
/
test_check_consistency_with_ontology_high_level.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
import re
from dataclasses import dataclass
from pathlib import Path
from test.unittests.commands.xmlupload.connection_mock import ConnectionMockBase
from typing import Any
import pytest
from lxml import etree
from dsp_tools.commands.xmlupload.check_consistency_with_ontology import do_xml_consistency_check
from dsp_tools.commands.xmlupload.ontology_client import OntologyClientLive
from dsp_tools.models.exceptions import BaseError, UserError
@dataclass
class ConnectionMockRaising(ConnectionMockBase):
def get(
self,
route: str, # noqa: ARG002 (unused-method-argument)
headers: dict[str, str] | None = None, # noqa: ARG002 (unused-method-argument)
) -> dict[str, Any]:
raise BaseError("foo")
@dataclass
class ConnectionMockWithResponses(ConnectionMockBase):
get_responses: tuple[dict[str, Any], ...] = (
{
"project": {
"ontologies": ["/testonto"],
}
},
{
"@graph": [
{
"@id": "testonto:ValidResourceClass",
"knora-api:isResourceClass": True,
}
]
},
{
"@graph": [
{
"@id": "knora-api:ValidResourceClass",
"knora-api:isResourceClass": True,
}
]
},
)
counter = 0
def get(
self,
route: str, # noqa: ARG002 (unused-method-argument)
headers: dict[str, str] | None = None, # noqa: ARG002 (unused-method-argument)
) -> dict[str, Any]:
response = self.get_responses[self.counter]
self.counter += 1
return response
def test_error_on_nonexistent_shortcode() -> None:
root = etree.parse("testdata/xml-data/test-data-minimal.xml").getroot()
con = ConnectionMockRaising()
ontology_client = OntologyClientLive(
con=con,
shortcode="9999",
default_ontology="foo",
save_location=Path("bar"),
)
with pytest.raises(UserError, match="A project with shortcode 9999 could not be found on the DSP server"):
do_xml_consistency_check(ontology_client, root)
def test_error_on_nonexistent_onto_name() -> None:
root = etree.fromstring(
'<knora shortcode="4124" default-ontology="notexistingfantasyonto">'
'<resource label="The only resource" restype=":minimalResource" id="the_only_resource"/>'
"</knora>"
)
con = ConnectionMockWithResponses()
ontology_client = OntologyClientLive(
con=con,
shortcode="4124",
default_ontology="notexistingfantasyonto",
save_location=Path("bar"),
)
expected = re.escape(
"\nSome property and/or class type(s) used in the XML are unknown.\n"
"The ontologies for your project on the server are:\n"
" - testonto\n"
" - knora-api\n\n"
"---------------------------------------\n\n"
"The following resource(s) have an invalid resource type:\n\n"
" Resource Type: ':minimalResource'\n"
" Problem: 'Unknown ontology prefix'\n"
" Resource ID(s):\n"
" - the_only_resource\n\n"
"---------------------------------------\n\n"
)
with pytest.raises(UserError, match=expected):
do_xml_consistency_check(ontology_client, root)
if __name__ == "__main__":
pytest.main([__file__])