arkindex-base-worker 0.3.7rc4__py3-none-any.whl → 0.5.0a1__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 (60) hide show
  1. {arkindex_base_worker-0.3.7rc4.dist-info → arkindex_base_worker-0.5.0a1.dist-info}/METADATA +18 -19
  2. arkindex_base_worker-0.5.0a1.dist-info/RECORD +61 -0
  3. {arkindex_base_worker-0.3.7rc4.dist-info → arkindex_base_worker-0.5.0a1.dist-info}/WHEEL +1 -1
  4. {arkindex_base_worker-0.3.7rc4.dist-info → arkindex_base_worker-0.5.0a1.dist-info}/top_level.txt +2 -0
  5. arkindex_worker/cache.py +1 -1
  6. arkindex_worker/image.py +167 -2
  7. arkindex_worker/models.py +18 -0
  8. arkindex_worker/utils.py +98 -4
  9. arkindex_worker/worker/__init__.py +117 -218
  10. arkindex_worker/worker/base.py +39 -46
  11. arkindex_worker/worker/classification.py +45 -29
  12. arkindex_worker/worker/corpus.py +86 -0
  13. arkindex_worker/worker/dataset.py +89 -26
  14. arkindex_worker/worker/element.py +352 -91
  15. arkindex_worker/worker/entity.py +13 -11
  16. arkindex_worker/worker/image.py +21 -0
  17. arkindex_worker/worker/metadata.py +26 -16
  18. arkindex_worker/worker/process.py +92 -0
  19. arkindex_worker/worker/task.py +5 -4
  20. arkindex_worker/worker/training.py +25 -10
  21. arkindex_worker/worker/transcription.py +89 -68
  22. arkindex_worker/worker/version.py +3 -1
  23. hooks/pre_gen_project.py +3 -0
  24. tests/__init__.py +8 -0
  25. tests/conftest.py +47 -58
  26. tests/test_base_worker.py +212 -12
  27. tests/test_dataset_worker.py +294 -437
  28. tests/test_elements_worker/{test_classifications.py → test_classification.py} +313 -200
  29. tests/test_elements_worker/test_cli.py +3 -11
  30. tests/test_elements_worker/test_corpus.py +168 -0
  31. tests/test_elements_worker/test_dataset.py +106 -157
  32. tests/test_elements_worker/test_element.py +427 -0
  33. tests/test_elements_worker/test_element_create_multiple.py +715 -0
  34. tests/test_elements_worker/test_element_create_single.py +528 -0
  35. tests/test_elements_worker/test_element_list_children.py +969 -0
  36. tests/test_elements_worker/test_element_list_parents.py +530 -0
  37. tests/test_elements_worker/{test_entities.py → test_entity_create.py} +37 -195
  38. tests/test_elements_worker/test_entity_list_and_check.py +160 -0
  39. tests/test_elements_worker/test_image.py +66 -0
  40. tests/test_elements_worker/test_metadata.py +252 -161
  41. tests/test_elements_worker/test_process.py +89 -0
  42. tests/test_elements_worker/test_task.py +8 -18
  43. tests/test_elements_worker/test_training.py +17 -8
  44. tests/test_elements_worker/test_transcription_create.py +873 -0
  45. tests/test_elements_worker/test_transcription_create_with_elements.py +951 -0
  46. tests/test_elements_worker/test_transcription_list.py +450 -0
  47. tests/test_elements_worker/test_version.py +60 -0
  48. tests/test_elements_worker/test_worker.py +578 -293
  49. tests/test_image.py +542 -209
  50. tests/test_merge.py +1 -2
  51. tests/test_utils.py +89 -4
  52. worker-demo/tests/__init__.py +0 -0
  53. worker-demo/tests/conftest.py +32 -0
  54. worker-demo/tests/test_worker.py +12 -0
  55. worker-demo/worker_demo/__init__.py +6 -0
  56. worker-demo/worker_demo/worker.py +19 -0
  57. arkindex_base_worker-0.3.7rc4.dist-info/RECORD +0 -41
  58. tests/test_elements_worker/test_elements.py +0 -2713
  59. tests/test_elements_worker/test_transcriptions.py +0 -2119
  60. {arkindex_base_worker-0.3.7rc4.dist-info → arkindex_base_worker-0.5.0a1.dist-info}/LICENSE +0 -0
