-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_list_client_live.py
244 lines (226 loc) · 8.57 KB
/
test_list_client_live.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
from dataclasses import dataclass
from test.unittests.commands.xmlupload.connection_mock import ConnectionMockBase
from typing import Any
from dsp_tools.commands.xmlupload.list_client import (
List,
ListClientLive,
ListNode,
_get_list_from_server,
_get_list_iris_from_server,
)
# pylint: disable=missing-class-docstring,missing-function-docstring
@dataclass
class ConnectionMock(ConnectionMockBase):
get_responses: list[dict[str, Any]]
def get(self, route: str, headers: dict[str, str] | None = None) -> dict[str, Any]:
return self.get_responses.pop(0)
class TestGetListNodeLookup:
"""Test the get_list_node_id_to_iri_lookup method of the ListClientLive class."""
def test_list_root_only(self) -> None:
list_iris = {"lists": [{"id": "http://www.example.org/lists#a"}]}
list_a = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [],
}
}
con = ConnectionMock([list_iris, list_a])
list_client = ListClientLive(con, "")
lookup = list_client.get_list_node_id_to_iri_lookup()
expected = {"list-a:list-a": "http://www.example.org/lists#a"}
assert lookup == expected
def test_list_with_children(self) -> None:
list_iris = {"lists": [{"id": "http://www.example.org/lists#a"}]}
list_a = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [
{
"id": "http://www.example.org/lists#a1",
"name": "node-a1",
},
{
"id": "http://www.example.org/lists#a2",
"name": "node-a2",
},
],
}
}
con = ConnectionMock([list_iris, list_a])
list_client = ListClientLive(con, "")
lookup = list_client.get_list_node_id_to_iri_lookup()
expected = {
"list-a:list-a": "http://www.example.org/lists#a",
"list-a:node-a1": "http://www.example.org/lists#a1",
"list-a:node-a2": "http://www.example.org/lists#a2",
}
assert lookup == expected
def test_multiple_lists(self) -> None:
list_iris = {
"lists": [
{"id": "http://www.example.org/lists#a"},
{"id": "http://www.example.org/lists#b"},
]
}
list_a = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [
{
"id": "http://www.example.org/lists#a1",
"name": "node-a1",
},
],
}
}
list_b = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#b",
"name": "list-b",
},
"children": [
{
"id": "http://www.example.org/lists#b1",
"name": "node-b1",
},
],
}
}
con = ConnectionMock([list_iris, list_a, list_b])
list_client = ListClientLive(con, "")
lookup = list_client.get_list_node_id_to_iri_lookup()
expected = {
"list-a:list-a": "http://www.example.org/lists#a",
"list-a:node-a1": "http://www.example.org/lists#a1",
"list-b:list-b": "http://www.example.org/lists#b",
"list-b:node-b1": "http://www.example.org/lists#b1",
}
assert lookup == expected
class TestGetListFromServer:
"""Test the _get_list_info_from_server function."""
def test_no_name_no_children(self) -> None:
list_response = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"labels": [{"value": "some label"}],
},
"children": [],
}
}
con = ConnectionMock([list_response])
result = _get_list_from_server(con, "")
expected_node = ListNode(node_iri="http://www.example.org/lists#a", node_name="some label")
expected = List(
root_iri="http://www.example.org/lists#a",
list_name="some label",
nodes=[expected_node],
)
assert result == expected
def test_name_no_children(self) -> None:
list_response = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [],
}
}
con = ConnectionMock([list_response])
result = _get_list_from_server(con, "")
expected_node = ListNode(node_iri="http://www.example.org/lists#a", node_name="list-a")
expected = List(
root_iri="http://www.example.org/lists#a",
list_name="list-a",
nodes=[expected_node],
)
assert result == expected
def test_with_children(self) -> None:
list_response = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [
{
"id": "http://www.example.org/lists#a1",
"name": "node-a1",
},
{
"id": "http://www.example.org/lists#a2",
"name": "node-a2",
},
],
}
}
con = ConnectionMock([list_response])
result = _get_list_from_server(con, "")
expected_node_root = ListNode(node_iri="http://www.example.org/lists#a", node_name="list-a")
expected_node_1 = ListNode(node_iri="http://www.example.org/lists#a1", node_name="node-a1")
expected_node_2 = ListNode(node_iri="http://www.example.org/lists#a2", node_name="node-a2")
expected = List(
root_iri="http://www.example.org/lists#a",
list_name="list-a",
nodes=[expected_node_root, expected_node_1, expected_node_2],
)
assert result == expected
def test_multiple_levels(self) -> None:
list_response = {
"list": {
"listinfo": {
"id": "http://www.example.org/lists#a",
"name": "list-a",
},
"children": [
{
"id": "http://www.example.org/lists#a1",
"name": "node-a1",
"children": [
{
"id": "http://www.example.org/lists#a1.1",
"name": "node-a1.1",
},
{
"id": "http://www.example.org/lists#a1.2",
"name": "node-a1.2",
},
],
}
],
}
}
con = ConnectionMock([list_response])
result = _get_list_from_server(con, "")
expected_node_root = ListNode(node_iri="http://www.example.org/lists#a", node_name="list-a")
expected_node_1 = ListNode(node_iri="http://www.example.org/lists#a1", node_name="node-a1")
expected_node_1_1 = ListNode(node_iri="http://www.example.org/lists#a1.1", node_name="node-a1.1")
expected_node_1_2 = ListNode(node_iri="http://www.example.org/lists#a1.2", node_name="node-a1.2")
expected = List(
root_iri="http://www.example.org/lists#a",
list_name="list-a",
nodes=[expected_node_root, expected_node_1, expected_node_1_1, expected_node_1_2],
)
assert result == expected
def test_get_list_iris_from_server() -> None:
list_iris = {
"lists": [
{"id": "http://www.example.org/lists#a"},
{"id": "http://www.example.org/lists#b"},
]
}
con = ConnectionMock([list_iris])
result = _get_list_iris_from_server(con, "")
expected = ["http://www.example.org/lists#a", "http://www.example.org/lists#b"]
assert result == expected