arkindex-base-worker 0.3.7rc3__py3-none-any.whl → 0.3.7rc4__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.
@@ -10,6 +10,10 @@ from arkindex_worker.models import Element
10
10
 
11
11
  from . import BASE_API_CALLS
12
12
 
13
+ # Special string used to know if the `arg_name` passed in
14
+ # `pytest.mark.parametrize` should be removed from the payload
15
+ DELETE_PARAMETER = "DELETE_PARAMETER"
16
+
13
17
 
14
18
  def test_get_ml_class_id_load_classes(responses, mock_elements_worker):
15
19
  corpus_id = "11111111-1111-1111-1111-111111111111"
@@ -190,54 +194,116 @@ def test_retrieve_ml_class_not_in_cache(responses, mock_elements_worker):
190
194
  ]
191
195
 
192
196
 
193
- def test_create_classification_wrong_element(mock_elements_worker):
194
- with pytest.raises(
195
- AssertionError,
196
- match="element shouldn't be null and should be an Element or CachedElement",
197
- ):
198
- mock_elements_worker.create_classification(
199
- element=None,
200
- ml_class="a_class",
201
- confidence=0.42,
202
- high_confidence=True,
203
- )
204
-
205
- with pytest.raises(
206
- AssertionError,
207
- match="element shouldn't be null and should be an Element or CachedElement",
208
- ):
197
+ @pytest.mark.parametrize(
198
+ ("arg_name", "data", "error_message"),
199
+ [
200
+ # Wrong element
201
+ (
202
+ "element",
203
+ None,
204
+ "element shouldn't be null and should be an Element or CachedElement",
205
+ ),
206
+ (
207
+ "element",
208
+ "not element type",
209
+ "element shouldn't be null and should be an Element or CachedElement",
210
+ ),
211
+ # Wrong ml_class
212
+ (
213
+ "ml_class",
214
+ None,
215
+ "ml_class shouldn't be null and should be of type str",
216
+ ),
217
+ (
218
+ "ml_class",
219
+ 1234,
220
+ "ml_class shouldn't be null and should be of type str",
221
+ ),
222
+ # Wrong confidence
223
+ (
224
+ "confidence",
225
+ None,
226
+ "confidence shouldn't be null and should be a float in [0..1] range",
227
+ ),
228
+ (
229
+ "confidence",
230
+ "wrong confidence",
231
+ "confidence shouldn't be null and should be a float in [0..1] range",
232
+ ),
233
+ (
234
+ "confidence",
235
+ 0,
236
+ "confidence shouldn't be null and should be a float in [0..1] range",
237
+ ),
238
+ (
239
+ "confidence",
240
+ 2.00,
241
+ "confidence shouldn't be null and should be a float in [0..1] range",
242
+ ),
243
+ # Wrong high_confidence
244
+ (
245
+ "high_confidence",
246
+ None,
247
+ "high_confidence shouldn't be null and should be of type bool",
248
+ ),
249
+ (
250
+ "high_confidence",
251
+ "wrong high_confidence",
252
+ "high_confidence shouldn't be null and should be of type bool",
253
+ ),
254
+ ],
255
+ )
256
+ def test_create_classification_wrong_data(
257
+ arg_name, data, error_message, mock_elements_worker
258
+ ):
259
+ mock_elements_worker.classes = {"a_class": "0000"}
260
+ with pytest.raises(AssertionError, match=re.escape(error_message)):
209
261
  mock_elements_worker.create_classification(
210
- element="not element type",
211
- ml_class="a_class",
212
- confidence=0.42,
213
- high_confidence=True,
262
+ **{
263
+ "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
264
+ "ml_class": "a_class",
265
+ "confidence": 0.42,
266
+ "high_confidence": True,
267
+ # Overwrite with wrong data
268
+ arg_name: data,
269
+ }
214
270
  )
215
271
 
216
272
 
217
- def test_create_classification_wrong_ml_class(mock_elements_worker, responses):
273
+ def test_create_classification_api_error(responses, mock_elements_worker):
274
+ mock_elements_worker.classes = {"a_class": "0000"}
218
275
  elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
276
+ responses.add(
277
+ responses.POST,
278
+ "http://testserver/api/v1/classifications/",
279
+ status=500,
280
+ )
219
281
 
