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