-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_upload_stash_with_mock.py
184 lines (167 loc) · 5.72 KB
/
test_upload_stash_with_mock.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
from dataclasses import dataclass, field
from typing import Any
from uuid import uuid4
from dsp_tools.connection.connection import Connection
from dsp_tools.models.value import KnoraStandoffXml
from dsp_tools.utils.xmlupload.stash.stash_models import (
LinkValueStash,
LinkValueStashItem,
StandoffStash,
StandoffStashItem,
Stash,
)
from dsp_tools.utils.xmlupload.xmlupload import _upload_stash
# pylint: disable=unused-argument,missing-function-docstring,missing-class-docstring,too-few-public-methods
@dataclass
class ConnectionMock:
"""Mock class for Connection."""
get_responses: list[dict[str, Any]] = field(default_factory=list)
post_responses: list[dict[str, Any]] = field(default_factory=list)
put_responses: list[dict[str, Any]] = field(default_factory=list)
def get(
self,
route: str,
headers: dict[str, str] | None = None,
) -> dict[str, Any]:
return self.get_responses.pop(0)
def post(
self,
route: str,
jsondata: str | None = None,
content_type: str = "application/json",
) -> dict[str, Any]:
return self.post_responses.pop(0)
def put(
self,
route: str,
jsondata: str | None = None,
content_type: str = "application/json",
) -> dict[str, Any]:
return self.put_responses.pop(0)
def delete(
self,
route: str,
params: dict[str, Any] | None = None,
) -> dict[str, Any]:
raise AssertionError("'delete' not implemented")
def get_token(self) -> str:
raise AssertionError("'get token' not implemented in this mock")
def login(self, email: str, password: str) -> None:
raise AssertionError("'login' not implemented in this mock")
def logout(self) -> None:
raise AssertionError("'logout' not implemented in this mock")
class TestUploadLinkValueStashes:
def test_upload_link_value_stash(self) -> None:
"""Upload stashed link values (resptr), if all goes well."""
stash = Stash.make(
standoff_stash=None,
link_value_stash=LinkValueStash.make(
[
LinkValueStashItem("001", "sometype", "someprop", "002"),
],
),
)
assert stash
id2iri_mapping = {
"001": "http://www.rdfh.ch/0001/001",
"002": "http://www.rdfh.ch/0001/002",
}
con: Connection = ConnectionMock(post_responses=[{}])
nonapplied = _upload_stash(
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied is None
class TestUploadTextValueStashes:
def test_upload_text_value_stash(self) -> None:
"""Upload stashed text values (standoff), if all goes well."""
value_uuid = str(uuid4())
property_name = "someprop"
stash = Stash.make(
standoff_stash=StandoffStash.make(
[
StandoffStashItem(
"001", "sometype", value_uuid, property_name, KnoraStandoffXml("<p>some text</p>")
),
]
),
link_value_stash=None,
)
assert stash
id2iri_mapping = {
"001": "http://www.rdfh.ch/0001/001",
"002": "http://www.rdfh.ch/0001/002",
}
con: Connection = ConnectionMock(
get_responses=[
{
property_name: [
{
"@id": "http://www.rdfh.ch/0001/001/values/01",
"knora-api:textValueAsXml": "<p>not relevant</p>",
},
{
"@id": "http://www.rdfh.ch/0001/001/values/01",
"knora-api:textValueAsXml": f"<p>{value_uuid}</p>",
},
],
"@context": {},
},
],
put_responses=[{}],
)
nonapplied = _upload_stash(
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied is None
def test_not_upload_text_value_stash_if_uuid_not_on_value(self) -> None:
"""
Do not upload stashed text values (standoff), if the resource has no value containing the UUID of the stashed
text value in its text.
"""
value_uuid = str(uuid4())
property_name = "someprop"
stash = Stash.make(
standoff_stash=StandoffStash.make(
[
StandoffStashItem(
"001", "sometype", value_uuid, property_name, KnoraStandoffXml("<p>some text</p>")
),
]
),
link_value_stash=None,
)
assert stash
id2iri_mapping = {
"001": "http://www.rdfh.ch/0001/001",
"002": "http://www.rdfh.ch/0001/002",
}
con: Connection = ConnectionMock(
get_responses=[
{
property_name: [
{
"@id": "http://www.rdfh.ch/0001/001/values/01",
"knora-api:textValueAsXml": "<p>not relevant</p>",
},
],
"@context": {},
},
],
put_responses=[{}],
)
nonapplied = _upload_stash(
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied == stash