220
- with pytest.raises(
221
- AssertionError, match="ml_class shouldn't be null and should be of type str"
222
- ):
282
+ with pytest.raises(ErrorResponse):
223
283
  mock_elements_worker.create_classification(
224
284
  element=elt,
225
- ml_class=None,
285
+ ml_class="a_class",
226
286
  confidence=0.42,
227
287
  high_confidence=True,
228
288
  )
229
289
 
230
- with pytest.raises(
231
- AssertionError, match="ml_class shouldn't be null and should be of type str"
232
- ):
233
- mock_elements_worker.create_classification(
234
- element=elt,
235
- ml_class=1234,
236
- confidence=0.42,
237
- high_confidence=True,
238
- )
290
+ assert len(responses.calls) == len(BASE_API_CALLS) + 5
291
+ assert [
292
+ (call.request.method, call.request.url) for call in responses.calls
293
+ ] == BASE_API_CALLS + [
294
+ # We retry 5 times the API call
295
+ ("POST", "http://testserver/api/v1/classifications/"),
296
+ ("POST", "http://testserver/api/v1/classifications/"),
297
+ ("POST", "http://testserver/api/v1/classifications/"),
298
+ ("POST", "http://testserver/api/v1/classifications/"),
299
+ ("POST", "http://testserver/api/v1/classifications/"),
300
+ ]
301
+
239
302
 
240
- # Automatically create a missing class !
303
+ def test_create_classification_create_ml_class(mock_elements_worker, responses):
304
+ elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
305
+
306
+ # Automatically create a missing class!
241
307
  responses.add(
242
308
  responses.POST,
243
309
  "http://testserver/api/v1/corpus/11111111-1111-1111-1111-111111111111/classes/",
@@ -283,119 +349,6 @@ def test_create_classification_wrong_ml_class(mock_elements_worker, responses):
283
349
  ]
284
350
 
285
351
 
286
- def test_create_classification_wrong_confidence(mock_elements_worker):
287
- mock_elements_worker.classes = {"a_class": "0000"}
288
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
289
- with pytest.raises(
290
- AssertionError,
291
- match=re.escape(
292
- "confidence shouldn't be null and should be a float in [0..1] range"
293
- ),
294
- ):
295
- mock_elements_worker.create_classification(
296
- element=elt,
297
- ml_class="a_class",
298
- confidence=None,
299
- high_confidence=True,
300
- )
301
-
302
- with pytest.raises(
303
- AssertionError,
304
- match=re.escape(
305
- "confidence shouldn't be null and should be a float in [0..1] range"
306
- ),
307
- ):
308
- mock_elements_worker.create_classification(
309
- element=elt,
310
- ml_class="a_class",
311
- confidence="wrong confidence",
312
- high_confidence=True,
313
- )
314
-
315
- with pytest.raises(
316
- AssertionError,
317
- match=re.escape(
318
- "confidence shouldn't be null and should be a float in [0..1] range"
319
- ),
320
- ):
321
- mock_elements_worker.create_classification(
322
- element=elt,
323
- ml_class="a_class",
324
- confidence=0,
325
- high_confidence=True,
326
- )
327
-
328
- with pytest.raises(
329
- AssertionError,
330
- match=re.escape(
331
- "confidence shouldn't be null and should be a float in [0..1] range"
332
- ),
333
- ):
334
- mock_elements_worker.create_classification(
335
- element=elt,
336
- ml_class="a_class",
337
- confidence=2.00,
338
- high_confidence=True,
339
- )
340
-
341
-
342
- def test_create_classification_wrong_high_confidence(mock_elements_worker):
343
- mock_elements_worker.classes = {"a_class": "0000"}
344
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
345
-
346
- with pytest.raises(
347
- AssertionError,
348
- match="high_confidence shouldn't be null and should be of type bool",
349
- ):
350
- mock_elements_worker.create_classification(
351
- element=elt,
352
- ml_class="a_class",
353
- confidence=0.42,
354
- high_confidence=None,
355
- )
356
-
357
- with pytest.raises(
358
- AssertionError,
359
- match="high_confidence shouldn't be null and should be of type bool",
360
- ):
361
- mock_elements_worker.create_classification(
362
- element=elt,
363
- ml_class="a_class",
364
- confidence=0.42,
365
- high_confidence="wrong high_confidence",
366
- )
367
-
368
-
369
- def test_create_classification_api_error(responses, mock_elements_worker):
370
- mock_elements_worker.classes = {"a_class": "0000"}
371
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
372
- responses.add(
373
- responses.POST,
374
- "http://testserver/api/v1/classifications/",
375
- status=500,
376
- )
377
-
378
- with pytest.raises(ErrorResponse):
379
- mock_elements_worker.create_classification(
380
- element=elt,
381
- ml_class="a_class",
382
- confidence=0.42,
383
- high_confidence=True,
384
- )
385
-
386
- assert len(responses.calls) == len(BASE_API_CALLS) + 5
387
- assert [
388
- (call.request.method, call.request.url) for call in responses.calls
389
- ] == BASE_API_CALLS + [
390
- # We retry 5 times the API call
391
- ("POST", "http://testserver/api/v1/classifications/"),
392
- ("POST", "http://testserver/api/v1/classifications/"),
393
- ("POST", "http://testserver/api/v1/classifications/"),
394
- ("POST", "http://testserver/api/v1/classifications/"),
395
- ("POST", "http://testserver/api/v1/classifications/"),
396
- ]
397
-
398
-
399
352
  def test_create_classification(responses, mock_elements_worker):
