arkindex-base-worker 0.4.0rc3__py3-none-any.whl → 0.4.0rc5__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.
Files changed (26) hide show
  1. {arkindex_base_worker-0.4.0rc3.dist-info → arkindex_base_worker-0.4.0rc5.dist-info}/METADATA +15 -14
  2. {arkindex_base_worker-0.4.0rc3.dist-info → arkindex_base_worker-0.4.0rc5.dist-info}/RECORD +24 -16
  3. {arkindex_base_worker-0.4.0rc3.dist-info → arkindex_base_worker-0.4.0rc5.dist-info}/WHEEL +1 -1
  4. arkindex_worker/cache.py +1 -1
  5. arkindex_worker/worker/__init__.py +6 -2
  6. arkindex_worker/worker/entity.py +8 -19
  7. arkindex_worker/worker/process.py +5 -0
  8. tests/test_elements_worker/{test_classifications.py → test_classification.py} +86 -0
  9. tests/test_elements_worker/test_corpus.py +31 -31
  10. tests/test_elements_worker/test_element.py +427 -0
  11. tests/test_elements_worker/test_element_create_multiple.py +715 -0
  12. tests/test_elements_worker/test_element_create_single.py +528 -0
  13. tests/test_elements_worker/test_element_list_children.py +969 -0
  14. tests/test_elements_worker/test_element_list_parents.py +530 -0
  15. tests/test_elements_worker/{test_entities.py → test_entity_create.py} +42 -245
  16. tests/test_elements_worker/test_entity_list_and_check.py +160 -0
  17. tests/test_elements_worker/test_transcription_create.py +873 -0
  18. tests/test_elements_worker/test_transcription_create_with_elements.py +951 -0
  19. tests/test_elements_worker/test_transcription_list.py +450 -0
  20. tests/test_elements_worker/test_version.py +60 -0
  21. tests/test_elements_worker/test_worker.py +525 -88
  22. tests/test_image.py +181 -198
  23. tests/test_elements_worker/test_elements.py +0 -3704
  24. tests/test_elements_worker/test_transcriptions.py +0 -2252
  25. {arkindex_base_worker-0.4.0rc3.dist-info → arkindex_base_worker-0.4.0rc5.dist-info}/LICENSE +0 -0
  26. {arkindex_base_worker-0.4.0rc3.dist-info → arkindex_base_worker-0.4.0rc5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,450 @@
1
+ from uuid import UUID
2
+
3
+ import pytest
4
+
5
+ from arkindex_worker.cache import CachedElement, CachedTranscription
6
+ from arkindex_worker.models import Element
7
+
8
+ from . import BASE_API_CALLS
9
+
10
+
11
+ def test_list_transcriptions_wrong_element(mock_elements_worker):
12
+ with pytest.raises(
13
+ AssertionError,
14
+ match="element shouldn't be null and should be an Element or CachedElement",
15
+ ):
16
+ mock_elements_worker.list_transcriptions(element=None)
17
+
18
+ with pytest.raises(
19
+ AssertionError,
20
+ match="element shouldn't be null and should be an Element or CachedElement",
21
+ ):
22
+ mock_elements_worker.list_transcriptions(element="not element type")
23
+
24
+
25
+ def test_list_transcriptions_wrong_element_type(mock_elements_worker):
26
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
27
+
28
+ with pytest.raises(AssertionError, match="element_type should be of type str"):
29
+ mock_elements_worker.list_transcriptions(
30
+ element=elt,
31
+ element_type=1234,
32
+ )
33
+
34
+
35
+ def test_list_transcriptions_wrong_recursive(mock_elements_worker):
36
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
37
+
38
+ with pytest.raises(AssertionError, match="recursive should be of type bool"):
39
+ mock_elements_worker.list_transcriptions(
40
+ element=elt,
41
+ recursive="not bool",
42
+ )
43
+
44
+
45
+ def test_list_transcriptions_wrong_worker_run(mock_elements_worker):
46
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
47
+
48
+ with pytest.raises(
49
+ AssertionError, match="worker_run should be of type str or bool"
50
+ ):
51
+ mock_elements_worker.list_transcriptions(
52
+ element=elt,
53
+ worker_run=1234,
54
+ )
55
+
56
+
57
+ def test_list_transcriptions_wrong_worker_version(mock_elements_worker):
58
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
59
+
60
+ # WARNING: pytest.deprecated_call must be placed BEFORE pytest.raises, otherwise `match` argument won't be checked
61
+ with (
62
+ pytest.deprecated_call(
63
+ match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
64
+ ),
65
+ pytest.raises(
66
+ AssertionError, match="worker_version should be of type str or bool"
67
+ ),
68
+ ):
69
+ mock_elements_worker.list_transcriptions(
70
+ element=elt,
71
+ worker_version=1234,
72
+ )
73
+
74
+
75
+ def test_list_transcriptions_wrong_bool_worker_run(mock_elements_worker):
76
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
77
+
78
+ with pytest.raises(
79
+ AssertionError, match="if of type bool, worker_run can only be set to False"
80
+ ):
81
+ mock_elements_worker.list_transcriptions(
82
+ element=elt,
83
+ worker_run=True,
84
+ )
85
+
86
+
87
+ def test_list_transcriptions_wrong_bool_worker_version(mock_elements_worker):
88
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
89
+
90
+ # WARNING: pytest.deprecated_call must be placed BEFORE pytest.raises, otherwise `match` argument won't be checked
91
+ with (
92
+ pytest.deprecated_call(
93
+ match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
94
+ ),
95
+ pytest.raises(
96
+ AssertionError,
97
+ match="if of type bool, worker_version can only be set to False",
98
+ ),
99
+ ):
100
+ mock_elements_worker.list_transcriptions(
101
+ element=elt,
102
+ worker_version=True,
103
+ )
104
+
105
+
106
+ def test_list_transcriptions_api_error(responses, mock_elements_worker):
107
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
108
+ responses.add(
109
+ responses.GET,
110
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
111
+ status=418,
112
+ )
113
+
114
+ with pytest.raises(
115
+ Exception, match="Stopping pagination as data will be incomplete"
116
+ ):
117
+ next(mock_elements_worker.list_transcriptions(element=elt))
118
+
119
+ assert len(responses.calls) == len(BASE_API_CALLS) + 5
120
+ assert [
121
+ (call.request.method, call.request.url) for call in responses.calls
122
+ ] == BASE_API_CALLS + [
123
+ # We do 5 retries
124
+ (
125
+ "GET",
126
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
127
+ ),
128
+ (
129
+ "GET",
130
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
131
+ ),
132
+ (
133
+ "GET",
134
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
135
+ ),
136
+ (
137
+ "GET",
138
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
139
+ ),
140
+ (
141
+ "GET",
142
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
143
+ ),
144
+ ]
145
+
146
+
147
+ def test_list_transcriptions(responses, mock_elements_worker):
148
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
149
+ trans = [
150
+ {
151
+ "id": "0000",
152
+ "text": "hey",
153
+ "confidence": 0.42,
154
+ "worker_version_id": "56785678-5678-5678-5678-567856785678",
155
+ "worker_run_id": "56785678-5678-5678-5678-567856785678",
156
+ "element": None,
157
+ },
158
+ {
159
+ "id": "1111",
160
+ "text": "it's",
161
+ "confidence": 0.42,
162
+ "worker_version_id": "56785678-5678-5678-5678-567856785678",
163
+ "worker_run_id": "56785678-5678-5678-5678-567856785678",
164
+ "element": None,
165
+ },
166
+ {
167
+ "id": "2222",
168
+ "text": "me",
169
+ "confidence": 0.42,
170
+ "worker_version_id": "56785678-5678-5678-5678-567856785678",
171
+ "worker_run_id": "56785678-5678-5678-5678-567856785678",
172
+ "element": None,
173
+ },
174
+ ]
175
+ responses.add(
176
+ responses.GET,
177
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
178
+ status=200,
179
+ json={
180
+ "count": 3,
181
+ "next": None,
182
+ "results": trans,
183
+ },
184
+ )
185
+
186
+ for idx, transcription in enumerate(
187
+ mock_elements_worker.list_transcriptions(element=elt)
188
+ ):
189
+ assert transcription == trans[idx]
190
+
191
+ assert len(responses.calls) == len(BASE_API_CALLS) + 1
192
+ assert [
193
+ (call.request.method, call.request.url) for call in responses.calls
194
+ ] == BASE_API_CALLS + [
195
+ (
196
+ "GET",
197
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
198
+ ),
199
+ ]
200
+
201
+
202
+ def test_list_transcriptions_manual_worker_version(responses, mock_elements_worker):
203
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
204
+ trans = [
205
+ {
206
+ "id": "0000",
207
+ "text": "hey",
208
+ "confidence": 0.42,
209
+ "worker_version_id": None,
210
+ "worker_run_id": None,
211
+ "element": None,
212
+ }
213
+ ]
214
+ responses.add(
215
+ responses.GET,
216
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_version=False",
217
+ status=200,
218
+ json={
219
+ "count": 1,
220
+ "next": None,
221
+ "results": trans,
222
+ },
223
+ )
224
+
225
+ with pytest.deprecated_call(
226
+ match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
227
+ ):
228
+ for idx, transcription in enumerate(
229
+ mock_elements_worker.list_transcriptions(element=elt, worker_version=False)
230
+ ):
231
+ assert transcription == trans[idx]
232
+
233
+ assert len(responses.calls) == len(BASE_API_CALLS) + 1
234
+ assert [
235
+ (call.request.method, call.request.url) for call in responses.calls
236
+ ] == BASE_API_CALLS + [
237
+ (
238
+ "GET",
239
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_version=False",
240
+ ),
241
+ ]
242
+
243
+
244
+ def test_list_transcriptions_manual_worker_run(responses, mock_elements_worker):
245
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
246
+ trans = [
247
+ {
248
+ "id": "0000",
249
+ "text": "hey",
250
+ "confidence": 0.42,
251
+ "worker_version_id": None,
252
+ "worker_run_id": None,
253
+ "element": None,
254
+ }
255
+ ]
256
+ responses.add(
257
+ responses.GET,
258
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_run=False",
259
+ status=200,
260
+ json={
261
+ "count": 1,
262
+ "next": None,
263
+ "results": trans,
264
+ },
265
+ )
266
+
267
+ for idx, transcription in enumerate(
268
+ mock_elements_worker.list_transcriptions(element=elt, worker_run=False)
269
+ ):
270
+ assert transcription == trans[idx]
271
+
272
+ assert len(responses.calls) == len(BASE_API_CALLS) + 1
273
+ assert [
274
+ (call.request.method, call.request.url) for call in responses.calls
275
+ ] == BASE_API_CALLS + [
276
+ (
277
+ "GET",
278
+ "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_run=False",
279
+ ),
280
+ ]
281
+
282
+
283
+ @pytest.mark.usefixtures("_mock_cached_transcriptions")
284
+ @pytest.mark.parametrize(
285
+ ("filters", "expected_ids"),
286
+ [
287
+ # Filter on element should give first and sixth transcription
288
+ (
289
+ {
290
+ "element": CachedElement(
291
+ id="11111111-1111-1111-1111-111111111111", type="page"
292
+ ),
293
+ },
294
+ (
295
+ "11111111-1111-1111-1111-111111111111",
296
+ "66666666-6666-6666-6666-666666666666",
297
+ ),
298
+ ),
299
+ # Filter on element and element_type should give first and sixth transcription
300
+ (
301
+ {
302
+ "element": CachedElement(
303
+ id="11111111-1111-1111-1111-111111111111", type="page"
304
+ ),
305
+ "element_type": "page",
306
+ },
307
+ (
308
+ "11111111-1111-1111-1111-111111111111",
309
+ "66666666-6666-6666-6666-666666666666",
310
+ ),
311
+ ),
312
+ # Filter on element and worker run should give the first transcription
313
+ (
314
+ {
315
+ "element": CachedElement(
316
+ id="11111111-1111-1111-1111-111111111111", type="page"
317
+ ),
318
+ "worker_run": "56785678-5678-5678-5678-567856785678",
319
+ },
320
+ ("11111111-1111-1111-1111-111111111111",),
321
+ ),
322
+ # Filter on element, manual worker run should give the sixth transcription
323
+ (
324
+ {
325
+ "element": CachedElement(
326
+ id="11111111-1111-1111-1111-111111111111", type="page"
327
+ ),
328
+ "worker_run": False,
329
+ },
330
+ ("66666666-6666-6666-6666-666666666666",),
331
+ ),
332
+ # Filter recursively on element should give all transcriptions inserted
333
+ (
334
+ {
335
+ "element": CachedElement(
336
+ id="11111111-1111-1111-1111-111111111111", type="page"
337
+ ),
338
+ "recursive": True,
339
+ },
340
+ (
341
+ "11111111-1111-1111-1111-111111111111",
342
+ "22222222-2222-2222-2222-222222222222",
343
+ "33333333-3333-3333-3333-333333333333",
344
+ "44444444-4444-4444-4444-444444444444",
345
+ "55555555-5555-5555-5555-555555555555",
346
+ "66666666-6666-6666-6666-666666666666",
347
+ ),
348
+ ),
349
+ # Filter recursively on element and element_type should give three transcriptions
350
+ (
351
+ {
352
+ "element": CachedElement(
353
+ id="11111111-1111-1111-1111-111111111111", type="page"
354
+ ),
355
+ "element_type": "something_else",
356
+ "recursive": True,
357
+ },
358
+ (
359
+ "22222222-2222-2222-2222-222222222222",
360
+ "44444444-4444-4444-4444-444444444444",
361
+ "55555555-5555-5555-5555-555555555555",
362
+ ),
363
+ ),
364
+ ],
365
+ )
366
+ def test_list_transcriptions_with_cache(
367
+ responses, mock_elements_worker_with_cache, filters, expected_ids
368
+ ):
369
+ # Check we have 5 elements already present in database
370
+ assert CachedTranscription.select().count() == 6
371
+
372
+ # Query database through cache
373
+ transcriptions = mock_elements_worker_with_cache.list_transcriptions(**filters)
374
+ assert transcriptions.count() == len(expected_ids)
375
+ for transcription, expected_id in zip(
376
+ transcriptions.order_by(CachedTranscription.id), expected_ids, strict=True
377
+ ):
378
+ assert transcription.id == UUID(expected_id)
379
+
380
+ # Check the worker never hits the API for elements
381
+ assert len(responses.calls) == len(BASE_API_CALLS)
382
+ assert [
383
+ (call.request.method, call.request.url) for call in responses.calls
384
+ ] == BASE_API_CALLS
385
+
386
+
387
+ @pytest.mark.usefixtures("_mock_cached_transcriptions")
388
+ @pytest.mark.parametrize(
389
+ ("filters", "expected_ids"),
390
+ [
391
+ # Filter on element and worker_version should give first transcription
392
+ (
393
+ {
394
+ "element": CachedElement(
395
+ id="11111111-1111-1111-1111-111111111111", type="page"
396
+ ),
397
+ "worker_version": "56785678-5678-5678-5678-567856785678",
398
+ },
399
+ ("11111111-1111-1111-1111-111111111111",),
400
+ ),
401
+ # Filter recursively on element and worker_version should give four transcriptions
402
+ (
403
+ {
404
+ "element": CachedElement(
405
+ id="11111111-1111-1111-1111-111111111111", type="page"
406
+ ),
407
+ "worker_version": "90129012-9012-9012-9012-901290129012",
408
+ "recursive": True,
409
+ },
410
+ (
411
+ "22222222-2222-2222-2222-222222222222",
412
+ "33333333-3333-3333-3333-333333333333",
413
+ "44444444-4444-4444-4444-444444444444",
414
+ "55555555-5555-5555-5555-555555555555",
415
+ ),
416
+ ),
417
+ # Filter on element with manually created transcription should give sixth transcription
418
+ (
419
+ {
420
+ "element": CachedElement(
421
+ id="11111111-1111-1111-1111-111111111111", type="page"
422
+ ),
423
+ "worker_version": False,
424
+ },
425
+ ("66666666-6666-6666-6666-666666666666",),
426
+ ),
427
+ ],
428
+ )
429
+ def test_list_transcriptions_with_cache_deprecation(
430
+ responses, mock_elements_worker_with_cache, filters, expected_ids
431
+ ):
432
+ # Check we have 5 elements already present in database
433
+ assert CachedTranscription.select().count() == 6
434
+
435
+ with pytest.deprecated_call(
436
+ match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
437
+ ):
438
+ # Query database through cache
439
+ transcriptions = mock_elements_worker_with_cache.list_transcriptions(**filters)
440
+ assert transcriptions.count() == len(expected_ids)
441
+ for transcription, expected_id in zip(
442
+ transcriptions.order_by(CachedTranscription.id), expected_ids, strict=True
443
+ ):
444
+ assert transcription.id == UUID(expected_id)
445
+
446
+ # Check the worker never hits the API for elements
447
+ assert len(responses.calls) == len(BASE_API_CALLS)
448
+ assert [
449
+ (call.request.method, call.request.url) for call in responses.calls
450
+ ] == BASE_API_CALLS
@@ -0,0 +1,60 @@
1
+ import pytest
2
+
3
+ TEST_VERSION_ID = "test_123"
4
+ TEST_SLUG = "some_slug"
5
+
6
+
7
+ def test_get_worker_version(fake_dummy_worker):
8
+ api_client = fake_dummy_worker.api_client
9
+
10
+ response = {"worker": {"slug": TEST_SLUG}}
11
+
12
+ api_client.add_response("RetrieveWorkerVersion", response, id=TEST_VERSION_ID)
13
+
14
+ with pytest.deprecated_call(match="WorkerVersion usage is deprecated."):
15
+ res = fake_dummy_worker.get_worker_version(TEST_VERSION_ID)
16
+
17
+ assert res == response
18
+ assert fake_dummy_worker._worker_version_cache[TEST_VERSION_ID] == response
19
+
20
+
21
+ def test_get_worker_version__uses_cache(fake_dummy_worker):
22
+ api_client = fake_dummy_worker.api_client
23
+
24
+ response = {"worker": {"slug": TEST_SLUG}}
25
+
26
+ api_client.add_response("RetrieveWorkerVersion", response, id=TEST_VERSION_ID)
27
+
28
+ with pytest.deprecated_call(match="WorkerVersion usage is deprecated."):
29
+ response_1 = fake_dummy_worker.get_worker_version(TEST_VERSION_ID)
30
+
31
+ with pytest.deprecated_call(match="WorkerVersion usage is deprecated."):
32
+ response_2 = fake_dummy_worker.get_worker_version(TEST_VERSION_ID)
33
+
34
+ assert response_1 == response
35
+ assert response_1 == response_2
36
+
37
+ # assert that only one call to the API
38
+ assert len(api_client.history) == 1
39
+ assert not api_client.responses
40
+
41
+
42
+ def test_get_worker_version_slug(mocker, fake_dummy_worker):
43
+ fake_dummy_worker.get_worker_version = mocker.MagicMock()
44
+ fake_dummy_worker.get_worker_version.return_value = {
45
+ "id": TEST_VERSION_ID,
46
+ "worker": {"slug": "mock_slug"},
47
+ }
48
+
49
+ with pytest.deprecated_call(match="WorkerVersion usage is deprecated."):
50
+ slug = fake_dummy_worker.get_worker_version_slug(TEST_VERSION_ID)
51
+ assert slug == "mock_slug"
52
+
53
+
54
+ def test_get_worker_version_slug_none(fake_dummy_worker):
55
+ # WARNING: pytest.deprecated_call must be placed BEFORE pytest.raises, otherwise `match` argument won't be checked
56
+ with (
57
+ pytest.deprecated_call(match="WorkerVersion usage is deprecated."),
58
+ pytest.raises(ValueError, match="No worker version ID"),
59
+ ):
60
+ fake_dummy_worker.get_worker_version_slug(None)