-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
test_jobs.py
1492 lines (1300 loc) · 53.8 KB
/
test_jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2021-2022 Intel Corporation
# Copyright (C) 2022-2023 CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import json
import os
import xml.etree.ElementTree as ET
import zipfile
from copy import deepcopy
from http import HTTPStatus
from io import BytesIO
from typing import Any, Dict, List, Optional
import numpy as np
import pytest
from cvat_sdk import models
from cvat_sdk.api_client.api_client import ApiClient, Endpoint
from cvat_sdk.core.helpers import get_paginated_collection
from deepdiff import DeepDiff
from PIL import Image
from shared.utils.config import make_api_client
from shared.utils.helpers import generate_image_files
from .utils import CollectionSimpleFilterTestBase, compare_annotations, create_task, export_dataset
def get_job_staff(job, tasks, projects):
job_staff = []
job_staff.append(job["assignee"])
tid = job["task_id"]
job_staff.append(tasks[tid]["owner"])
job_staff.append(tasks[tid]["assignee"])
pid = job["project_id"]
if pid:
job_staff.append(projects[pid]["owner"])
job_staff.append(projects[pid]["assignee"])
job_staff = set(u["id"] for u in job_staff if u is not None)
return job_staff
def filter_jobs(jobs, tasks, org):
if isinstance(org, int):
kwargs = {"org_id": org}
jobs = [job for job in jobs if tasks[job["task_id"]]["organization"] == org]
elif org == "":
kwargs = {"org": ""}
jobs = [job for job in jobs if tasks[job["task_id"]]["organization"] is None]
else:
kwargs = {}
jobs = jobs.raw
return jobs, kwargs
@pytest.mark.usefixtures("restore_db_per_function")
class TestPostJobs:
def _test_create_job_ok(self, user: str, data: Dict[str, Any], **kwargs):
with make_api_client(user) as api_client:
(_, response) = api_client.jobs_api.create(
models.JobWriteRequest(**deepcopy(data)), **kwargs
)
assert response.status == HTTPStatus.CREATED
return response
def _test_create_job_fails(
self, user: str, data: Dict[str, Any], *, expected_status: int, **kwargs
):
with make_api_client(user) as api_client:
(_, response) = api_client.jobs_api.create(
models.JobWriteRequest(**deepcopy(data)),
**kwargs,
_check_status=False,
_parse_response=False,
)
assert response.status == expected_status
return response
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
def test_can_create_gt_job_with_manual_frames(self, admin_user, tasks, task_mode):
user = admin_user
job_frame_count = 4
task = next(
t
for t in tasks
if not t["project_id"]
and not t["organization"]
and t["mode"] == task_mode
and t["size"] > job_frame_count
)
task_id = task["id"]
with make_api_client(user) as api_client:
(task_meta, _) = api_client.tasks_api.retrieve_data_meta(task_id)
frame_step = int(task_meta.frame_filter.split("=")[-1]) if task_meta.frame_filter else 1
job_frame_ids = list(range(task_meta.start_frame, task_meta.stop_frame, frame_step))[
:job_frame_count
]
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "manual",
"frames": job_frame_ids,
}
response = self._test_create_job_ok(user, job_spec)
job_id = json.loads(response.data)["id"]
with make_api_client(user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(job_id)
assert job_frame_count == gt_job_meta.size
assert job_frame_ids == gt_job_meta.included_frames
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
def test_can_create_gt_job_with_random_frames(self, admin_user, tasks, task_mode):
user = admin_user
job_frame_count = 3
required_task_frame_count = job_frame_count + 1
task = next(
t
for t in tasks
if not t["project_id"]
and not t["organization"]
and t["mode"] == task_mode
and t["size"] > required_task_frame_count
)
task_id = task["id"]
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": job_frame_count,
}
response = self._test_create_job_ok(user, job_spec)
job_id = json.loads(response.data)["id"]
with make_api_client(user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(job_id)
assert job_frame_count == gt_job_meta.size
assert job_frame_count == len(gt_job_meta.included_frames)
@pytest.mark.parametrize(
"task_id, frame_ids",
[
# The results have to be the same in different CVAT revisions,
# so the task ids are fixed
(21, [3, 5, 7]), # annotation task
(5, [11, 14, 20]), # interpolation task
],
)
def test_can_create_gt_job_with_random_frames_and_seed(self, admin_user, task_id, frame_ids):
user = admin_user
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 3,
"seed": 42,
}
response = self._test_create_job_ok(user, job_spec)
job_id = json.loads(response.data)["id"]
with make_api_client(user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(job_id)
assert frame_ids == gt_job_meta.included_frames
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
def test_can_create_gt_job_with_all_frames(self, admin_user, tasks, jobs, task_mode):
user = admin_user
task = next(
t
for t in tasks
if t["mode"] == task_mode
and t["size"]
and not any(j for j in jobs if j["task_id"] == t["id"] and j["type"] == "ground_truth")
)
task_id = task["id"]
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": task["size"],
}
response = self._test_create_job_ok(user, job_spec)
job_id = json.loads(response.data)["id"]
with make_api_client(user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(job_id)
assert task["size"] == gt_job_meta.size
def test_can_create_no_more_than_1_gt_job(self, admin_user, jobs):
user = admin_user
task_id = next(j for j in jobs if j["type"] == "ground_truth")["task_id"]
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
response = self._test_create_job_fails(
user, job_spec, expected_status=HTTPStatus.BAD_REQUEST
)
assert b"A task can have only 1 ground truth job" in response.data
def test_can_create_gt_job_in_sandbox_task(self, tasks, jobs, users):
task = next(
t
for t in tasks
if t["organization"] is None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and not users[t["owner"]["id"]]["is_superuser"]
)
user = task["owner"]["username"]
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
self._test_create_job_ok(user, job_spec)
@pytest.mark.parametrize(
"org_role, is_staff, allow",
[
("owner", True, True),
("owner", False, True),
("maintainer", True, True),
("maintainer", False, True),
("supervisor", True, True),
("supervisor", False, False),
("worker", True, False),
("worker", False, False),
],
)
def test_create_gt_job_in_org_task(
self, tasks, jobs, users, is_org_member, is_task_staff, org_role, is_staff, allow
):
for user in users:
if user["is_superuser"]:
continue
task = next(
(
t
for t in tasks
if t["organization"] is not None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and is_task_staff(user["id"], t["id"]) == is_staff
and is_org_member(user["id"], t["organization"], role=org_role)
),
None,
)
if task is not None:
break
assert task
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
if allow:
self._test_create_job_ok(user["username"], job_spec)
else:
self._test_create_job_fails(
user["username"], job_spec, expected_status=HTTPStatus.FORBIDDEN
)
def test_create_response_matches_get(self, tasks, jobs, users):
task = next(
t
for t in tasks
if t["organization"] is None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and not users[t["owner"]["id"]]["is_superuser"]
)
user = task["owner"]["username"]
spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
response = self._test_create_job_ok(user, spec)
job = json.loads(response.data)
with make_api_client(user) as api_client:
(_, response) = api_client.jobs_api.retrieve(job["id"])
assert DeepDiff(job, json.loads(response.data), ignore_order=True) == {}
@pytest.mark.usefixtures("restore_db_per_function")
class TestDeleteJobs:
def _test_destroy_job_ok(self, user, job_id, **kwargs):
with make_api_client(user) as api_client:
(_, response) = api_client.jobs_api.destroy(job_id, **kwargs)
assert response.status == HTTPStatus.NO_CONTENT
def _test_destroy_job_fails(self, user, job_id, *, expected_status: int, **kwargs):
with make_api_client(user) as api_client:
(_, response) = api_client.jobs_api.destroy(
job_id, **kwargs, _check_status=False, _parse_response=False
)
assert response.status == expected_status
return response
@pytest.mark.usefixtures("restore_cvat_data")
@pytest.mark.parametrize("job_type, allow", (("ground_truth", True), ("annotation", False)))
def test_destroy_job(self, admin_user, jobs, job_type, allow):
job = next(j for j in jobs if j["type"] == job_type)
if allow:
self._test_destroy_job_ok(admin_user, job["id"])
else:
self._test_destroy_job_fails(
admin_user, job["id"], expected_status=HTTPStatus.BAD_REQUEST
)
def test_can_destroy_gt_job_in_sandbox_task(self, tasks, jobs, users, admin_user):
task = next(
t
for t in tasks
if t["organization"] is None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and not users[t["owner"]["id"]]["is_superuser"]
)
user = task["owner"]["username"]
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
with make_api_client(admin_user) as api_client:
(job, _) = api_client.jobs_api.create(job_spec)
self._test_destroy_job_ok(user, job.id)
@pytest.mark.parametrize(
"org_role, is_staff, allow",
[
("owner", True, True),
("owner", False, True),
("maintainer", True, True),
("maintainer", False, True),
("supervisor", True, True),
("supervisor", False, False),
("worker", True, False),
("worker", False, False),
],
)
def test_destroy_gt_job_in_org_task(
self,
tasks,
jobs,
users,
is_org_member,
is_task_staff,
org_role,
is_staff,
allow,
admin_user,
):
for user in users:
task = next(
(
t
for t in tasks
if t["organization"] is not None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and is_task_staff(user["id"], t["id"]) == is_staff
and is_org_member(user["id"], t["organization"], role=org_role)
),
None,
)
if task is not None:
break
assert task
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
with make_api_client(admin_user) as api_client:
(job, _) = api_client.jobs_api.create(job_spec)
if allow:
self._test_destroy_job_ok(user["username"], job.id)
else:
self._test_destroy_job_fails(
user["username"], job.id, expected_status=HTTPStatus.FORBIDDEN
)
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetJobs:
def _test_get_job_200(
self, user, jid, *, expected_data: Optional[Dict[str, Any]] = None, **kwargs
):
with make_api_client(user) as client:
(_, response) = client.jobs_api.retrieve(jid, **kwargs)
assert response.status == HTTPStatus.OK
if expected_data is not None:
assert compare_annotations(expected_data, json.loads(response.data)) == {}
def _test_get_job_403(self, user, jid, **kwargs):
with make_api_client(user) as client:
(_, response) = client.jobs_api.retrieve(
jid, **kwargs, _check_status=False, _parse_response=False
)
assert response.status == HTTPStatus.FORBIDDEN
def test_admin_can_get_sandbox_job(self, admin_user, jobs, tasks):
job = next(job for job in jobs if tasks[job["task_id"]]["organization"] is None)
self._test_get_job_200(admin_user, job["id"], expected_data=job)
def test_admin_can_get_org_job(self, admin_user, jobs, tasks):
job = next(job for job in jobs if tasks[job["task_id"]]["organization"] is not None)
self._test_get_job_200(admin_user, job["id"], expected_data=job)
@pytest.mark.parametrize("groups", [["business"], ["user"]])
def test_non_admin_org_staff_can_get_job(
self, groups, users, organizations, org_staff, jobs_by_org
):
user, org_id = next(
(user, org["id"])
for user in users
for org in organizations
if user["groups"] == groups and user["id"] in org_staff(org["id"])
)
job = jobs_by_org[org_id][0]
self._test_get_job_200(user["username"], job["id"], expected_data=job)
@pytest.mark.parametrize("groups", [["business"], ["user"], ["worker"]])
def test_non_admin_job_staff_can_get_job(self, groups, users, jobs, is_job_staff):
user, job = next(
(user, job)
for user in users
for job in jobs
if user["groups"] == groups and is_job_staff(user["id"], job["id"])
)
self._test_get_job_200(user["username"], job["id"], expected_data=job)
@pytest.mark.parametrize("groups", [["business"], ["user"], ["worker"]])
def test_non_admin_non_job_staff_non_org_staff_cannot_get_job(
self, groups, users, organizations, org_staff, jobs, is_job_staff
):
user, job_id = next(
(user, job["id"])
for user in users
for org in organizations
for job in jobs
if user["groups"] == groups
and user["id"] not in org_staff(org["id"])
and not is_job_staff(user["id"], job["id"])
)
self._test_get_job_403(user["username"], job_id)
@pytest.mark.usefixtures("restore_db_per_function")
def test_can_get_gt_job_in_sandbox_task(self, tasks, jobs, users, admin_user):
task = next(
t
for t in tasks
if t["organization"] is None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and not users[t["owner"]["id"]]["is_superuser"]
)
user = task["owner"]["username"]
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
with make_api_client(admin_user) as api_client:
(job, _) = api_client.jobs_api.create(job_spec)
self._test_get_job_200(user, job.id)
@pytest.mark.usefixtures("restore_db_per_function")
@pytest.mark.parametrize(
"org_role, is_staff, allow",
[
("owner", True, True),
("owner", False, True),
("maintainer", True, True),
("maintainer", False, True),
("supervisor", True, True),
("supervisor", False, False),
("worker", True, True),
("worker", False, False),
],
)
def test_get_gt_job_in_org_task(
self,
tasks,
jobs,
users,
is_org_member,
is_task_staff,
org_role,
is_staff,
allow,
admin_user,
):
for user in users:
task = next(
(
t
for t in tasks
if t["organization"] is not None
and all(j["type"] != "ground_truth" for j in jobs if j["task_id"] == t["id"])
and is_task_staff(user["id"], t["id"]) == is_staff
and is_org_member(user["id"], t["organization"], role=org_role)
),
None,
)
if task is not None:
break
assert task
job_spec = {
"task_id": task["id"],
"type": "ground_truth",
"frame_selection_method": "random_uniform",
"frame_count": 1,
}
with make_api_client(admin_user) as api_client:
(_, response) = api_client.jobs_api.create(job_spec)
job = json.loads(response.data)
if allow:
self._test_get_job_200(user["username"], job["id"], expected_data=job)
else:
self._test_get_job_403(user["username"], job["id"])
@pytest.mark.usefixtures(
# if the db is restored per test, there are conflicts with the server data cache
# if we don't clean the db, the gt jobs created will be reused, and their
# ids won't conflict
"restore_db_per_class"
)
class TestGetGtJobData:
@pytest.mark.usefixtures("restore_db_per_function")
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
def test_can_get_gt_job_meta(self, admin_user, tasks, task_mode):
user = admin_user
job_frame_count = 4
task = next(
t
for t in tasks
if not t["project_id"]
and not t["organization"]
and t["mode"] == task_mode
and t["size"] > job_frame_count
)
task_id = task["id"]
with make_api_client(user) as api_client:
(task_meta, _) = api_client.tasks_api.retrieve_data_meta(task_id)
frame_step = int(task_meta.frame_filter.split("=")[-1]) if task_meta.frame_filter else 1
job_frame_ids = list(range(task_meta.start_frame, task_meta.stop_frame, frame_step))[
:job_frame_count
]
gt_job = self._get_or_create_gt_job(admin_user, task_id, job_frame_ids)
with make_api_client(user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(gt_job.id)
# These values are relative to the resulting task frames, unlike meta values
assert 0 == gt_job.start_frame
assert task_meta.size - 1 == gt_job.stop_frame
# The size is adjusted by the frame step and included frames
assert job_frame_count == gt_job_meta.size
assert job_frame_ids == gt_job_meta.included_frames
# The frames themselves are the same as in the whole range
# this is required by the UI implementation
assert task_meta.start_frame == gt_job_meta.start_frame
assert task_meta.stop_frame == gt_job_meta.stop_frame
if task_mode == "annotation":
assert (
len(gt_job_meta.frames)
== (gt_job_meta.stop_frame + 1 - gt_job_meta.start_frame) / frame_step
)
elif task_mode == "interpolation":
assert len(gt_job_meta.frames) == 1
else:
assert False
@pytest.mark.usefixtures("restore_db_per_function")
def test_can_get_gt_job_meta_with_complex_frame_setup(self, admin_user):
image_count = 50
start_frame = 3
stop_frame = image_count - 4
frame_step = 5
images = generate_image_files(image_count)
task_id, _ = create_task(
admin_user,
spec={
"name": "test complex frame setup",
"labels": [{"name": "cat"}],
},
data={
"image_quality": 75,
"start_frame": start_frame,
"stop_frame": stop_frame,
"frame_filter": f"step={frame_step}",
"client_files": images,
"sorting_method": "predefined",
},
)
task_frame_ids = range(start_frame, stop_frame, frame_step)
job_frame_ids = list(task_frame_ids[::3])
gt_job = self._get_or_create_gt_job(admin_user, task_id, job_frame_ids)
with make_api_client(admin_user) as api_client:
(gt_job_meta, _) = api_client.jobs_api.retrieve_data_meta(gt_job.id)
# These values are relative to the resulting task frames, unlike meta values
assert 0 == gt_job.start_frame
assert len(task_frame_ids) - 1 == gt_job.stop_frame
# The size is adjusted by the frame step and included frames
assert len(job_frame_ids) == gt_job_meta.size
assert job_frame_ids == gt_job_meta.included_frames
# The frames themselves are the same as in the whole range
# with placeholders in the frames outside the job.
# This is required by the UI implementation
assert start_frame == gt_job_meta.start_frame
assert max(task_frame_ids) == gt_job_meta.stop_frame
assert [frame_info["name"] for frame_info in gt_job_meta.frames] == [
images[frame].name if frame in job_frame_ids else "placeholder.jpg"
for frame in task_frame_ids
]
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
@pytest.mark.parametrize("quality", ["compressed", "original"])
def test_can_get_gt_job_chunk(self, admin_user, tasks, task_mode, quality):
user = admin_user
job_frame_count = 4
task = next(
t
for t in tasks
if not t["project_id"]
and not t["organization"]
and t["mode"] == task_mode
and t["size"] > job_frame_count
)
task_id = task["id"]
with make_api_client(user) as api_client:
(task_meta, _) = api_client.tasks_api.retrieve_data_meta(task_id)
frame_step = int(task_meta.frame_filter.split("=")[-1]) if task_meta.frame_filter else 1
job_frame_ids = list(range(task_meta.start_frame, task_meta.stop_frame, frame_step))[
:job_frame_count
]
gt_job = self._get_or_create_gt_job(admin_user, task_id, job_frame_ids)
with make_api_client(admin_user) as api_client:
(chunk_file, response) = api_client.jobs_api.retrieve_data(
gt_job.id, number=0, quality=quality, type="chunk"
)
assert response.status == HTTPStatus.OK
frame_range = range(
task_meta.start_frame, min(task_meta.stop_frame + 1, task_meta.chunk_size), frame_step
)
included_frames = job_frame_ids
# The frame count is the same as in the whole range
# with placeholders in the frames outside the job.
# This is required by the UI implementation
with zipfile.ZipFile(chunk_file) as chunk:
assert set(chunk.namelist()) == set("{:06d}.jpeg".format(i) for i in frame_range)
for file_info in chunk.filelist:
with chunk.open(file_info) as image_file:
image = Image.open(image_file)
image_data = np.array(image)
if int(os.path.splitext(file_info.filename)[0]) not in included_frames:
assert image.size == (1, 1)
assert np.all(image_data == 0), image_data
else:
assert image.size > (1, 1)
assert np.any(image_data != 0)
def _get_or_create_gt_job(self, user, task_id, frames):
with make_api_client(user) as api_client:
(task_jobs, _) = api_client.jobs_api.list(task_id=task_id, type="ground_truth")
if task_jobs.results:
gt_job = task_jobs.results[0]
else:
job_spec = {
"task_id": task_id,
"type": "ground_truth",
"frame_selection_method": "manual",
"frames": frames,
}
(gt_job, _) = api_client.jobs_api.create(job_spec)
return gt_job
@pytest.mark.parametrize("task_mode", ["annotation", "interpolation"])
@pytest.mark.parametrize("quality", ["compressed", "original"])
def test_can_get_gt_job_frame(self, admin_user, tasks, task_mode, quality):
user = admin_user
job_frame_count = 4
task = next(
t
for t in tasks
if not t["project_id"]
and not t["organization"]
and t["mode"] == task_mode
and t["size"] > job_frame_count
)
task_id = task["id"]
with make_api_client(user) as api_client:
(task_meta, _) = api_client.tasks_api.retrieve_data_meta(task_id)
frame_step = int(task_meta.frame_filter.split("=")[-1]) if task_meta.frame_filter else 1
job_frame_ids = list(range(task_meta.start_frame, task_meta.stop_frame, frame_step))[
:job_frame_count
]
gt_job = self._get_or_create_gt_job(admin_user, task_id, job_frame_ids)
frame_range = range(
task_meta.start_frame, min(task_meta.stop_frame + 1, task_meta.chunk_size), frame_step
)
included_frames = job_frame_ids
excluded_frames = list(set(frame_range).difference(included_frames))
with make_api_client(admin_user) as api_client:
(_, response) = api_client.jobs_api.retrieve_data(
gt_job.id,
number=excluded_frames[0],
quality=quality,
type="frame",
_parse_response=False,
_check_status=False,
)
assert response.status == HTTPStatus.BAD_REQUEST
assert b"The frame number doesn't belong to the job" in response.data
(_, response) = api_client.jobs_api.retrieve_data(
gt_job.id, number=included_frames[0], quality=quality, type="frame"
)
assert response.status == HTTPStatus.OK
@pytest.mark.usefixtures("restore_db_per_class")
class TestListJobs:
def _test_list_jobs_200(self, user, data, **kwargs):
with make_api_client(user) as client:
results = get_paginated_collection(
client.jobs_api.list_endpoint, return_json=True, **kwargs
)
assert compare_annotations(data, results) == {}
def _test_list_jobs_403(self, user, **kwargs):
with make_api_client(user) as client:
(_, response) = client.jobs_api.list(
**kwargs, _check_status=False, _parse_response=False
)
assert response.status == HTTPStatus.FORBIDDEN
@pytest.mark.parametrize("org", [None, "", 1, 2])
def test_admin_list_jobs(self, jobs, tasks, org):
jobs, kwargs = filter_jobs(jobs, tasks, org)
self._test_list_jobs_200("admin1", jobs, **kwargs)
@pytest.mark.parametrize("org_id", ["", None, 1, 2])
@pytest.mark.parametrize("groups", [["business"], ["user"], ["worker"], []])
def test_non_admin_list_jobs(
self, org_id, groups, users, jobs, tasks, projects, org_staff, is_org_member
):
users = [u for u in users if u["groups"] == groups][:2]
jobs, kwargs = filter_jobs(jobs, tasks, org_id)
org_staff = org_staff(org_id)
for user in users:
user_jobs = []
for job in jobs:
job_staff = get_job_staff(job, tasks, projects)
if user["id"] in job_staff | org_staff:
user_jobs.append(job)
if is_org_member(user["id"], org_id):
self._test_list_jobs_200(user["username"], user_jobs, **kwargs)
else:
self._test_list_jobs_403(user["username"], **kwargs)
class TestJobsListFilters(CollectionSimpleFilterTestBase):
field_lookups = {
"assignee": ["assignee", "username"],
}
@pytest.fixture(autouse=True)
def setup(self, restore_db_per_class, admin_user, jobs):
self.user = admin_user
self.samples = jobs
def _get_endpoint(self, api_client: ApiClient) -> Endpoint:
return api_client.jobs_api.list_endpoint
@pytest.mark.parametrize(
"field",
(
"assignee",
"state",
"stage",
"task_id",
"project_id",
"type",
),
)
def test_can_use_simple_filter_for_object_list(self, field):
return super().test_can_use_simple_filter_for_object_list(field)
@pytest.mark.usefixtures("restore_db_per_class")
class TestGetAnnotations:
def _test_get_job_annotations_200(self, user, jid, data):
with make_api_client(user) as client:
(_, response) = client.jobs_api.retrieve_annotations(jid)
assert response.status == HTTPStatus.OK
assert compare_annotations(data, json.loads(response.data)) == {}
def _test_get_job_annotations_403(self, user, jid):
with make_api_client(user) as client:
(_, response) = client.jobs_api.retrieve_annotations(
jid, _check_status=False, _parse_response=False
)
assert response.status == HTTPStatus.FORBIDDEN
@pytest.mark.parametrize("org", [""])
@pytest.mark.parametrize(
"groups, job_staff, expect_success",
[
(["admin"], True, True),
(["admin"], False, True),
(["business"], True, True),
(["business"], False, False),
(["worker"], True, True),
(["worker"], False, False),
(["user"], True, True),
(["user"], False, False),
],
)
def test_user_get_job_annotations(
self,
org,
groups,
job_staff,
expect_success,
users,
jobs,
tasks,
annotations,
find_job_staff_user,
):
users = [u for u in users if u["groups"] == groups]
jobs, _ = filter_jobs(jobs, tasks, org)
username, job_id = find_job_staff_user(jobs, users, job_staff)
if expect_success:
self._test_get_job_annotations_200(username, job_id, annotations["job"][str(job_id)])
else:
self._test_get_job_annotations_403(username, job_id)
@pytest.mark.parametrize("org", [2])
@pytest.mark.parametrize(
"role, job_staff, expect_success",
[
("owner", True, True),
("owner", False, True),
("maintainer", True, True),
("maintainer", False, True),
("supervisor", True, True),
("supervisor", False, False),
("worker", True, True),
("worker", False, False),
],
)
def test_member_get_job_annotations(
self,
org,
role,
job_staff,
expect_success,
jobs,
tasks,
find_job_staff_user,
annotations,
find_users,
):
users = find_users(org=org, role=role)
jobs, _ = filter_jobs(jobs, tasks, org)
username, jid = find_job_staff_user(jobs, users, job_staff)
if expect_success:
data = annotations["job"][str(jid)]
data["shapes"] = sorted(data["shapes"], key=lambda a: a["id"])
self._test_get_job_annotations_200(username, jid, data)
else:
self._test_get_job_annotations_403(username, jid)
@pytest.mark.parametrize("org", [1])
@pytest.mark.parametrize(
"privilege, expect_success",
[("admin", True), ("business", False), ("worker", False), ("user", False)],
)
def test_non_member_get_job_annotations(
self,
org,
privilege,
expect_success,
jobs,
tasks,
find_job_staff_user,
annotations,
find_users,
):
users = find_users(privilege=privilege, exclude_org=org)
jobs, _ = filter_jobs(jobs, tasks, org)
username, job_id = find_job_staff_user(jobs, users, False)
if expect_success:
self._test_get_job_annotations_200(username, job_id, annotations["job"][str(job_id)])
else:
self._test_get_job_annotations_403(username, job_id)
@pytest.mark.parametrize("job_type", ("ground_truth", "annotation"))
def test_can_get_annotations(self, admin_user, jobs, annotations, job_type):
job = next(j for j in jobs if j["type"] == job_type)
self._test_get_job_annotations_200(
admin_user, job["id"], annotations["job"][str(job["id"])]
)
@pytest.mark.usefixtures("restore_db_per_function")
class TestPatchJobAnnotations:
def _check_response(self, username, jid, expect_success, data=None):
with make_api_client(username) as client:
(_, response) = client.jobs_api.partial_update_annotations(
id=jid,
patched_labeled_data_request=deepcopy(data),
action="update",
_parse_response=expect_success,
_check_status=expect_success,
)
if expect_success:
assert response.status == HTTPStatus.OK
assert compare_annotations(data, json.loads(response.data)) == {}
else:
assert response.status == HTTPStatus.FORBIDDEN
@pytest.fixture(scope="class")
def request_data(self, annotations):