400
353
  mock_elements_worker.classes = {"a_class": "0000"}
401
354
  elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
@@ -519,278 +472,154 @@ def test_create_classification_duplicate_worker_run(responses, mock_elements_wor
519
472
  }
520
473
 
521
474
 
522
- def test_create_classifications_wrong_element(mock_elements_worker):
523
- with pytest.raises(
524
- AssertionError,
525
- match="element shouldn't be null and should be an Element or CachedElement",
526
- ):
527
- mock_elements_worker.create_classifications(
528
- element=None,
529
- classifications=[
530
- {
531
- "ml_class_id": "uuid1",
532
- "confidence": 0.75,
533
- "high_confidence": False,
534
- },
535
- {
536
- "ml_class_id": "uuid2",
537
- "confidence": 0.25,
538
- "high_confidence": False,
539
- },
540
- ],
541
- )
542
-
543
- with pytest.raises(
544
- AssertionError,
545
- match="element shouldn't be null and should be an Element or CachedElement",
546
- ):
547
- mock_elements_worker.create_classifications(
548
- element="not element type",
549
- classifications=[
550
- {
551
- "ml_class_id": "uuid1",
552
- "confidence": 0.75,
553
- "high_confidence": False,
554
- },
555
- {
556
- "ml_class_id": "uuid2",
557
- "confidence": 0.25,
558
- "high_confidence": False,
559
- },
560
- ],
561
- )
562
-
563
-
564
- def test_create_classifications_wrong_classifications(mock_elements_worker):
565
- elt = Element({"id": "12341234-1234-1234-1234-123412341234"})
566
-
567
- with pytest.raises(
568
- AssertionError,
569
- match="classifications shouldn't be null and should be of type list",
570
- ):
571
- mock_elements_worker.create_classifications(
572
- element=elt,
573
- classifications=None,
574
- )
575
-
576
- with pytest.raises(
577
- AssertionError,
578
- match="classifications shouldn't be null and should be of type list",
579
- ):
580
- mock_elements_worker.create_classifications(
581
- element=elt,
582
- classifications=1234,
583
- )
584
-
585
- with pytest.raises(
586
- AssertionError,
587
- match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
588
- ):
589
- mock_elements_worker.create_classifications(
590
- element=elt,
591
- classifications=[
592
- {
593
- "ml_class_id": str(uuid4()),
594
- "confidence": 0.75,
595
- "high_confidence": False,
596
- },
597
- {
598
- "ml_class_id": 0.25,
599
- "high_confidence": False,
600
- },
601
- ],
602
- )
603
-
604
- with pytest.raises(
605
- AssertionError,
606
- match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
607
- ):
608
- mock_elements_worker.create_classifications(
609
- element=elt,
610
- classifications=[
611
- {
612
- "ml_class_id": str(uuid4()),
613
- "confidence": 0.75,
614
- "high_confidence": False,
615
- },
616
- {
617
- "ml_class_id": None,
618
- "confidence": 0.25,
619
- "high_confidence": False,
620
- },
621
- ],
622
- )
623
-
624
- with pytest.raises(
625
- AssertionError,
626
- match="Classification at index 1 in classifications: ml_class_id shouldn't be null and should be of type str",
627
- ):
475
+ @pytest.mark.parametrize(
476
+ ("arg_name", "data", "error_message"),
477
+ [
478
+ (
479
+ "element",
480
+ None,
481
+ "element shouldn't be null and should be an Element or CachedElement",
482
+ ),
483
+ (
484
+ "element",
485
+ "not element type",
486
+ "element shouldn't be null and should be an Element or CachedElement",
487
+ ),
488
+ (
489
+ "classifications",
490
+ None,
491
+ "classifications shouldn't be null and should be of type list",
492
+ ),
493
+ (
494
+ "classifications",
495
+ 1234,
496
+ "classifications shouldn't be null and should be of type list",
497
+ ),
498
+ ],
499
+ )
500
+ def test_create_classifications_wrong_data(
501
+ arg_name, data, error_message, mock_elements_worker
502
+ ):
503
+ with pytest.raises(AssertionError, match=error_message):
628
504
  mock_elements_worker.create_classifications(
629
- element=elt,
630
- classifications=[
631
- {
632
- "ml_class_id": str(uuid4()),
633
- "confidence": 0.75,
634
- "high_confidence": False,
635
- },
636
- {
637
- "ml_class_id": 1234,
638
- "confidence": 0.25,
639
- "high_confidence": False,
640
- },
641
- ],
505
+ **{
506
+ "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
507
+ "classifications": [
508
+ {
509
+ "ml_class_id": "uuid1",
510
+ "confidence": 0.75,
511
+ "high_confidence": False,
512
+ },
513
+ {
514
+ "ml_class_id": "uuid2",
515
+ "confidence": 0.25,
516
+ "high_confidence": False,
517
+ },
518
+ ],
519
+ # Overwrite with wrong data
520
+ arg_name: data,
521
+ },
642
522
  )
