arkindex-base-worker 0.5.0b3__py3-none-any.whl → 0.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,293 +0,0 @@
1
- import pytest
2
- from responses import matchers
3
-
4
- from arkindex.exceptions import ErrorResponse
5
- from arkindex_worker.models import Transcription
6
- from arkindex_worker.worker.entity import MissingEntityType
7
- from tests import CORPUS_ID
8
-
9
- from . import BASE_API_CALLS
10
-
11
-
12
- def test_create_entity_type_wrong_name(mock_elements_worker):
13
- with pytest.raises(
14
- AssertionError, match="name shouldn't be null and should be of type str"
15
- ):
16
- mock_elements_worker.create_entity_type(name=None)
17
-
18
- with pytest.raises(
19
- AssertionError, match="name shouldn't be null and should be of type str"
20
- ):
21
- mock_elements_worker.create_entity_type(name=1234)
22
-
23
-
24
- def test_create_entity_type_api_error(responses, mock_elements_worker):
25
- responses.add(
26
- responses.POST,
27
- "http://testserver/api/v1/entity/types/",
28
- status=418,
29
- )
30
-
31
- with pytest.raises(ErrorResponse):
32
- mock_elements_worker.create_entity_type(name="firstname")
33
-
34
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
35
- assert [
36
- (call.request.method, call.request.url) for call in responses.calls
37
- ] == BASE_API_CALLS + [("POST", "http://testserver/api/v1/entity/types/")]
38
-
39
-
40
- def test_create_entity_type_already_exists(responses, mock_elements_worker):
41
- assert mock_elements_worker.entity_types == {}
42
-
43
- responses.add(
44
- responses.POST,
45
- "http://testserver/api/v1/entity/types/",
46
- status=400,
47
- match=[
48
- matchers.json_params_matcher({"name": "firstname", "corpus": CORPUS_ID})
49
- ],
50
- )
51
- responses.add(
52
- responses.GET,
53
- f"http://testserver/api/v1/corpus/{CORPUS_ID}/entity-types/",
54
- status=200,
55
- json={
56
- "count": 1,
57
- "next": None,
58
- "results": [
59
- {"id": "lastname-id", "name": "lastname", "color": "ffd1b3"},
60
- {"id": "firstname-id", "name": "firstname", "color": "ffd1b3"},
61
- ],
62
- },
63
- )
64
-
65
- mock_elements_worker.create_entity_type(name="firstname")
66
-
67
- assert len(responses.calls) == len(BASE_API_CALLS) + 2
68
- assert [
69
- (call.request.method, call.request.url) for call in responses.calls
70
- ] == BASE_API_CALLS + [
71
- ("POST", "http://testserver/api/v1/entity/types/"),
72
- ("GET", f"http://testserver/api/v1/corpus/{CORPUS_ID}/entity-types/"),
73
- ]
74
-
75
- # Make sure the entity_types attribute has been updated
76
- assert mock_elements_worker.entity_types == {
77
- "lastname": "lastname-id",
78
- "firstname": "firstname-id",
79
- }
80
-
81
-
82
- def test_create_entity_type(responses, mock_elements_worker):
83
- assert mock_elements_worker.entity_types == {}
84
-
85
- responses.add(
86
- responses.POST,
87
- "http://testserver/api/v1/entity/types/",
88
- status=200,
89
- match=[
90
- matchers.json_params_matcher({"name": "firstname", "corpus": CORPUS_ID})
91
- ],
92
- json={
93
- "id": "firstname-id",
94
- "name": "firstname",
95
- "corpus": CORPUS_ID,
96
- "color": "ffd1b3",
97
- },
98
- )
99
-
100
- mock_elements_worker.create_entity_type(name="firstname")
101
-
102
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
103
- assert [
104
- (call.request.method, call.request.url) for call in responses.calls
105
- ] == BASE_API_CALLS + [
106
- ("POST", "http://testserver/api/v1/entity/types/"),
107
- ]
108
-
109
- # Make sure the entity_types attribute has been updated
110
- assert mock_elements_worker.entity_types == {"firstname": "firstname-id"}
111
-
112
-
113
- def test_check_required_entity_types_wrong_entity_types(mock_elements_worker):
114
- with pytest.raises(
115
- AssertionError,
116
- match="entity_types shouldn't be null and should be of type list",
117
- ):
118
- mock_elements_worker.check_required_entity_types(entity_types=None)
119
-
120
- with pytest.raises(
121
- AssertionError,
122
- match="entity_types shouldn't be null and should be of type list",
123
- ):
124
- mock_elements_worker.check_required_entity_types(entity_types=1234)
125
-
126
- with pytest.raises(
127
- AssertionError,
128
- match="Entity type at index 1 in entity_types: Should be of type str",
129
- ):
130
- mock_elements_worker.check_required_entity_types(
131
- entity_types=["firstname", 1234]
132
- )
133
-
134
-
135
- def test_check_required_entity_types_wrong_create_missing(mock_elements_worker):
136
- with pytest.raises(
137
- AssertionError,
138
- match="create_missing shouldn't be null and should be of type bool",
139
- ):
140
- mock_elements_worker.check_required_entity_types(
141
- entity_types=["firstname"], create_missing=None
142
- )
143
-
144
- with pytest.raises(
145
- AssertionError,
146
- match="create_missing shouldn't be null and should be of type bool",
147
- ):
148
- mock_elements_worker.check_required_entity_types(
149
- entity_types=["firstname"], create_missing=1234
150
- )
151
-
152
-
153
- def test_check_required_entity_types_do_not_create_missing(
154
- responses, mock_elements_worker
155
- ):
156
- # Set one entity type
157
- mock_elements_worker.entity_types = {"lastname": "lastname-id"}
158
-
159
- with pytest.raises(
160
- MissingEntityType, match="Entity type `firstname` was not in the corpus."
161
- ):
162
- mock_elements_worker.check_required_entity_types(
163
- entity_types=["lastname", "firstname"], create_missing=False
164
- )
165
-
166
- assert len(responses.calls) == len(BASE_API_CALLS)
167
- assert [
168
- (call.request.method, call.request.url) for call in responses.calls
169
- ] == BASE_API_CALLS
170
-
171
-
172
- def test_check_required_entity_types(responses, mock_elements_worker):
173
- # Set one entity type
174
- mock_elements_worker.entity_types = {"lastname": "lastname-id"}
175
-
176
- # Call to create a new entity type
177
- responses.add(
178
- responses.POST,
179
- "http://testserver/api/v1/entity/types/",
180
- status=200,
181
- match=[
182
- matchers.json_params_matcher({"name": "firstname", "corpus": CORPUS_ID})
183
- ],
184
- json={
185
- "id": "firstname-id",
186
- "name": "firstname",
187
- "corpus": CORPUS_ID,
188
- "color": "ffd1b3",
189
- },
190
- )
191
-
192
- mock_elements_worker.check_required_entity_types(
193
- entity_types=["lastname", "firstname"], create_missing=True
194
- )
195
-
196
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
197
- assert [
198
- (call.request.method, call.request.url) for call in responses.calls
199
- ] == BASE_API_CALLS + [
200
- (
201
- "POST",
202
- "http://testserver/api/v1/entity/types/",
203
- ),
204
- ]
205
-
206
- # Make sure the entity_types attribute has been updated
207
- assert mock_elements_worker.entity_types == {
208
- "lastname": "lastname-id",
209
- "firstname": "firstname-id",
210
- }
211
-
212
-
213
- def test_list_transcription_entities_deprecation(fake_dummy_worker):
214
- transcription = Transcription({"id": "fake_transcription_id"})
215
- worker_version = "worker_version_id"
216
- fake_dummy_worker.api_client.add_response(
217
- "ListTranscriptionEntities",
218
- id=transcription.id,
219
- worker_version=worker_version,
220
- response={"id": "entity_id"},
221
- )
222
- with pytest.deprecated_call(
223
- match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
224
- ):
225
- assert fake_dummy_worker.list_transcription_entities(
226
- transcription, worker_version=worker_version
227
- ) == {"id": "entity_id"}
228
-
229
- assert len(fake_dummy_worker.api_client.history) == 1
230
- assert len(fake_dummy_worker.api_client.responses) == 0
231
-
232
-
233
- def test_list_transcription_entities(fake_dummy_worker):
234
- transcription = Transcription({"id": "fake_transcription_id"})
235
- worker_run = "worker_run_id"
236
- fake_dummy_worker.api_client.add_response(
237
- "ListTranscriptionEntities",
238
- id=transcription.id,
239
- worker_run=worker_run,
240
- response={"id": "entity_id"},
241
- )
242
- assert fake_dummy_worker.list_transcription_entities(
243
- transcription, worker_run=worker_run
244
- ) == {"id": "entity_id"}
245
-
246
- assert len(fake_dummy_worker.api_client.history) == 1
247
- assert len(fake_dummy_worker.api_client.responses) == 0
248
-
249
-
250
- def test_list_corpus_entities(responses, mock_elements_worker):
251
- responses.add(
252
- responses.GET,
253
- f"http://testserver/api/v1/corpus/{CORPUS_ID}/entities/",
254
- json={
255
- "count": 1,
256
- "next": None,
257
- "results": [
258
- {
259
- "id": "fake_entity_id",
260
- }
261
- ],
262
- },
263
- )
264
-
265
- mock_elements_worker.list_corpus_entities()
266
-
267
- assert mock_elements_worker.entities == {
268
- "fake_entity_id": {
269
- "id": "fake_entity_id",
270
- }
271
- }
272
-
273
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
274
- assert [
275
- (call.request.method, call.request.url) for call in responses.calls
276
- ] == BASE_API_CALLS + [
277
- (
278
- "GET",
279
- f"http://testserver/api/v1/corpus/{CORPUS_ID}/entities/",
280
- ),
281
- ]
282
-
283
-
284
- @pytest.mark.parametrize("wrong_name", [1234, 12.5])
285
- def test_list_corpus_entities_wrong_name(mock_elements_worker, wrong_name):
286
- with pytest.raises(AssertionError, match="name should be of type str"):
287
- mock_elements_worker.list_corpus_entities(name=wrong_name)
288
-
289
-
290
- @pytest.mark.parametrize("wrong_parent", [{"id": "element_id"}, 12.5, "blabla"])
291
- def test_list_corpus_entities_wrong_parent(mock_elements_worker, wrong_parent):
292
- with pytest.raises(AssertionError, match="parent should be of type Element"):
293
- mock_elements_worker.list_corpus_entities(parent=wrong_parent)