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