643
523
 
644
- with pytest.raises(
645
- ValueError,
646
- match="Classification at index 1 in classifications: ml_class_id is not a valid uuid.",
647
- ):
648
- mock_elements_worker.create_classifications(
649
- element=elt,
650
- classifications=[
651
- {
652
- "ml_class_id": str(uuid4()),
653
- "confidence": 0.75,
654
- "high_confidence": False,
655
- },
656
- {
657
- "ml_class_id": "not_an_uuid",
658
- "confidence": 0.25,
659
- "high_confidence": False,
660
- },
661
- ],
662
- )
663
524
 
664
- with pytest.raises(
665
- AssertionError,
666
- match=re.escape(
667
- "Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
525
+ @pytest.mark.parametrize(
526
+ ("arg_name", "data", "error_message", "error_type"),
527
+ [
528
+ # Wrong classifications > ml_class_id
529
+ (
530
+ "ml_class_id",
531
+ DELETE_PARAMETER,
532
+ "ml_class_id shouldn't be null and should be of type str",
533
+ AssertionError,
534
+ ), # Updated
535
+ (
536
+ "ml_class_id",
537
+ None,
538
+ "ml_class_id shouldn't be null and should be of type str",
539
+ AssertionError,
668
540
  ),
669
- ):
670
- mock_elements_worker.create_classifications(
671
- element=elt,
672
- classifications=[
673
- {
674
- "ml_class_id": str(uuid4()),
675
- "confidence": 0.75,
676
- "high_confidence": False,
677
- },
678
- {
679
- "ml_class_id": str(uuid4()),
680
- "high_confidence": False,
681
- },
682
- ],
683
- )
684
-
685
- with pytest.raises(
686
- AssertionError,
687
- match=re.escape(
688
- "Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
541
+ (
542
+ "ml_class_id",
543
+ 1234,
544
+ "ml_class_id shouldn't be null and should be of type str",
545
+ AssertionError,
689
546
  ),
690
- ):
691
- mock_elements_worker.create_classifications(
692
- element=elt,
693
- classifications=[
694
- {
695
- "ml_class_id": str(uuid4()),
696
- "confidence": 0.75,
697
- "high_confidence": False,
698
- },
699
- {
700
- "ml_class_id": str(uuid4()),
701
- "confidence": None,
702
- "high_confidence": False,
703
- },
704
- ],
705
- )
706
-
707
- with pytest.raises(
708
- AssertionError,
709
- match=re.escape(
710
- "Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
547
+ (
548
+ "ml_class_id",
549
+ "not_an_uuid",
550
+ "ml_class_id is not a valid uuid.",
551
+ ValueError,
711
552
  ),
712
- ):
713
- mock_elements_worker.create_classifications(
714
- element=elt,
715
- classifications=[
716
- {
717
- "ml_class_id": str(uuid4()),
718
- "confidence": 0.75,
719
- "high_confidence": False,
720
- },
721
- {
722
- "ml_class_id": str(uuid4()),
723
- "confidence": "wrong confidence",
724
- "high_confidence": False,
725
- },
726
- ],
727
- )
728
-
729
- with pytest.raises(
730
- AssertionError,
731
- match=re.escape(
732
- "Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
553
+ # Wrong classifications > confidence
554
+ (
555
+ "confidence",
556
+ DELETE_PARAMETER,
557
+ "confidence shouldn't be null and should be a float in [0..1] range",
558
+ AssertionError,
733
559
  ),
734
- ):
735
- mock_elements_worker.create_classifications(
736
- element=elt,
737
- classifications=[
738
- {
739
- "ml_class_id": str(uuid4()),
740
- "confidence": 0.75,
741
- "high_confidence": False,
742
- },
743
- {
744
- "ml_class_id": str(uuid4()),
745
- "confidence": 0,
746
- "high_confidence": False,
747
- },
748
- ],
749
- )
750
-
751
- with pytest.raises(
752
- AssertionError,
753
- match=re.escape(
754
- "Classification at index 1 in classifications: confidence shouldn't be null and should be a float in [0..1] range"
560
+ (
561
+ "confidence",
562
+ None,
563
+ "confidence shouldn't be null and should be a float in [0..1] range",
564
+ AssertionError,
755
565
  ),
756
- ):
757
- mock_elements_worker.create_classifications(
758
- element=elt,
759
- classifications=[
760
- {
761
- "ml_class_id": str(uuid4()),
762
- "confidence": 0.75,
763
- "high_confidence": False,
764
- },
765
- {
766
- "ml_class_id": str(uuid4()),
767
- "confidence": 2.00,
768
- "high_confidence": False,
769
- },
770
- ],
771
- )
566
+ (
567
+ "confidence",
568
+ "wrong confidence",
569
+ "confidence shouldn't be null and should be a float in [0..1] range",
570
+ AssertionError,
571
+ ),
572
+ (
573
+ "confidence",
574
+ 0,
575
+ "confidence shouldn't be null and should be a float in [0..1] range",
576
+ AssertionError,
577
+ ),
578
+ (
579
+ "confidence",
580
+ 2.00,
581
+ "confidence shouldn't be null and should be a float in [0..1] range",
582
+ AssertionError,
583
+ ),
584
+ # Wrong classifications > high_confidence
585
+ (
586
+ "high_confidence",
587
+ "wrong high_confidence",
588
+ "high_confidence should be of type bool",
589
+ AssertionError,
590
+ ),
591
+ ],
592
+ )
593
+ def test_create_classifications_wrong_classifications_data(
594
+ arg_name, data, error_message, error_type, mock_elements_worker
595
+ ):
596
+ all_data = {
597
+ "element": Element({"id": "12341234-1234-1234-1234-123412341234"}),
598
+ "classifications": [
599
+ {
600
+ "ml_class_id": str(uuid4()),
601
+ "confidence": 0.75,
602
+ "high_confidence": False,
603
+ },
604
+ {
605
+ "ml_class_id": str(uuid4()),
606
+ "confidence": 0.25,
607
+ "high_confidence": False,
608
+ # Overwrite with wrong data
609
+ arg_name: data,
610
+ },
611
+ ],
612
+ }
613
+ if data == DELETE_PARAMETER:
614
+ del all_data["classifications"][1][arg_name]
772
615
 
773
616
  with pytest.raises(
774
- AssertionError,
617
+ error_type,
775
618
  match=re.escape(
776
- "Classification at index 1 in classifications: high_confidence should be of type bool"
619
+ f"Classification at index 1 in classifications: {error_message}"
777
620
  ),
778
621
  ):
779
- mock_elements_worker.create_classifications(
780
- element=elt,
781
- classifications=[
782
- {
783
- "ml_class_id": str(uuid4()),
784
- "confidence": 0.75,
785
- "high_confidence": False,
786
- },
787
- {
788
- "ml_class_id": str(uuid4()),
789
- "confidence": 0.25,
790
- "high_confidence": "wrong high_confidence",
791
- },
792
- ],
793
- )
622
+ mock_elements_worker.create_classifications(**all_data)
794
623
 
795
624
 
796
625
  def test_create_classifications_api_error(responses, mock_elements_worker):