@@ -1,2119 +0,0 @@
1
- import json
2
- import re
3
- from uuid import UUID
4
-
5
- import pytest
6
- from apistar.exceptions import ErrorResponse
7
- from playhouse.shortcuts import model_to_dict
8
-
9
- from arkindex_worker.cache import CachedElement, CachedTranscription
10
- from arkindex_worker.models import Element
11
- from arkindex_worker.worker.transcription import TextOrientation
12
-
13
- from . import BASE_API_CALLS
14
-
15
- TRANSCRIPTIONS_SAMPLE = [
16
- {
17
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
18
- "confidence": 0.5,
19
- "text": "The",
20
- },
21
- {
22
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
23
- "confidence": 0.75,
24
- "text": "first",
25
- "element_confidence": 0.75,
26
- },
27
- {
28
- "polygon": [[1000, 300], [1200, 300], [1200, 500], [1000, 500]],
29
- "confidence": 0.9,
30
- "text": "line",
31
- },
32
- ]
33
-
34
-
35
- def test_create_transcription_wrong_element(mock_elements_worker):
36
- with pytest.raises(
37
- AssertionError,
38
- match="element shouldn't be null and should be an Element or CachedElement",
39
- ):
40
- mock_elements_worker.create_transcription(
41
- element=None,
42
- text="i am a line",
43
- confidence=0.42,
44
- )
45
-
46
- with pytest.raises(
47
- AssertionError,
48
- match="element shouldn't be null and should be an Element or CachedElement",
49
- ):
50
- mock_elements_worker.create_transcription(
51
- element="not element type",
52
- text="i am a line",
53
- confidence=0.42,
54
- )
55
-
56
-
57
- def test_create_transcription_wrong_text(mock_elements_worker):
58
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
59
-
60
- with pytest.raises(
61
- AssertionError, match="text shouldn't be null and should be of type str"
62
- ):
63
- mock_elements_worker.create_transcription(
64
- element=elt,
65
- text=None,
66
- confidence=0.42,
67
- )
68
-
69
- with pytest.raises(
70
- AssertionError, match="text shouldn't be null and should be of type str"
71
- ):
72
- mock_elements_worker.create_transcription(
73
- element=elt,
74
- text=1234,
75
- confidence=0.42,
76
- )
77
-
78
-
79
- def test_create_transcription_wrong_confidence(mock_elements_worker):
80
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
81
-
82
- with pytest.raises(
83
- AssertionError,
84
- match=re.escape(
85
- "confidence shouldn't be null and should be a float in [0..1] range"
86
- ),
87
- ):
88
- mock_elements_worker.create_transcription(
89
- element=elt,
90
- text="i am a line",
91
- confidence=None,
92
- )
93
-
94
- with pytest.raises(
95
- AssertionError,
96
- match=re.escape(
97
- "confidence shouldn't be null and should be a float in [0..1] range"
98
- ),
99
- ):
100
- mock_elements_worker.create_transcription(
101
- element=elt,
102
- text="i am a line",
103
- confidence="wrong confidence",
104
- )
105
-
106
- with pytest.raises(
107
- AssertionError,
108
- match=re.escape(
109
- "confidence shouldn't be null and should be a float in [0..1] range"
110
- ),
111
- ):
112
- mock_elements_worker.create_transcription(
113
- element=elt,
114
- text="i am a line",
115
- confidence=0,
116
- )
117
-
118
- with pytest.raises(
119
- AssertionError,
120
- match=re.escape(
121
- "confidence shouldn't be null and should be a float in [0..1] range"
122
- ),
123
- ):
124
- mock_elements_worker.create_transcription(
125
- element=elt,
126
- text="i am a line",
127
- confidence=2.00,
128
- )
129
-
130
-
131
- def test_create_transcription_default_orientation(responses, mock_elements_worker):
132
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
133
- responses.add(
134
- responses.POST,
135
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
136
- status=200,
137
- json={
138
- "id": "56785678-5678-5678-5678-567856785678",
139
- "text": "Animula vagula blandula",
140
- "confidence": 0.42,
141
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
142
- },
143
- )
144
- mock_elements_worker.create_transcription(
145
- element=elt,
146
- text="Animula vagula blandula",
147
- confidence=0.42,
148
- )
149
- assert json.loads(responses.calls[-1].request.body) == {
150
- "text": "Animula vagula blandula",
151
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
152
- "confidence": 0.42,
153
- "orientation": "horizontal-lr",
154
- }
155
-
156
-
157
- def test_create_transcription_orientation(responses, mock_elements_worker):
158
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
159
- responses.add(
160
- responses.POST,
161
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
162
- status=200,
163
- json={
164
- "id": "56785678-5678-5678-5678-567856785678",
165
- "text": "Animula vagula blandula",
166
- "confidence": 0.42,
167
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
168
- },
169
- )
170
- mock_elements_worker.create_transcription(
171
- element=elt,
172
- text="Animula vagula blandula",
173
- orientation=TextOrientation.VerticalLeftToRight,
174
- confidence=0.42,
175
- )
176
- assert json.loads(responses.calls[-1].request.body) == {
177
- "text": "Animula vagula blandula",
178
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
179
- "confidence": 0.42,
180
- "orientation": "vertical-lr",
181
- }
182
-
183
-
184
- def test_create_transcription_wrong_orientation(mock_elements_worker):
185
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
186
- with pytest.raises(
187
- AssertionError,
188
- match="orientation shouldn't be null and should be of type TextOrientation",
189
- ):
190
- mock_elements_worker.create_transcription(
191
- element=elt,
192
- text="Animula vagula blandula",
193
- confidence=0.26,
194
- orientation="elliptical",
195
- )
196
-
197
-
198
- def test_create_transcription_api_error(responses, mock_elements_worker):
199
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
200
- responses.add(
201
- responses.POST,
202
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
203
- status=500,
204
- )
205
-
206
- with pytest.raises(ErrorResponse):
207
- mock_elements_worker.create_transcription(
208
- element=elt,
209
- text="i am a line",
210
- confidence=0.42,
211
- )
212
-
213
- assert len(responses.calls) == len(BASE_API_CALLS) + 5
214
- assert [
215
- (call.request.method, call.request.url) for call in responses.calls
216
- ] == BASE_API_CALLS + [
217
- # We retry 5 times the API call
218
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
219
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
220
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
221
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
222
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
223
- ]
224
-
225
-
226
- def test_create_transcription(responses, mock_elements_worker):
227
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
228
- responses.add(
229
- responses.POST,
230
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
231
- status=200,
232
- json={
233
- "id": "56785678-5678-5678-5678-567856785678",
234
- "text": "i am a line",
235
- "confidence": 0.42,
236
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
237
- },
238
- )
239
-
240
- mock_elements_worker.create_transcription(
241
- element=elt,
242
- text="i am a line",
243
- confidence=0.42,
244
- )
245
-
246
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
247
- assert [
248
- (call.request.method, call.request.url) for call in responses.calls
249
- ] == BASE_API_CALLS + [
250
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
251
- ]
252
-
253
- assert json.loads(responses.calls[-1].request.body) == {
254
- "text": "i am a line",
255
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
256
- "confidence": 0.42,
257
- "orientation": "horizontal-lr",
258
- }
259
-
260
-
261
- def test_create_transcription_with_cache(responses, mock_elements_worker_with_cache):
262
- elt = CachedElement.create(id="12341234-1234-1234-1234-123412341234", type="thing")
263
-
264
- responses.add(
265
- responses.POST,
266
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
267
- status=200,
268
- json={
269
- "id": "56785678-5678-5678-5678-567856785678",
270
- "text": "i am a line",
271
- "confidence": 0.42,
272
- "orientation": "horizontal-lr",
273
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
274
- },
275
- )
276
-
277
- mock_elements_worker_with_cache.create_transcription(
278
- element=elt,
279
- text="i am a line",
280
- confidence=0.42,
281
- )
282
-
283
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
284
- assert [
285
- (call.request.method, call.request.url) for call in responses.calls
286
- ] == BASE_API_CALLS + [
287
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcription/"),
288
- ]
289
-
290
- assert json.loads(responses.calls[-1].request.body) == {
291
- "text": "i am a line",
292
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
293
- "orientation": "horizontal-lr",
294
- "confidence": 0.42,
295
- }
296
-
297
- # Check that created transcription was properly stored in SQLite cache
298
- assert list(CachedTranscription.select()) == [
299
- CachedTranscription(
300
- id=UUID("56785678-5678-5678-5678-567856785678"),
301
- element_id=UUID(elt.id),
302
- text="i am a line",
303
- confidence=0.42,
304
- orientation=TextOrientation.HorizontalLeftToRight,
305
- worker_version_id=None,
306
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
307
- )
308
- ]
309
-
310
-
311
- def test_create_transcription_orientation_with_cache(
312
- responses, mock_elements_worker_with_cache
313
- ):
314
- elt = CachedElement.create(id="12341234-1234-1234-1234-123412341234", type="thing")
315
- responses.add(
316
- responses.POST,
317
- f"http://testserver/api/v1/element/{elt.id}/transcription/",
318
- status=200,
319
- json={
320
- "id": "56785678-5678-5678-5678-567856785678",
321
- "text": "Animula vagula blandula",
322
- "confidence": 0.42,
323
- "orientation": "vertical-lr",
324
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
325
- },
326
- )
327
- mock_elements_worker_with_cache.create_transcription(
328
- element=elt,
329
- text="Animula vagula blandula",
330
- orientation=TextOrientation.VerticalLeftToRight,
331
- confidence=0.42,
332
- )
333
- assert json.loads(responses.calls[-1].request.body) == {
334
- "text": "Animula vagula blandula",
335
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
336
- "orientation": "vertical-lr",
337
- "confidence": 0.42,
338
- }
339
- # Check that the text orientation was properly stored in SQLite cache
340
- assert list(map(model_to_dict, CachedTranscription.select())) == [
341
- {
342
- "id": UUID("56785678-5678-5678-5678-567856785678"),
343
- "element": {
344
- "id": UUID("12341234-1234-1234-1234-123412341234"),
345
- "parent_id": None,
346
- "type": "thing",
347
- "image": None,
348
- "polygon": None,
349
- "rotation_angle": 0,
350
- "mirrored": False,
351
- "initial": False,
352
- "worker_version_id": None,
353
- "worker_run_id": None,
354
- "confidence": None,
355
- },
356
- "text": "Animula vagula blandula",
357
- "confidence": 0.42,
358
- "orientation": TextOrientation.VerticalLeftToRight.value,
359
- "worker_version_id": None,
360
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
361
- }
362
- ]
363
-
364
-
365
- def test_create_transcriptions_wrong_transcriptions(mock_elements_worker):
366
- with pytest.raises(
367
- AssertionError,
368
- match="transcriptions shouldn't be null and should be of type list",
369
- ):
370
- mock_elements_worker.create_transcriptions(
371
- transcriptions=None,
372
- )
373
-
374
- with pytest.raises(
375
- AssertionError,
376
- match="transcriptions shouldn't be null and should be of type list",
377
- ):
378
- mock_elements_worker.create_transcriptions(
379
- transcriptions=1234,
380
- )
381
-
382
- with pytest.raises(
383
- AssertionError,
384
- match="Transcription at index 1 in transcriptions: element_id shouldn't be null and should be of type str",
385
- ):
386
- mock_elements_worker.create_transcriptions(
387
- transcriptions=[
388
- {
389
- "element_id": "11111111-1111-1111-1111-111111111111",
390
- "text": "The",
391
- "confidence": 0.75,
392
- },
393
- {
394
- "text": "word",
395
- "confidence": 0.5,
396
- },
397
- ],
398
- )
399
-
400
- with pytest.raises(
401
- AssertionError,
402
- match="Transcription at index 1 in transcriptions: element_id shouldn't be null and should be of type str",
403
- ):
404
- mock_elements_worker.create_transcriptions(
405
- transcriptions=[
406
- {
407
- "element_id": "11111111-1111-1111-1111-111111111111",
408
- "text": "The",
409
- "confidence": 0.75,
410
- },
411
- {
412
- "element_id": None,
413
- "text": "word",
414
- "confidence": 0.5,
415
- },
416
- ],
417
- )
418
-
419
- with pytest.raises(
420
- AssertionError,
421
- match="Transcription at index 1 in transcriptions: element_id shouldn't be null and should be of type str",
422
- ):
423
- mock_elements_worker.create_transcriptions(
424
- transcriptions=[
425
- {
426
- "element_id": "11111111-1111-1111-1111-111111111111",
427
- "text": "The",
428
- "confidence": 0.75,
429
- },
430
- {
431
- "element_id": 1234,
432
- "text": "word",
433
- "confidence": 0.5,
434
- },
435
- ],
436
- )
437
-
438
- with pytest.raises(
439
- AssertionError,
440
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
441
- ):
442
- mock_elements_worker.create_transcriptions(
443
- transcriptions=[
444
- {
445
- "element_id": "11111111-1111-1111-1111-111111111111",
446
- "text": "The",
447
- "confidence": 0.75,
448
- },
449
- {
450
- "element_id": "11111111-1111-1111-1111-111111111111",
451
- "confidence": 0.5,
452
- },
453
- ],
454
- )
455
-
456
- with pytest.raises(
457
- AssertionError,
458
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
459
- ):
460
- mock_elements_worker.create_transcriptions(
461
- transcriptions=[
462
- {
463
- "element_id": "11111111-1111-1111-1111-111111111111",
464
- "text": "The",
465
- "confidence": 0.75,
466
- },
467
- {
468
- "element_id": "11111111-1111-1111-1111-111111111111",
469
- "text": None,
470
- "confidence": 0.5,
471
- },
472
- ],
473
- )
474
-
475
- with pytest.raises(
476
- AssertionError,
477
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
478
- ):
479
- mock_elements_worker.create_transcriptions(
480
- transcriptions=[
481
- {
482
- "element_id": "11111111-1111-1111-1111-111111111111",
483
- "text": "The",
484
- "confidence": 0.75,
485
- },
486
- {
487
- "element_id": "11111111-1111-1111-1111-111111111111",
488
- "text": 1234,
489
- "confidence": 0.5,
490
- },
491
- ],
492
- )
493
-
494
- with pytest.raises(
495
- AssertionError,
496
- match=re.escape(
497
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
498
- ),
499
- ):
500
- mock_elements_worker.create_transcriptions(
501
- transcriptions=[
502
- {
503
- "element_id": "11111111-1111-1111-1111-111111111111",
504
- "text": "The",
505
- "confidence": 0.75,
506
- },
507
- {
508
- "element_id": "11111111-1111-1111-1111-111111111111",
509
- "text": "word",
510
- },
511
- ],
512
- )
513
-
514
- with pytest.raises(
515
- AssertionError,
516
- match=re.escape(
517
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
518
- ),
519
- ):
520
- mock_elements_worker.create_transcriptions(
521
- transcriptions=[
522
- {
523
- "element_id": "11111111-1111-1111-1111-111111111111",
524
- "text": "The",
525
- "confidence": 0.75,
526
- },
527
- {
528
- "element_id": "11111111-1111-1111-1111-111111111111",
529
- "text": "word",
530
- "confidence": None,
531
- },
532
- ],
533
- )
534
-
535
- with pytest.raises(
536
- AssertionError,
537
- match=re.escape(
538
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
539
- ),
540
- ):
541
- mock_elements_worker.create_transcriptions(
542
- transcriptions=[
543
- {
544
- "element_id": "11111111-1111-1111-1111-111111111111",
545
- "text": "The",
546
- "confidence": 0.75,
547
- },
548
- {
549
- "element_id": "11111111-1111-1111-1111-111111111111",
550
- "text": "word",
551
- "confidence": "a wrong confidence",
552
- },
553
- ],
554
- )
555
-
556
- with pytest.raises(
557
- AssertionError,
558
- match=re.escape(
559
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
560
- ),
561
- ):
562
- mock_elements_worker.create_transcriptions(
563
- transcriptions=[
564
- {
565
- "element_id": "11111111-1111-1111-1111-111111111111",
566
- "text": "The",
567
- "confidence": 0.75,
568
- },
569
- {
570
- "element_id": "11111111-1111-1111-1111-111111111111",
571
- "text": "word",
572
- "confidence": 0,
573
- },
574
- ],
575
- )
576
-
577
- with pytest.raises(
578
- AssertionError,
579
- match=re.escape(
580
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
581
- ),
582
- ):
583
- mock_elements_worker.create_transcriptions(
584
- transcriptions=[
585
- {
586
- "element_id": "11111111-1111-1111-1111-111111111111",
587
- "text": "The",
588
- "confidence": 0.75,
589
- },
590
- {
591
- "element_id": "11111111-1111-1111-1111-111111111111",
592
- "text": "word",
593
- "confidence": 2.00,
594
- },
595
- ],
596
- )
597
-
598
- with pytest.raises(
599
- AssertionError,
600
- match="Transcription at index 1 in transcriptions: orientation shouldn't be null and should be of type TextOrientation",
601
- ):
602
- mock_elements_worker.create_transcriptions(
603
- transcriptions=[
604
- {
605
- "element_id": "11111111-1111-1111-1111-111111111111",
606
- "text": "The",
607
- "confidence": 0.75,
608
- },
609
- {
610
- "element_id": "11111111-1111-1111-1111-111111111111",
611
- "text": "word",
612
- "confidence": 0.28,
613
- "orientation": "wobble",
614
- },
615
- ],
616
- )
617
-
618
-
619
- def test_create_transcriptions_api_error(responses, mock_elements_worker):
620
- responses.add(
621
- responses.POST,
622
- "http://testserver/api/v1/transcription/bulk/",
623
- status=500,
624
- )
625
- trans = [
626
- {
627
- "element_id": "11111111-1111-1111-1111-111111111111",
628
- "text": "The",
629
- "confidence": 0.75,
630
- },
631
- {
632
- "element_id": "11111111-1111-1111-1111-111111111111",
633
- "text": "word",
634
- "confidence": 0.42,
635
- },
636
- ]
637
-
638
- with pytest.raises(ErrorResponse):
639
- mock_elements_worker.create_transcriptions(transcriptions=trans)
640
-
641
- assert len(responses.calls) == len(BASE_API_CALLS) + 5
642
- assert [
643
- (call.request.method, call.request.url) for call in responses.calls
644
- ] == BASE_API_CALLS + [
645
- # We retry 5 times the API call
646
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
647
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
648
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
649
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
650
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
651
- ]
652
-
653
-
654
- def test_create_transcriptions(responses, mock_elements_worker_with_cache):
655
- CachedElement.create(id="11111111-1111-1111-1111-111111111111", type="thing")
656
- trans = [
657
- {
658
- "element_id": "11111111-1111-1111-1111-111111111111",
659
- "text": "The",
660
- "confidence": 0.75,
661
- },
662
- {
663
- "element_id": "11111111-1111-1111-1111-111111111111",
664
- "text": "word",
665
- "confidence": 0.42,
666
- },
667
- ]
668
-
669
- responses.add(
670
- responses.POST,
671
- "http://testserver/api/v1/transcription/bulk/",
672
- status=200,
673
- json={
674
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
675
- "transcriptions": [
676
- {
677
- "id": "00000000-0000-0000-0000-000000000000",
678
- "element_id": "11111111-1111-1111-1111-111111111111",
679
- "text": "The",
680
- "orientation": "horizontal-lr",
681
- "confidence": 0.75,
682
- },
683
- {
684
- "id": "11111111-1111-1111-1111-111111111111",
685
- "element_id": "11111111-1111-1111-1111-111111111111",
686
- "text": "word",
687
- "orientation": "horizontal-lr",
688
- "confidence": 0.42,
689
- },
690
- ],
691
- },
692
- )
693
-
694
- mock_elements_worker_with_cache.create_transcriptions(
695
- transcriptions=trans,
696
- )
697
-
698
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
699
- assert [
700
- (call.request.method, call.request.url) for call in responses.calls
701
- ] == BASE_API_CALLS + [
702
- ("POST", "http://testserver/api/v1/transcription/bulk/"),
703
- ]
704
-
705
- assert json.loads(responses.calls[-1].request.body) == {
706
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
707
- "transcriptions": [
708
- {
709
- "element_id": "11111111-1111-1111-1111-111111111111",
710
- "text": "The",
711
- "confidence": 0.75,
712
- "orientation": TextOrientation.HorizontalLeftToRight.value,
713
- },
714
- {
715
- "element_id": "11111111-1111-1111-1111-111111111111",
716
- "text": "word",
717
- "confidence": 0.42,
718
- "orientation": TextOrientation.HorizontalLeftToRight.value,
719
- },
720
- ],
721
- }
722
-
723
- # Check that created transcriptions were properly stored in SQLite cache
724
- assert list(CachedTranscription.select()) == [
725
- CachedTranscription(
726
- id=UUID("00000000-0000-0000-0000-000000000000"),
727
- element_id=UUID("11111111-1111-1111-1111-111111111111"),
728
- text="The",
729
- confidence=0.75,
730
- orientation=TextOrientation.HorizontalLeftToRight,
731
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
732
- ),
733
- CachedTranscription(
734
- id=UUID("11111111-1111-1111-1111-111111111111"),
735
- element_id=UUID("11111111-1111-1111-1111-111111111111"),
736
- text="word",
737
- confidence=0.42,
738
- orientation=TextOrientation.HorizontalLeftToRight,
739
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
740
- ),
741
- ]
742
-
743
-
744
- def test_create_transcriptions_orientation(responses, mock_elements_worker_with_cache):
745
- CachedElement.create(id="11111111-1111-1111-1111-111111111111", type="thing")
746
- trans = [
747
- {
748
- "element_id": "11111111-1111-1111-1111-111111111111",
749
- "text": "Animula vagula blandula",
750
- "confidence": 0.12,
751
- "orientation": TextOrientation.HorizontalRightToLeft,
752
- },
753
- {
754
- "element_id": "11111111-1111-1111-1111-111111111111",
755
- "text": "Hospes comesque corporis",
756
- "confidence": 0.21,
757
- "orientation": TextOrientation.VerticalLeftToRight,
758
- },
759
- ]
760
-
761
- responses.add(
762
- responses.POST,
763
- "http://testserver/api/v1/transcription/bulk/",
764
- status=200,
765
- json={
766
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
767
- "transcriptions": [
768
- {
769
- "id": "00000000-0000-0000-0000-000000000000",
770
- "element_id": "11111111-1111-1111-1111-111111111111",
771
- "text": "Animula vagula blandula",
772
- "orientation": "horizontal-rl",
773
- "confidence": 0.12,
774
- },
775
- {
776
- "id": "11111111-1111-1111-1111-111111111111",
777
- "element_id": "11111111-1111-1111-1111-111111111111",
778
- "text": "Hospes comesque corporis",
779
- "orientation": "vertical-lr",
780
- "confidence": 0.21,
781
- },
782
- ],
783
- },
784
- )
785
-
786
- mock_elements_worker_with_cache.create_transcriptions(
787
- transcriptions=trans,
788
- )
789
-
790
- assert json.loads(responses.calls[-1].request.body) == {
791
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
792
- "transcriptions": [
793
- {
794
- "element_id": "11111111-1111-1111-1111-111111111111",
795
- "text": "Animula vagula blandula",
796
- "confidence": 0.12,
797
- "orientation": TextOrientation.HorizontalRightToLeft.value,
798
- },
799
- {
800
- "element_id": "11111111-1111-1111-1111-111111111111",
801
- "text": "Hospes comesque corporis",
802
- "confidence": 0.21,
803
- "orientation": TextOrientation.VerticalLeftToRight.value,
804
- },
805
- ],
806
- }
807
-
808
- # Check that oriented transcriptions were properly stored in SQLite cache
809
- assert list(map(model_to_dict, CachedTranscription.select())) == [
810
- {
811
- "id": UUID("00000000-0000-0000-0000-000000000000"),
812
- "element": {
813
- "id": UUID("11111111-1111-1111-1111-111111111111"),
814
- "parent_id": None,
815
- "type": "thing",
816
- "image": None,
817
- "polygon": None,
818
- "rotation_angle": 0,
819
- "mirrored": False,
820
- "initial": False,
821
- "worker_version_id": None,
822
- "worker_run_id": None,
823
- "confidence": None,
824
- },
825
- "text": "Animula vagula blandula",
826
- "confidence": 0.12,
827
- "orientation": TextOrientation.HorizontalRightToLeft.value,
828
- "worker_version_id": None,
829
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
830
- },
831
- {
832
- "id": UUID("11111111-1111-1111-1111-111111111111"),
833
- "element": {
834
- "id": UUID("11111111-1111-1111-1111-111111111111"),
835
- "parent_id": None,
836
- "type": "thing",
837
- "image": None,
838
- "polygon": None,
839
- "rotation_angle": 0,
840
- "mirrored": False,
841
- "initial": False,
842
- "worker_version_id": None,
843
- "worker_run_id": None,
844
- "confidence": None,
845
- },
846
- "text": "Hospes comesque corporis",
847
- "confidence": 0.21,
848
- "orientation": TextOrientation.VerticalLeftToRight.value,
849
- "worker_version_id": None,
850
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
851
- },
852
- ]
853
-
854
-
855
- def test_create_element_transcriptions_wrong_element(mock_elements_worker):
856
- with pytest.raises(
857
- AssertionError,
858
- match="element shouldn't be null and should be an Element or CachedElement",
859
- ):
860
- mock_elements_worker.create_element_transcriptions(
861
- element=None,
862
- sub_element_type="page",
863
- transcriptions=TRANSCRIPTIONS_SAMPLE,
864
- )
865
-
866
- with pytest.raises(
867
- AssertionError,
868
- match="element shouldn't be null and should be an Element or CachedElement",
869
- ):
870
- mock_elements_worker.create_element_transcriptions(
871
- element="not element type",
872
- sub_element_type="page",
873
- transcriptions=TRANSCRIPTIONS_SAMPLE,
874
- )
875
-
876
-
877
- def test_create_element_transcriptions_wrong_sub_element_type(mock_elements_worker):
878
- elt = Element({"zone": None})
879
-
880
- with pytest.raises(
881
- AssertionError,
882
- match="sub_element_type shouldn't be null and should be of type str",
883
- ):
884
- mock_elements_worker.create_element_transcriptions(
885
- element=elt,
886
- sub_element_type=None,
887
- transcriptions=TRANSCRIPTIONS_SAMPLE,
888
- )
889
-
890
- with pytest.raises(
891
- AssertionError,
892
- match="sub_element_type shouldn't be null and should be of type str",
893
- ):
894
- mock_elements_worker.create_element_transcriptions(
895
- element=elt,
896
- sub_element_type=1234,
897
- transcriptions=TRANSCRIPTIONS_SAMPLE,
898
- )
899
-
900
-
901
- def test_create_element_transcriptions_wrong_transcriptions(mock_elements_worker):
902
- elt = Element({"zone": None})
903
-
904
- with pytest.raises(
905
- AssertionError,
906
- match="transcriptions shouldn't be null and should be of type list",
907
- ):
908
- mock_elements_worker.create_element_transcriptions(
909
- element=elt,
910
- sub_element_type="page",
911
- transcriptions=None,
912
- )
913
-
914
- with pytest.raises(
915
- AssertionError,
916
- match="transcriptions shouldn't be null and should be of type list",
917
- ):
918
- mock_elements_worker.create_element_transcriptions(
919
- element=elt,
920
- sub_element_type="page",
921
- transcriptions=1234,
922
- )
923
-
924
- with pytest.raises(
925
- AssertionError,
926
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
927
- ):
928
- mock_elements_worker.create_element_transcriptions(
929
- element=elt,
930
- sub_element_type="page",
931
- transcriptions=[
932
- {
933
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
934
- "confidence": 0.75,
935
- "text": "The",
936
- },
937
- {
938
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
939
- "confidence": 0.5,
940
- },
941
- ],
942
- )
943
-
944
- with pytest.raises(
945
- AssertionError,
946
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
947
- ):
948
- mock_elements_worker.create_element_transcriptions(
949
- element=elt,
950
- sub_element_type="page",
951
- transcriptions=[
952
- {
953
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
954
- "confidence": 0.75,
955
- "text": "The",
956
- },
957
- {
958
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
959
- "confidence": 0.5,
960
- "text": None,
961
- },
962
- ],
963
- )
964
-
965
- with pytest.raises(
966
- AssertionError,
967
- match="Transcription at index 1 in transcriptions: text shouldn't be null and should be of type str",
968
- ):
969
- mock_elements_worker.create_element_transcriptions(
970
- element=elt,
971
- sub_element_type="page",
972
- transcriptions=[
973
- {
974
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
975
- "confidence": 0.75,
976
- "text": "The",
977
- },
978
- {
979
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
980
- "confidence": 0.5,
981
- "text": 1234,
982
- },
983
- ],
984
- )
985
-
986
- with pytest.raises(
987
- AssertionError,
988
- match=re.escape(
989
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
990
- ),
991
- ):
992
- mock_elements_worker.create_element_transcriptions(
993
- element=elt,
994
- sub_element_type="page",
995
- transcriptions=[
996
- {
997
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
998
- "confidence": 0.75,
999
- "text": "The",
1000
- },
1001
- {
1002
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1003
- "text": "word",
1004
- },
1005
- ],
1006
- )
1007
-
1008
- with pytest.raises(
1009
- AssertionError,
1010
- match=re.escape(
1011
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
1012
- ),
1013
- ):
1014
- mock_elements_worker.create_element_transcriptions(
1015
- element=elt,
1016
- sub_element_type="page",
1017
- transcriptions=[
1018
- {
1019
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1020
- "confidence": 0.75,
1021
- "text": "The",
1022
- },
1023
- {
1024
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1025
- "confidence": None,
1026
- "text": "word",
1027
- },
1028
- ],
1029
- )
1030
-
1031
- with pytest.raises(
1032
- AssertionError,
1033
- match=re.escape(
1034
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
1035
- ),
1036
- ):
1037
- mock_elements_worker.create_element_transcriptions(
1038
- element=elt,
1039
- sub_element_type="page",
1040
- transcriptions=[
1041
- {
1042
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1043
- "confidence": 0.75,
1044
- "text": "The",
1045
- },
1046
- {
1047
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1048
- "confidence": "a wrong confidence",
1049
- "text": "word",
1050
- },
1051
- ],
1052
- )
1053
-
1054
- with pytest.raises(
1055
- AssertionError,
1056
- match=re.escape(
1057
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
1058
- ),
1059
- ):
1060
- mock_elements_worker.create_element_transcriptions(
1061
- element=elt,
1062
- sub_element_type="page",
1063
- transcriptions=[
1064
- {
1065
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1066
- "confidence": 0.75,
1067
- "text": "The",
1068
- },
1069
- {
1070
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1071
- "confidence": 0,
1072
- "text": "word",
1073
- },
1074
- ],
1075
- )
1076
-
1077
- with pytest.raises(
1078
- AssertionError,
1079
- match=re.escape(
1080
- "Transcription at index 1 in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"
1081
- ),
1082
- ):
1083
- mock_elements_worker.create_element_transcriptions(
1084
- element=elt,
1085
- sub_element_type="page",
1086
- transcriptions=[
1087
- {
1088
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1089
- "confidence": 0.75,
1090
- "text": "The",
1091
- },
1092
- {
1093
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1094
- "confidence": 2.00,
1095
- "text": "word",
1096
- },
1097
- ],
1098
- )
1099
-
1100
- with pytest.raises(
1101
- AssertionError,
1102
- match="Transcription at index 1 in transcriptions: polygon shouldn't be null and should be of type list",
1103
- ):
1104
- mock_elements_worker.create_element_transcriptions(
1105
- element=elt,
1106
- sub_element_type="page",
1107
- transcriptions=[
1108
- {
1109
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1110
- "confidence": 0.75,
1111
- "text": "The",
1112
- },
1113
- {"confidence": 0.5, "text": "word"},
1114
- ],
1115
- )
1116
-
1117
- with pytest.raises(
1118
- AssertionError,
1119
- match="Transcription at index 1 in transcriptions: polygon shouldn't be null and should be of type list",
1120
- ):
1121
- mock_elements_worker.create_element_transcriptions(
1122
- element=elt,
1123
- sub_element_type="page",
1124
- transcriptions=[
1125
- {
1126
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1127
- "confidence": 0.75,
1128
- "text": "The",
1129
- },
1130
- {"polygon": None, "confidence": 0.5, "text": "word"},
1131
- ],
1132
- )
1133
-
1134
- with pytest.raises(
1135
- AssertionError,
1136
- match="Transcription at index 1 in transcriptions: polygon shouldn't be null and should be of type list",
1137
- ):
1138
- mock_elements_worker.create_element_transcriptions(
1139
- element=elt,
1140
- sub_element_type="page",
1141
- transcriptions=[
1142
- {
1143
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1144
- "confidence": 0.75,
1145
- "text": "The",
1146
- },
1147
- {"polygon": "not a polygon", "confidence": 0.5, "text": "word"},
1148
- ],
1149
- )
1150
-
1151
- with pytest.raises(
1152
- AssertionError,
1153
- match="Transcription at index 1 in transcriptions: polygon should have at least three points",
1154
- ):
1155
- mock_elements_worker.create_element_transcriptions(
1156
- element=elt,
1157
- sub_element_type="page",
1158
- transcriptions=[
1159
- {
1160
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1161
- "confidence": 0.75,
1162
- "text": "The",
1163
- },
1164
- {"polygon": [[1, 1], [2, 2]], "confidence": 0.5, "text": "word"},
1165
- ],
1166
- )
1167
- with pytest.raises(
1168
- AssertionError,
1169
- match="Transcription at index 1 in transcriptions: polygon points should be lists of two items",
1170
- ):
1171
- mock_elements_worker.create_element_transcriptions(
1172
- element=elt,
1173
- sub_element_type="page",
1174
- transcriptions=[
1175
- {
1176
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1177
- "confidence": 0.75,
1178
- "text": "The",
1179
- },
1180
- {
1181
- "polygon": [[1, 1, 1], [2, 2, 1], [2, 1, 1], [1, 2, 1]],
1182
- "confidence": 0.5,
1183
- "text": "word",
1184
- },
1185
- ],
1186
- )
1187
-
1188
- with pytest.raises(
1189
- AssertionError,
1190
- match="Transcription at index 1 in transcriptions: polygon points should be lists of two items",
1191
- ):
1192
- mock_elements_worker.create_element_transcriptions(
1193
- element=elt,
1194
- sub_element_type="page",
1195
- transcriptions=[
1196
- {
1197
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1198
- "confidence": 0.75,
1199
- "text": "The",
1200
- },
1201
- {"polygon": [[1], [2], [2], [1]], "confidence": 0.5, "text": "word"},
1202
- ],
1203
- )
1204
-
1205
- with pytest.raises(
1206
- AssertionError,
1207
- match="Transcription at index 1 in transcriptions: polygon points should be lists of two numbers",
1208
- ):
1209
- mock_elements_worker.create_element_transcriptions(
1210
- element=elt,
1211
- sub_element_type="page",
1212
- transcriptions=[
1213
- {
1214
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1215
- "confidence": 0.75,
1216
- "text": "The",
1217
- },
1218
- {
1219
- "polygon": [["not a coord", 1], [2, 2], [2, 1], [1, 2]],
1220
- "confidence": 0.5,
1221
- "text": "word",
1222
- },
1223
- ],
1224
- )
1225
-
1226
- with pytest.raises(
1227
- AssertionError,
1228
- match="Transcription at index 1 in transcriptions: orientation shouldn't be null and should be of type TextOrientation",
1229
- ):
1230
- mock_elements_worker.create_element_transcriptions(
1231
- element=elt,
1232
- sub_element_type="page",
1233
- transcriptions=[
1234
- {
1235
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1236
- "confidence": 0.75,
1237
- "text": "The",
1238
- },
1239
- {
1240
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1241
- "confidence": 0.35,
1242
- "text": "word",
1243
- "orientation": "uptown",
1244
- },
1245
- ],
1246
- )
1247
-
1248
- with pytest.raises(
1249
- AssertionError,
1250
- match=re.escape(
1251
- "Transcription at index 1 in transcriptions: element_confidence should be either null or a float in [0..1] range"
1252
- ),
1253
- ):
1254
- mock_elements_worker.create_element_transcriptions(
1255
- element=elt,
1256
- sub_element_type="page",
1257
- transcriptions=[
1258
- {
1259
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1260
- "confidence": 0.75,
1261
- "text": "The",
1262
- },
1263
- {
1264
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1265
- "confidence": 0.75,
1266
- "text": "word",
1267
- "element_confidence": "not a confidence",
1268
- },
1269
- ],
1270
- )
1271
-
1272
-
1273
- def test_create_element_transcriptions_api_error(responses, mock_elements_worker):
1274
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1275
- responses.add(
1276
- responses.POST,
1277
- f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/",
1278
- status=500,
1279
- )
1280
-
1281
- with pytest.raises(ErrorResponse):
1282
- mock_elements_worker.create_element_transcriptions(
1283
- element=elt,
1284
- sub_element_type="page",
1285
- transcriptions=TRANSCRIPTIONS_SAMPLE,
1286
- )
1287
-
1288
- assert len(responses.calls) == len(BASE_API_CALLS) + 5
1289
- assert [
1290
- (call.request.method, call.request.url) for call in responses.calls
1291
- ] == BASE_API_CALLS + [
1292
- # We retry 5 times the API call
1293
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1294
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1295
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1296
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1297
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1298
- ]
1299
-
1300
-
1301
- def test_create_element_transcriptions(responses, mock_elements_worker):
1302
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1303
- responses.add(
1304
- responses.POST,
1305
- f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/",
1306
- status=200,
1307
- json=[
1308
- {
1309
- "id": "56785678-5678-5678-5678-567856785678",
1310
- "element_id": "11111111-1111-1111-1111-111111111111",
1311
- "created": True,
1312
- },
1313
- {
1314
- "id": "67896789-6789-6789-6789-678967896789",
1315
- "element_id": "22222222-2222-2222-2222-222222222222",
1316
- "created": False,
1317
- },
1318
- {
1319
- "id": "78907890-7890-7890-7890-789078907890",
1320
- "element_id": "11111111-1111-1111-1111-111111111111",
1321
- "created": True,
1322
- },
1323
- ],
1324
- )
1325
-
1326
- annotations = mock_elements_worker.create_element_transcriptions(
1327
- element=elt,
1328
- sub_element_type="page",
1329
- transcriptions=TRANSCRIPTIONS_SAMPLE,
1330
- )
1331
-
1332
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
1333
- assert [
1334
- (call.request.method, call.request.url) for call in responses.calls
1335
- ] == BASE_API_CALLS + [
1336
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1337
- ]
1338
-
1339
- assert json.loads(responses.calls[-1].request.body) == {
1340
- "element_type": "page",
1341
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1342
- "transcriptions": [
1343
- {
1344
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1345
- "confidence": 0.5,
1346
- "text": "The",
1347
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1348
- },
1349
- {
1350
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1351
- "confidence": 0.75,
1352
- "text": "first",
1353
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1354
- "element_confidence": 0.75,
1355
- },
1356
- {
1357
- "polygon": [[1000, 300], [1200, 300], [1200, 500], [1000, 500]],
1358
- "confidence": 0.9,
1359
- "text": "line",
1360
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1361
- },
1362
- ],
1363
- "return_elements": True,
1364
- }
1365
- assert annotations == [
1366
- {
1367
- "id": "56785678-5678-5678-5678-567856785678",
1368
- "element_id": "11111111-1111-1111-1111-111111111111",
1369
- "created": True,
1370
- },
1371
- {
1372
- "id": "67896789-6789-6789-6789-678967896789",
1373
- "element_id": "22222222-2222-2222-2222-222222222222",
1374
- "created": False,
1375
- },
1376
- {
1377
- "id": "78907890-7890-7890-7890-789078907890",
1378
- "element_id": "11111111-1111-1111-1111-111111111111",
1379
- "created": True,
1380
- },
1381
- ]
1382
-
1383
-
1384
- def test_create_element_transcriptions_with_cache(
1385
- responses, mock_elements_worker_with_cache
1386
- ):
1387
- elt = CachedElement(id="12341234-1234-1234-1234-123412341234", type="thing")
1388
-
1389
- responses.add(
1390
- responses.POST,
1391
- f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/",
1392
- status=200,
1393
- json=[
1394
- {
1395
- "id": "56785678-5678-5678-5678-567856785678",
1396
- "element_id": "11111111-1111-1111-1111-111111111111",
1397
- "created": True,
1398
- },
1399
- {
1400
- "id": "67896789-6789-6789-6789-678967896789",
1401
- "element_id": "22222222-2222-2222-2222-222222222222",
1402
- "created": False,
1403
- },
1404
- {
1405
- "id": "78907890-7890-7890-7890-789078907890",
1406
- "element_id": "11111111-1111-1111-1111-111111111111",
1407
- "created": True,
1408
- },
1409
- ],
1410
- )
1411
-
1412
- annotations = mock_elements_worker_with_cache.create_element_transcriptions(
1413
- element=elt,
1414
- sub_element_type="page",
1415
- transcriptions=TRANSCRIPTIONS_SAMPLE,
1416
- )
1417
-
1418
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
1419
- assert [
1420
- (call.request.method, call.request.url) for call in responses.calls
1421
- ] == BASE_API_CALLS + [
1422
- ("POST", f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/"),
1423
- ]
1424
-
1425
- assert json.loads(responses.calls[-1].request.body) == {
1426
- "element_type": "page",
1427
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1428
- "transcriptions": [
1429
- {
1430
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1431
- "confidence": 0.5,
1432
- "text": "The",
1433
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1434
- },
1435
- {
1436
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1437
- "confidence": 0.75,
1438
- "text": "first",
1439
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1440
- "element_confidence": 0.75,
1441
- },
1442
- {
1443
- "polygon": [[1000, 300], [1200, 300], [1200, 500], [1000, 500]],
1444
- "confidence": 0.9,
1445
- "text": "line",
1446
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1447
- },
1448
- ],
1449
- "return_elements": True,
1450
- }
1451
- assert annotations == [
1452
- {
1453
- "id": "56785678-5678-5678-5678-567856785678",
1454
- "element_id": "11111111-1111-1111-1111-111111111111",
1455
- "created": True,
1456
- },
1457
- {
1458
- "id": "67896789-6789-6789-6789-678967896789",
1459
- "element_id": "22222222-2222-2222-2222-222222222222",
1460
- "created": False,
1461
- },
1462
- {
1463
- "id": "78907890-7890-7890-7890-789078907890",
1464
- "element_id": "11111111-1111-1111-1111-111111111111",
1465
- "created": True,
1466
- },
1467
- ]
1468
-
1469
- # Check that created transcriptions and elements were properly stored in SQLite cache
1470
- assert list(CachedElement.select()) == [
1471
- CachedElement(
1472
- id=UUID("11111111-1111-1111-1111-111111111111"),
1473
- parent_id=UUID("12341234-1234-1234-1234-123412341234"),
1474
- type="page",
1475
- polygon="[[100, 150], [700, 150], [700, 200], [100, 200]]",
1476
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
1477
- ),
1478
- CachedElement(
1479
- id=UUID("22222222-2222-2222-2222-222222222222"),
1480
- parent_id=UUID("12341234-1234-1234-1234-123412341234"),
1481
- type="page",
1482
- polygon="[[0, 0], [2000, 0], [2000, 3000], [0, 3000]]",
1483
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
1484
- confidence=0.75,
1485
- ),
1486
- ]
1487
- assert list(CachedTranscription.select()) == [
1488
- CachedTranscription(
1489
- id=UUID("56785678-5678-5678-5678-567856785678"),
1490
- element_id=UUID("11111111-1111-1111-1111-111111111111"),
1491
- text="The",
1492
- confidence=0.5,
1493
- orientation=TextOrientation.HorizontalLeftToRight.value,
1494
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
1495
- ),
1496
- CachedTranscription(
1497
- id=UUID("67896789-6789-6789-6789-678967896789"),
1498
- element_id=UUID("22222222-2222-2222-2222-222222222222"),
1499
- text="first",
1500
- confidence=0.75,
1501
- orientation=TextOrientation.HorizontalLeftToRight.value,
1502
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
1503
- ),
1504
- CachedTranscription(
1505
- id=UUID("78907890-7890-7890-7890-789078907890"),
1506
- element_id=UUID("11111111-1111-1111-1111-111111111111"),
1507
- text="line",
1508
- confidence=0.9,
1509
- orientation=TextOrientation.HorizontalLeftToRight.value,
1510
- worker_run_id=UUID("56785678-5678-5678-5678-567856785678"),
1511
- ),
1512
- ]
1513
-
1514
-
1515
- def test_create_transcriptions_orientation_with_cache(
1516
- responses, mock_elements_worker_with_cache
1517
- ):
1518
- elt = CachedElement(id="12341234-1234-1234-1234-123412341234", type="thing")
1519
-
1520
- responses.add(
1521
- responses.POST,
1522
- f"http://testserver/api/v1/element/{elt.id}/transcriptions/bulk/",
1523
- status=200,
1524
- json=[
1525
- {
1526
- "id": "56785678-5678-5678-5678-567856785678",
1527
- "element_id": "11111111-1111-1111-1111-111111111111",
1528
- "created": True,
1529
- },
1530
- {
1531
- "id": "67896789-6789-6789-6789-678967896789",
1532
- "element_id": "22222222-2222-2222-2222-222222222222",
1533
- "created": False,
1534
- },
1535
- {
1536
- "id": "78907890-7890-7890-7890-789078907890",
1537
- "element_id": "11111111-1111-1111-1111-111111111111",
1538
- "created": True,
1539
- },
1540
- ],
1541
- )
1542
-
1543
- oriented_transcriptions = [
1544
- {
1545
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1546
- "confidence": 0.5,
1547
- "text": "Animula vagula blandula",
1548
- },
1549
- {
1550
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1551
- "confidence": 0.75,
1552
- "text": "Hospes comesque corporis",
1553
- "orientation": TextOrientation.VerticalLeftToRight,
1554
- },
1555
- {
1556
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1557
- "confidence": 0.9,
1558
- "text": "Quae nunc abibis in loca",
1559
- "orientation": TextOrientation.HorizontalRightToLeft,
1560
- },
1561
- ]
1562
-
1563
- annotations = mock_elements_worker_with_cache.create_element_transcriptions(
1564
- element=elt,
1565
- sub_element_type="page",
1566
- transcriptions=oriented_transcriptions,
1567
- )
1568
-
1569
- assert json.loads(responses.calls[-1].request.body) == {
1570
- "element_type": "page",
1571
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1572
- "transcriptions": [
1573
- {
1574
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1575
- "confidence": 0.5,
1576
- "text": "Animula vagula blandula",
1577
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1578
- },
1579
- {
1580
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1581
- "confidence": 0.75,
1582
- "text": "Hospes comesque corporis",
1583
- "orientation": TextOrientation.VerticalLeftToRight.value,
1584
- },
1585
- {
1586
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1587
- "confidence": 0.9,
1588
- "text": "Quae nunc abibis in loca",
1589
- "orientation": TextOrientation.HorizontalRightToLeft.value,
1590
- },
1591
- ],
1592
- "return_elements": True,
1593
- }
1594
- assert annotations == [
1595
- {
1596
- "id": "56785678-5678-5678-5678-567856785678",
1597
- "element_id": "11111111-1111-1111-1111-111111111111",
1598
- "created": True,
1599
- },
1600
- {
1601
- "id": "67896789-6789-6789-6789-678967896789",
1602
- "element_id": "22222222-2222-2222-2222-222222222222",
1603
- "created": False,
1604
- },
1605
- {
1606
- "id": "78907890-7890-7890-7890-789078907890",
1607
- "element_id": "11111111-1111-1111-1111-111111111111",
1608
- "created": True,
1609
- },
1610
- ]
1611
-
1612
- # Check that the text orientation was properly stored in SQLite cache
1613
- assert list(map(model_to_dict, CachedTranscription.select())) == [
1614
- {
1615
- "id": UUID("56785678-5678-5678-5678-567856785678"),
1616
- "element": {
1617
- "id": UUID("11111111-1111-1111-1111-111111111111"),
1618
- "parent_id": UUID(elt.id),
1619
- "type": "page",
1620
- "image": None,
1621
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1622
- "rotation_angle": 0,
1623
- "mirrored": False,
1624
- "initial": False,
1625
- "worker_version_id": None,
1626
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1627
- "confidence": None,
1628
- },
1629
- "text": "Animula vagula blandula",
1630
- "confidence": 0.5,
1631
- "orientation": TextOrientation.HorizontalLeftToRight.value,
1632
- "worker_version_id": None,
1633
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1634
- },
1635
- {
1636
- "id": UUID("67896789-6789-6789-6789-678967896789"),
1637
- "element": {
1638
- "id": UUID("22222222-2222-2222-2222-222222222222"),
1639
- "parent_id": UUID(elt.id),
1640
- "type": "page",
1641
- "image": None,
1642
- "polygon": [[0, 0], [2000, 0], [2000, 3000], [0, 3000]],
1643
- "rotation_angle": 0,
1644
- "mirrored": False,
1645
- "initial": False,
1646
- "worker_version_id": None,
1647
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1648
- "confidence": None,
1649
- },
1650
- "text": "Hospes comesque corporis",
1651
- "confidence": 0.75,
1652
- "orientation": TextOrientation.VerticalLeftToRight.value,
1653
- "worker_version_id": None,
1654
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1655
- },
1656
- {
1657
- "id": UUID("78907890-7890-7890-7890-789078907890"),
1658
- "element": {
1659
- "id": UUID("11111111-1111-1111-1111-111111111111"),
1660
- "parent_id": UUID(elt.id),
1661
- "type": "page",
1662
- "image": None,
1663
- "polygon": [[100, 150], [700, 150], [700, 200], [100, 200]],
1664
- "rotation_angle": 0,
1665
- "mirrored": False,
1666
- "initial": False,
1667
- "worker_version_id": None,
1668
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1669
- "confidence": None,
1670
- },
1671
- "text": "Quae nunc abibis in loca",
1672
- "confidence": 0.9,
1673
- "orientation": TextOrientation.HorizontalRightToLeft.value,
1674
- "worker_version_id": None,
1675
- "worker_run_id": UUID("56785678-5678-5678-5678-567856785678"),
1676
- },
1677
- ]
1678
-
1679
-
1680
- def test_list_transcriptions_wrong_element(mock_elements_worker):
1681
- with pytest.raises(
1682
- AssertionError,
1683
- match="element shouldn't be null and should be an Element or CachedElement",
1684
- ):
1685
- mock_elements_worker.list_transcriptions(element=None)
1686
-
1687
- with pytest.raises(
1688
- AssertionError,
1689
- match="element shouldn't be null and should be an Element or CachedElement",
1690
- ):
1691
- mock_elements_worker.list_transcriptions(element="not element type")
1692
-
1693
-
1694
- def test_list_transcriptions_wrong_element_type(mock_elements_worker):
1695
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1696
-
1697
- with pytest.raises(AssertionError, match="element_type should be of type str"):
1698
- mock_elements_worker.list_transcriptions(
1699
- element=elt,
1700
- element_type=1234,
1701
- )
1702
-
1703
-
1704
- def test_list_transcriptions_wrong_recursive(mock_elements_worker):
1705
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1706
-
1707
- with pytest.raises(AssertionError, match="recursive should be of type bool"):
1708
- mock_elements_worker.list_transcriptions(
1709
- element=elt,
1710
- recursive="not bool",
1711
- )
1712
-
1713
-
1714
- def test_list_transcriptions_wrong_worker_run(mock_elements_worker):
1715
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1716
-
1717
- with pytest.raises(
1718
- AssertionError, match="worker_run should be of type str or bool"
1719
- ):
1720
- mock_elements_worker.list_transcriptions(
1721
- element=elt,
1722
- worker_run=1234,
1723
- )
1724
-
1725
-
1726
- def test_list_transcriptions_wrong_worker_version(mock_elements_worker):
1727
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1728
-
1729
- # WARNING: pytest.deprecated_call must be placed BEFORE pytest.raises, otherwise `match` argument won't be checked
1730
- with (
1731
- pytest.deprecated_call(
1732
- match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
1733
- ),
1734
- pytest.raises(
1735
- AssertionError, match="worker_version should be of type str or bool"
1736
- ),
1737
- ):
1738
- mock_elements_worker.list_transcriptions(
1739
- element=elt,
1740
- worker_version=1234,
1741
- )
1742
-
1743
-
1744
- def test_list_transcriptions_wrong_bool_worker_run(mock_elements_worker):
1745
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1746
-
1747
- with pytest.raises(
1748
- AssertionError, match="if of type bool, worker_run can only be set to False"
1749
- ):
1750
- mock_elements_worker.list_transcriptions(
1751
- element=elt,
1752
- worker_run=True,
1753
- )
1754
-
1755
-
1756
- def test_list_transcriptions_wrong_bool_worker_version(mock_elements_worker):
1757
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1758
-
1759
- # WARNING: pytest.deprecated_call must be placed BEFORE pytest.raises, otherwise `match` argument won't be checked
1760
- with (
1761
- pytest.deprecated_call(
1762
- match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
1763
- ),
1764
- pytest.raises(
1765
- AssertionError,
1766
- match="if of type bool, worker_version can only be set to False",
1767
- ),
1768
- ):
1769
- mock_elements_worker.list_transcriptions(
1770
- element=elt,
1771
- worker_version=True,
1772
- )
1773
-
1774
-
1775
- def test_list_transcriptions_api_error(responses, mock_elements_worker):
1776
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1777
- responses.add(
1778
- responses.GET,
1779
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1780
- status=500,
1781
- )
1782
-
1783
- with pytest.raises(
1784
- Exception, match="Stopping pagination as data will be incomplete"
1785
- ):
1786
- next(mock_elements_worker.list_transcriptions(element=elt))
1787
-
1788
- assert len(responses.calls) == len(BASE_API_CALLS) + 5
1789
- assert [
1790
- (call.request.method, call.request.url) for call in responses.calls
1791
- ] == BASE_API_CALLS + [
1792
- # We do 5 retries
1793
- (
1794
- "GET",
1795
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1796
- ),
1797
- (
1798
- "GET",
1799
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1800
- ),
1801
- (
1802
- "GET",
1803
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1804
- ),
1805
- (
1806
- "GET",
1807
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1808
- ),
1809
- (
1810
- "GET",
1811
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1812
- ),
1813
- ]
1814
-
1815
-
1816
- def test_list_transcriptions(responses, mock_elements_worker):
1817
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1818
- trans = [
1819
- {
1820
- "id": "0000",
1821
- "text": "hey",
1822
- "confidence": 0.42,
1823
- "worker_version_id": "56785678-5678-5678-5678-567856785678",
1824
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1825
- "element": None,
1826
- },
1827
- {
1828
- "id": "1111",
1829
- "text": "it's",
1830
- "confidence": 0.42,
1831
- "worker_version_id": "56785678-5678-5678-5678-567856785678",
1832
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1833
- "element": None,
1834
- },
1835
- {
1836
- "id": "2222",
1837
- "text": "me",
1838
- "confidence": 0.42,
1839
- "worker_version_id": "56785678-5678-5678-5678-567856785678",
1840
- "worker_run_id": "56785678-5678-5678-5678-567856785678",
1841
- "element": None,
1842
- },
1843
- ]
1844
- responses.add(
1845
- responses.GET,
1846
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1847
- status=200,
1848
- json={
1849
- "count": 3,
1850
- "next": None,
1851
- "results": trans,
1852
- },
1853
- )
1854
-
1855
- for idx, transcription in enumerate(
1856
- mock_elements_worker.list_transcriptions(element=elt)
1857
- ):
1858
- assert transcription == trans[idx]
1859
-
1860
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
1861
- assert [
1862
- (call.request.method, call.request.url) for call in responses.calls
1863
- ] == BASE_API_CALLS + [
1864
- (
1865
- "GET",
1866
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/",
1867
- ),
1868
- ]
1869
-
1870
-
1871
- def test_list_transcriptions_manual_worker_version(responses, mock_elements_worker):
1872
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1873
- trans = [
1874
- {
1875
- "id": "0000",
1876
- "text": "hey",
1877
- "confidence": 0.42,
1878
- "worker_version_id": None,
1879
- "worker_run_id": None,
1880
- "element": None,
1881
- }
1882
- ]
1883
- responses.add(
1884
- responses.GET,
1885
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_version=False",
1886
- status=200,
1887
- json={
1888
- "count": 1,
1889
- "next": None,
1890
- "results": trans,
1891
- },
1892
- )
1893
-
1894
- with pytest.deprecated_call(
1895
- match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
1896
- ):
1897
- for idx, transcription in enumerate(
1898
- mock_elements_worker.list_transcriptions(element=elt, worker_version=False)
1899
- ):
1900
- assert transcription == trans[idx]
1901
-
1902
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
1903
- assert [
1904
- (call.request.method, call.request.url) for call in responses.calls
1905
- ] == BASE_API_CALLS + [
1906
- (
1907
- "GET",
1908
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_version=False",
1909
- ),
1910
- ]
1911
-
1912
-
1913
- def test_list_transcriptions_manual_worker_run(responses, mock_elements_worker):
1914
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
1915
- trans = [
1916
- {
1917
- "id": "0000",
1918
- "text": "hey",
1919
- "confidence": 0.42,
1920
- "worker_version_id": None,
1921
- "worker_run_id": None,
1922
- "element": None,
1923
- }
1924
- ]
1925
- responses.add(
1926
- responses.GET,
1927
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_run=False",
1928
- status=200,
1929
- json={
1930
- "count": 1,
1931
- "next": None,
1932
- "results": trans,
1933
- },
1934
- )
1935
-
1936
- for idx, transcription in enumerate(
1937
- mock_elements_worker.list_transcriptions(element=elt, worker_run=False)
1938
- ):
1939
- assert transcription == trans[idx]
1940
-
1941
- assert len(responses.calls) == len(BASE_API_CALLS) + 1
1942
- assert [
1943
- (call.request.method, call.request.url) for call in responses.calls
1944
- ] == BASE_API_CALLS + [
1945
- (
1946
- "GET",
1947
- "http://testserver/api/v1/element/12341234-1234-1234-1234-123412341234/transcriptions/?worker_run=False",
1948
- ),
1949
- ]
1950
-
1951
-
1952
- @pytest.mark.usefixtures("_mock_cached_transcriptions")
1953
- @pytest.mark.parametrize(
1954
- ("filters", "expected_ids"),
1955
- [
1956
- # Filter on element should give first and sixth transcription
1957
- (
1958
- {
1959
- "element": CachedElement(
1960
- id="11111111-1111-1111-1111-111111111111", type="page"
1961
- ),
1962
- },
1963
- (
1964
- "11111111-1111-1111-1111-111111111111",
1965
- "66666666-6666-6666-6666-666666666666",
1966
- ),
1967
- ),
1968
- # Filter on element and element_type should give first and sixth transcription
1969
- (
1970
- {
1971
- "element": CachedElement(
1972
- id="11111111-1111-1111-1111-111111111111", type="page"
1973
- ),
1974
- "element_type": "page",
1975
- },
1976
- (
1977
- "11111111-1111-1111-1111-111111111111",
1978
- "66666666-6666-6666-6666-666666666666",
1979
- ),
1980
- ),
1981
- # Filter on element and worker run should give the first transcription
1982
- (
1983
- {
1984
- "element": CachedElement(
1985
- id="11111111-1111-1111-1111-111111111111", type="page"
1986
- ),
1987
- "worker_run": "56785678-5678-5678-5678-567856785678",
1988
- },
1989
- ("11111111-1111-1111-1111-111111111111",),
1990
- ),
1991
- # Filter on element, manual worker run should give the sixth transcription
1992
- (
1993
- {
1994
- "element": CachedElement(
1995
- id="11111111-1111-1111-1111-111111111111", type="page"
1996
- ),
1997
- "worker_run": False,
1998
- },
1999
- ("66666666-6666-6666-6666-666666666666",),
2000
- ),
2001
- # Filter recursively on element should give all transcriptions inserted
2002
- (
2003
- {
2004
- "element": CachedElement(
2005
- id="11111111-1111-1111-1111-111111111111", type="page"
2006
- ),
2007
- "recursive": True,
2008
- },
2009
- (
2010
- "11111111-1111-1111-1111-111111111111",
2011
- "22222222-2222-2222-2222-222222222222",
2012
- "33333333-3333-3333-3333-333333333333",
2013
- "44444444-4444-4444-4444-444444444444",
2014
- "55555555-5555-5555-5555-555555555555",
2015
- "66666666-6666-6666-6666-666666666666",
2016
- ),
2017
- ),
2018
- # Filter recursively on element and element_type should give three transcriptions
2019
- (
2020
- {
2021
- "element": CachedElement(
2022
- id="11111111-1111-1111-1111-111111111111", type="page"
2023
- ),
2024
- "element_type": "something_else",
2025
- "recursive": True,
2026
- },
2027
- (
2028
- "22222222-2222-2222-2222-222222222222",
2029
- "44444444-4444-4444-4444-444444444444",
2030
- "55555555-5555-5555-5555-555555555555",
2031
- ),
2032
- ),
2033
- ],
2034
- )
2035
- def test_list_transcriptions_with_cache(
2036
- responses, mock_elements_worker_with_cache, filters, expected_ids
2037
- ):
2038
- # Check we have 5 elements already present in database
2039
- assert CachedTranscription.select().count() == 6
2040
-
2041
- # Query database through cache
2042
- transcriptions = mock_elements_worker_with_cache.list_transcriptions(**filters)
2043
- assert transcriptions.count() == len(expected_ids)
2044
- for transcription, expected_id in zip(
2045
- transcriptions.order_by(CachedTranscription.id), expected_ids, strict=True
2046
- ):
2047
- assert transcription.id == UUID(expected_id)
2048
-
2049
- # Check the worker never hits the API for elements
2050
- assert len(responses.calls) == len(BASE_API_CALLS)
2051
- assert [
2052
- (call.request.method, call.request.url) for call in responses.calls
2053
- ] == BASE_API_CALLS
2054
-
2055
-
2056
- @pytest.mark.usefixtures("_mock_cached_transcriptions")
2057
- @pytest.mark.parametrize(
2058
- ("filters", "expected_ids"),
2059
- [
2060
- # Filter on element and worker_version should give first transcription
2061
- (
2062
- {
2063
- "element": CachedElement(
2064
- id="11111111-1111-1111-1111-111111111111", type="page"
2065
- ),
2066
- "worker_version": "56785678-5678-5678-5678-567856785678",
2067
- },
2068
- ("11111111-1111-1111-1111-111111111111",),
2069
- ),
2070
- # Filter recursively on element and worker_version should give four transcriptions
2071
- (
2072
- {
2073
- "element": CachedElement(
2074
- id="11111111-1111-1111-1111-111111111111", type="page"
2075
- ),
2076
- "worker_version": "90129012-9012-9012-9012-901290129012",
2077
- "recursive": True,
2078
- },
2079
- (
2080
- "22222222-2222-2222-2222-222222222222",
2081
- "33333333-3333-3333-3333-333333333333",
2082
- "44444444-4444-4444-4444-444444444444",
2083
- "55555555-5555-5555-5555-555555555555",
2084
- ),
2085
- ),
2086
- # Filter on element with manually created transcription should give sixth transcription
2087
- (
2088
- {
2089
- "element": CachedElement(
2090
- id="11111111-1111-1111-1111-111111111111", type="page"
2091
- ),
2092
- "worker_version": False,
2093
- },
2094
- ("66666666-6666-6666-6666-666666666666",),
2095
- ),
2096
- ],
2097
- )
2098
- def test_list_transcriptions_with_cache_deprecation(
2099
- responses, mock_elements_worker_with_cache, filters, expected_ids
2100
- ):
2101
- # Check we have 5 elements already present in database
2102
- assert CachedTranscription.select().count() == 6
2103
-
2104
- with pytest.deprecated_call(
2105
- match="`worker_version` usage is deprecated. Consider using `worker_run` instead."
2106
- ):
2107
- # Query database through cache
2108
- transcriptions = mock_elements_worker_with_cache.list_transcriptions(**filters)
2109
- assert transcriptions.count() == len(expected_ids)
2110
- for transcription, expected_id in zip(
2111
- transcriptions.order_by(CachedTranscription.id), expected_ids, strict=True
2112
- ):
2113
- assert transcription.id == UUID(expected_id)
2114
-
2115
- # Check the worker never hits the API for elements
2116
- assert len(responses.calls) == len(BASE_API_CALLS)
2117
- assert [
2118
- (call.request.method, call.request.url) for call in responses.calls
2119
- ] == BASE_API_CALLS