-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathconftest.py
More file actions
1308 lines (1076 loc) · 37.8 KB
/
conftest.py
File metadata and controls
1308 lines (1076 loc) · 37.8 KB
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
import json
import os
import re
import time
import uuid
from datetime import datetime
from enum import Enum
from random import randint
from string import ascii_letters
from types import SimpleNamespace
from typing import Tuple, Type
import pytest
import requests
from lbox.exceptions import LabelboxError
from labelbox import (
Classification,
Client,
DataRow,
Dataset,
MediaType,
OntologyBuilder,
Option,
Tool,
)
from labelbox.orm import query
from labelbox.pagination import PaginatedCollection
from labelbox.schema.annotation_import import LabelImport
from labelbox.schema.enums import AnnotationImportState
from labelbox.schema.invite import Invite
from labelbox.schema.ontology import Ontology
from labelbox.schema.project import Project
from labelbox.schema.quality_mode import QualityMode
IMG_URL = "https://picsum.photos/200/300.jpg"
SMALL_DATASET_URL = "https://storage.googleapis.com/lb-artifacts-testing-public/sdk_integration_test/potato.jpeg"
DATA_ROW_PROCESSING_WAIT_TIMEOUT_SECONDS = 30
DATA_ROW_PROCESSING_WAIT_SLEEP_INTERNAL_SECONDS = 3
EPHEMERAL_BASE_URL = "http://lb-api-public"
IMAGE_URL = "https://storage.googleapis.com/diagnostics-demo-data/coco/COCO_train2014_000000000034.jpg"
EXTERNAL_ID = "my-image"
pytest_plugins = []
@pytest.fixture(scope="session")
def rand_gen():
def gen(field_type):
if field_type is str:
return "".join(
ascii_letters[randint(0, len(ascii_letters) - 1)]
for _ in range(16)
)
if field_type is datetime:
return datetime.now()
raise Exception(
"Can't random generate for field type '%r'" % field_type
)
return gen
class Environ(Enum):
LOCAL = "local"
PROD = "prod"
STAGING = "staging"
CUSTOM = "custom"
STAGING_EU = "staging-eu"
EPHEMERAL = "ephemeral" # Used for testing PRs with ephemeral environments
@pytest.fixture
def image_url() -> str:
return IMG_URL
@pytest.fixture
def external_id() -> str:
return EXTERNAL_ID
def ephemeral_endpoint() -> str:
return os.getenv("LABELBOX_TEST_BASE_URL", EPHEMERAL_BASE_URL)
def graphql_url(environ: str) -> str:
if environ == Environ.LOCAL:
return "http://localhost:3000/api/graphql"
elif environ == Environ.PROD:
return "https://api.labelbox.com/graphql"
elif environ == Environ.STAGING:
return "https://api.lb-stage.xyz/graphql"
elif environ == Environ.CUSTOM:
graphql_api_endpoint = os.environ.get(
"LABELBOX_TEST_GRAPHQL_API_ENDPOINT"
)
if graphql_api_endpoint is None:
raise Exception("Missing LABELBOX_TEST_GRAPHQL_API_ENDPOINT")
return graphql_api_endpoint
elif environ == Environ.EPHEMERAL:
return f"{ephemeral_endpoint()}/graphql"
return "http://host.docker.internal:8080/graphql"
def rest_url(environ: str) -> str:
if environ == Environ.LOCAL:
return "http://localhost:3000/api/v1"
elif environ == Environ.PROD:
return "https://api.labelbox.com/api/v1"
elif environ == Environ.STAGING:
return "https://api.lb-stage.xyz/api/v1"
elif environ == Environ.CUSTOM:
rest_api_endpoint = os.environ.get("LABELBOX_TEST_REST_API_ENDPOINT")
if rest_api_endpoint is None:
raise Exception("Missing LABELBOX_TEST_REST_API_ENDPOINT")
return rest_api_endpoint
elif environ == Environ.EPHEMERAL:
return f"{ephemeral_endpoint()}/api/v1"
return "http://host.docker.internal:8080/api/v1"
def testing_api_key(environ: Environ) -> str:
keys = [
f"LABELBOX_TEST_API_KEY_{environ.value.upper()}",
"LABELBOX_TEST_API_KEY",
"LABELBOX_API_KEY",
]
for key in keys:
value = os.environ.get(key)
if value is not None:
return value
raise Exception("Cannot find API to use for tests")
def service_api_key() -> str:
service_api_key = os.environ["SERVICE_API_KEY"]
if service_api_key is None:
raise Exception(
"SERVICE_API_KEY is missing and needed for admin client"
)
return service_api_key
class IntegrationClient(Client):
def __init__(self, environ: str) -> None:
api_url = graphql_url(environ)
api_key = testing_api_key(environ)
rest_endpoint = rest_url(environ)
super().__init__(
api_key,
api_url,
enable_experimental=True,
rest_endpoint=rest_endpoint,
)
self.queries = []
def execute(self, query=None, params=None, check_naming=True, **kwargs):
if check_naming and query is not None:
assert (
re.match(r"\s*(?:query|mutation) \w+PyApi", query) is not None
)
self.queries.append((query, params))
if not kwargs.get("timeout"):
kwargs["timeout"] = 30.0
return super().execute(query, params, **kwargs)
class AdminClient(Client):
def __init__(self, env):
"""
The admin client creates organizations and users using admin api described here https://labelbox.atlassian.net/wiki/spaces/AP/pages/2206564433/Internal+Admin+APIs.
"""
self._api_key = service_api_key()
self._admin_endpoint = f"{ephemeral_endpoint()}/admin/v1"
self._api_url = graphql_url(env)
self._rest_endpoint = rest_url(env)
super().__init__(
self._api_key,
self._api_url,
enable_experimental=True,
rest_endpoint=self._rest_endpoint,
)
def _create_organization(self) -> str:
endpoint = f"{self._admin_endpoint}/organizations/"
response = requests.post(
endpoint,
headers=self.headers,
json={"name": f"Test Org {uuid.uuid4()}"},
)
data = response.json()
if response.status_code not in [
requests.codes.created,
requests.codes.ok,
]:
raise Exception(
"Failed to create org, message: " + str(data["message"])
)
return data["id"]
def _create_user(self, organization_id=None) -> Tuple[str, str]:
if organization_id is None:
organization_id = self.organization_id
endpoint = f"{self._admin_endpoint}/user-identities/"
identity_id = f"e2e+{uuid.uuid4()}"
response = requests.post(
endpoint,
headers=self.headers,
json={
"identityId": identity_id,
"email": "email@email.com",
"name": f"tester{uuid.uuid4()}",
"verificationStatus": "VERIFIED",
},
)
data = response.json()
if response.status_code not in [
requests.codes.created,
requests.codes.ok,
]:
raise Exception(
"Failed to create user, message: " + str(data["message"])
)
user_identity_id = data["identityId"]
endpoint = (
f"{self._admin_endpoint}/organizations/{organization_id}/users/"
)
response = requests.post(
endpoint,
headers=self.headers,
json={"identityId": user_identity_id, "organizationRole": "Admin"},
)
data = response.json()
if response.status_code not in [
requests.codes.created,
requests.codes.ok,
]:
raise Exception(
"Failed to create link user to org, message: "
+ str(data["message"])
)
user_id = data["id"]
endpoint = f"{self._admin_endpoint}/users/{user_id}/token"
response = requests.get(
endpoint,
headers=self.headers,
)
data = response.json()
if response.status_code not in [
requests.codes.created,
requests.codes.ok,
]:
raise Exception(
"Failed to create ephemeral user, message: "
+ str(data["message"])
)
token = data["token"]
return user_id, token
def create_api_key_for_user(self) -> str:
organization_id = self._create_organization()
_, user_token = self._create_user(organization_id)
key_name = f"test-key+{uuid.uuid4()}"
query = """mutation CreateApiKeyPyApi($name: String!) {
createApiKey(data: {name: $name}) {
id
jwt
}
}
"""
params = {"name": key_name}
self.headers["Authorization"] = f"Bearer {user_token}"
res = self.execute(query, params, error_log_key="errors")
return res["createApiKey"]["jwt"]
class EphemeralClient(Client):
def __init__(self, environ=Environ.EPHEMERAL):
self.admin_client = AdminClient(environ)
self.api_key = self.admin_client.create_api_key_for_user()
api_url = graphql_url(environ)
rest_endpoint = rest_url(environ)
super().__init__(
self.api_key,
api_url,
enable_experimental=True,
rest_endpoint=rest_endpoint,
)
@pytest.fixture
def ephmeral_client() -> EphemeralClient:
return EphemeralClient
@pytest.fixture
def admin_client() -> AdminClient:
return AdminClient
@pytest.fixture
def integration_client() -> IntegrationClient:
return IntegrationClient
@pytest.fixture(scope="session")
def environ() -> Environ:
"""
Checks environment variables for LABELBOX_ENVIRON to be
'prod' or 'staging'
Make sure to set LABELBOX_TEST_ENVIRON in .github/workflows/python-package.yaml
"""
keys = ["LABELBOX_TEST_ENV", "LABELBOX_TEST_ENVIRON", "LABELBOX_ENV"]
for key in keys:
value = os.environ.get(key)
if value is not None:
return Environ(value)
raise Exception(f"Missing env key in: {os.environ}")
def cancel_invite(client, invite_id):
"""
Do not use. Only for testing.
"""
query_str = """mutation CancelInvitePyApi($where: WhereUniqueIdInput!) {
cancelInvite(where: $where) {id}}"""
client.execute(query_str, {"where": {"id": invite_id}}, experimental=True)
def get_project_invites(client, project_id):
"""
Do not use. Only for testing.
"""
id_param = "projectId"
query_str = """query GetProjectInvitationsPyApi($from: ID, $first: PageSize, $%s: ID!) {
project(where: {id: $%s}) {id
invites(from: $from, first: $first) { nodes { %s
projectInvites { projectId projectRoleName } } nextCursor}}}
""" % (id_param, id_param, query.results_query_part(Invite))
return PaginatedCollection(
client,
query_str,
{id_param: project_id},
["project", "invites", "nodes"],
Invite,
cursor_path=["project", "invites", "nextCursor"],
)
def get_invites(client):
"""
Do not use. Only for testing.
"""
query_str = """query GetOrgInvitationsPyApi($from: ID, $first: PageSize) {
organization { id invites(from: $from, first: $first) {
nodes { id createdAt organizationRoleName inviteeEmail } nextCursor }}}"""
invites = PaginatedCollection(
client,
query_str,
{},
["organization", "invites", "nodes"],
Invite,
cursor_path=["organization", "invites", "nextCursor"],
experimental=True,
)
return invites
@pytest.fixture
def queries():
return SimpleNamespace(
cancel_invite=cancel_invite,
get_project_invites=get_project_invites,
get_invites=get_invites,
)
@pytest.fixture(scope="session")
def admin_client(environ: str):
return AdminClient(environ)
@pytest.fixture(scope="session")
def client(environ: str):
if environ == Environ.EPHEMERAL:
return EphemeralClient()
return IntegrationClient(environ)
@pytest.fixture(scope="session")
def pdf_url(client):
pdf_url = client.upload_file("tests/assets/loremipsum.pdf")
return {
"row_data": {
"pdf_url": pdf_url,
},
"global_key": str(uuid.uuid4()),
}
@pytest.fixture(scope="session")
def pdf_entity_data_row(client):
pdf_url = client.upload_file(
"tests/assets/arxiv-pdf_data_99-word-token-pdfs_0801.3483.pdf"
)
text_layer_url = client.upload_file(
"tests/assets/arxiv-pdf_data_99-word-token-pdfs_0801.3483-lb-textlayer.json"
)
return {
"row_data": {"pdf_url": pdf_url, "text_layer_url": text_layer_url},
"global_key": str(uuid.uuid4()),
}
@pytest.fixture()
def conversation_entity_data_row(client, rand_gen):
return {
"row_data": "https://storage.googleapis.com/labelbox-developer-testing-assets/conversational_text/1000-conversations/conversation-1.json",
"global_key": f"https://storage.googleapis.com/labelbox-developer-testing-assets/conversational_text/1000-conversations/conversation-1.json-{rand_gen(str)}",
}
@pytest.fixture
def project(client, rand_gen):
project = client.create_project(
name=rand_gen(str),
media_type=MediaType.Image,
)
yield project
project.delete()
@pytest.fixture
def consensus_project(client, rand_gen):
project = client.create_project(
name=rand_gen(str),
quality_modes={QualityMode.Consensus},
media_type=MediaType.Image,
)
yield project
project.delete()
@pytest.fixture
def model_config(client, rand_gen, valid_model_id):
model_config = client.create_model_config(
name=rand_gen(str),
model_id=valid_model_id,
inference_params={"param": "value"},
)
yield model_config
client.delete_model_config(model_config.uid)
@pytest.fixture
def consensus_project_with_batch(
consensus_project, initial_dataset, rand_gen, image_url
):
project = consensus_project
dataset = initial_dataset
data_rows = []
for _ in range(3):
data_rows.append(
{DataRow.row_data: image_url, DataRow.global_key: str(uuid.uuid4())}
)
task = dataset.create_data_rows(data_rows)
task.wait_till_done()
assert task.status == "COMPLETE"
data_rows = list(dataset.data_rows())
assert len(data_rows) == 3
batch = project.create_batch(
rand_gen(str),
data_rows, # sample of data row objects
5, # priority between 1(Highest) - 5(lowest)
)
yield [project, batch, data_rows]
batch.delete()
@pytest.fixture
def dataset(client, rand_gen):
# Handle invalid default IAM integrations in test environments gracefully
dataset = create_dataset_robust(client, name=rand_gen(str))
yield dataset
dataset.delete()
@pytest.fixture(scope="function")
def unique_dataset(client, rand_gen):
# Handle invalid default IAM integrations in test environments gracefully
dataset = create_dataset_robust(client, name=rand_gen(str))
yield dataset
dataset.delete()
@pytest.fixture
def small_dataset(dataset: Dataset):
task = dataset.create_data_rows(
[
{"row_data": SMALL_DATASET_URL, "external_id": "my-image"},
]
* 2
)
task.wait_till_done()
yield dataset
@pytest.fixture
def data_row(dataset, image_url, rand_gen):
global_key = f"global-key-{rand_gen(str)}"
task = dataset.create_data_rows(
[
{
"row_data": image_url,
"external_id": "my-image",
"global_key": global_key,
},
]
)
task.wait_till_done()
dr = dataset.data_rows().get_one()
yield dr
dr.delete()
@pytest.fixture
def data_row_and_global_key(dataset, image_url, rand_gen):
global_key = f"global-key-{rand_gen(str)}"
task = dataset.create_data_rows(
[
{
"row_data": image_url,
"external_id": "my-image",
"global_key": global_key,
},
]
)
task.wait_till_done()
dr = dataset.data_rows().get_one()
yield dr, global_key
dr.delete()
# can be used with
# @pytest.mark.parametrize('data_rows', [<count of data rows>], indirect=True)
# if omitted, count defaults to 1
@pytest.fixture
def data_rows(
dataset, image_url, request, wait_for_data_row_processing, client
):
count = 1
if hasattr(request, "param"):
count = request.param
datarows = [
dict(row_data=image_url, global_key=f"global-key-{uuid.uuid4()}")
for _ in range(count)
]
task = dataset.create_data_rows(datarows)
task.wait_till_done()
datarows = dataset.data_rows().get_many(count)
for dr in dataset.data_rows():
wait_for_data_row_processing(client, dr)
yield datarows
for datarow in datarows:
datarow.delete()
@pytest.fixture
def iframe_url(environ) -> str:
if environ in [Environ.PROD, Environ.LOCAL]:
return "https://editor.labelbox.com"
elif environ == Environ.STAGING:
return "https://editor.lb-stage.xyz"
@pytest.fixture
def sample_image() -> str:
path_to_video = "tests/integration/media/sample_image.jpg"
return path_to_video
@pytest.fixture
def sample_video() -> str:
path_to_video = "tests/integration/media/cat.mp4"
return path_to_video
@pytest.fixture
def sample_bulk_conversation() -> list:
path_to_conversation = "tests/integration/media/bulk_conversation.json"
with open(path_to_conversation) as json_file:
conversations = json.load(json_file)
return conversations
@pytest.fixture
def organization(client):
# Must have at least one seat open in your org to run these tests
org = client.get_organization()
yield org
@pytest.fixture
def configured_project_with_label(
client,
rand_gen,
dataset,
data_row,
wait_for_label_processing,
teardown_helpers,
):
"""Project with a connected dataset, having one datarow
Project contains an ontology with 1 bbox tool
Additionally includes a create_label method for any needed extra labels
One label is already created and yielded when using fixture
"""
project = client.create_project(
name=rand_gen(str),
media_type=MediaType.Image,
)
project._wait_until_data_rows_are_processed(
data_row_ids=[data_row.uid],
wait_processing_max_seconds=DATA_ROW_PROCESSING_WAIT_TIMEOUT_SECONDS,
sleep_interval=DATA_ROW_PROCESSING_WAIT_SLEEP_INTERNAL_SECONDS,
)
project.create_batch(
rand_gen(str),
[data_row.uid], # sample of data row objects
5, # priority between 1(Highest) - 5(lowest)
)
ontology = _setup_ontology(project, client)
label = _create_label(
project, data_row, ontology, wait_for_label_processing
)
yield [project, dataset, data_row, label]
teardown_helpers.teardown_project_labels_ontology_feature_schemas(project)
def _create_label(project, data_row, ontology, wait_for_label_processing):
predictions = [
{
"uuid": str(uuid.uuid4()),
"schemaId": ontology.tools[0].feature_schema_id,
"dataRow": {"id": data_row.uid},
"bbox": {"top": 20, "left": 20, "height": 50, "width": 50},
}
]
def create_label():
"""Ad-hoc function to create a LabelImport
Creates a LabelImport task which will create a label
"""
upload_task = LabelImport.create_from_objects(
project.client,
project.uid,
f"label-import-{uuid.uuid4()}",
predictions,
)
upload_task.wait_until_done(sleep_time_seconds=5)
assert (
upload_task.state == AnnotationImportState.FINISHED
), "Label Import did not finish"
assert (
len(upload_task.errors) == 0
), f"Label Import {upload_task.name} failed with errors {upload_task.errors}"
project.create_label = create_label
project.create_label()
label = wait_for_label_processing(project)[0]
return label
def _setup_ontology(project: Project, client: Client):
ontology_builder = OntologyBuilder(
tools=[
Tool(tool=Tool.Type.BBOX, name="test-bbox-class"),
]
)
ontology = client.create_ontology(
name="ontology with features",
media_type=MediaType.Image,
normalized=ontology_builder.asdict(),
)
project.connect_ontology(ontology)
return OntologyBuilder.from_project(project)
@pytest.fixture
def big_dataset(dataset: Dataset):
task = dataset.create_data_rows(
[
{"row_data": IMAGE_URL, "external_id": EXTERNAL_ID},
]
* 3
)
task.wait_till_done()
yield dataset
@pytest.fixture
def configured_batch_project_with_label(
client,
dataset,
data_row,
wait_for_label_processing,
rand_gen,
teardown_helpers,
):
"""Project with a batch having one datarow
Project contains an ontology with 1 bbox tool
Additionally includes a create_label method for any needed extra labels
One label is already created and yielded when using fixture
"""
project = client.create_project(
name=rand_gen(str),
media_type=MediaType.Image,
)
data_rows = [dr.uid for dr in list(dataset.data_rows())]
project._wait_until_data_rows_are_processed(
data_row_ids=data_rows, sleep_interval=3
)
project.create_batch("test-batch", data_rows)
project.data_row_ids = data_rows
ontology = _setup_ontology(project, client)
label = _create_label(
project, data_row, ontology, wait_for_label_processing
)
yield [project, dataset, data_row, label]
teardown_helpers.teardown_project_labels_ontology_feature_schemas(project)
@pytest.fixture
def configured_batch_project_with_multiple_datarows(
client,
dataset,
data_rows,
wait_for_label_processing,
rand_gen,
teardown_helpers,
):
"""Project with a batch having multiple datarows
Project contains an ontology with 1 bbox tool
Additionally includes a create_label method for any needed extra labels
"""
project = client.create_project(
name=rand_gen(str),
media_type=MediaType.Image,
)
global_keys = [dr.global_key for dr in data_rows]
batch_name = f"batch {uuid.uuid4()}"
project.create_batch(batch_name, global_keys=global_keys)
ontology = _setup_ontology(project, client)
for datarow in data_rows:
_create_label(project, datarow, ontology, wait_for_label_processing)
yield [project, dataset, data_rows]
teardown_helpers.teardown_project_labels_ontology_feature_schemas(project)
# NOTE this is nice heuristics, also there is this logic _wait_until_data_rows_are_processed in Project
# in case we still have flakiness in the future, we can use it
@pytest.fixture
def wait_for_data_row_processing():
"""
Do not use. Only for testing.
Returns DataRow after waiting for it to finish processing media_attributes.
Some tests, specifically ones that rely on label export, rely on
DataRow be fully processed with media_attributes
"""
def func(client, data_row, custom_check=None):
"""
added check_updated_at because when a data_row is updated from say
an image to pdf, it already has media_attributes and the loop does
not wait for processing to a pdf
"""
data_row_id = data_row.uid
timeout_seconds = 60
while True:
data_row = client.get_data_row(data_row_id)
passed_custom_check = not custom_check or custom_check(data_row)
if data_row.media_attributes and passed_custom_check:
return data_row
timeout_seconds -= 2
if timeout_seconds <= 0:
raise TimeoutError(
f"Timed out waiting for DataRow '{data_row_id}' to finish processing media_attributes"
)
time.sleep(2)
return func
@pytest.fixture
def wait_for_label_processing():
"""
Do not use. Only for testing.
Returns project's labels as a list after waiting for them to finish processing.
If `project.labels()` is called before label is fully processed,
it may return an empty set
"""
def func(project):
timeout_seconds = 10
while True:
labels = list(project.labels())
if len(labels) > 0:
return labels
timeout_seconds -= 2
if timeout_seconds <= 0:
raise TimeoutError(
f"Timed out waiting for label for project '{project.uid}' to finish processing"
)
time.sleep(2)
return func
@pytest.fixture
def initial_dataset(client, rand_gen):
# Handle invalid default IAM integrations in test environments gracefully
dataset = create_dataset_robust(client, name=rand_gen(str))
yield dataset
dataset.delete()
@pytest.fixture
def video_data(client, rand_gen, video_data_row, wait_for_data_row_processing):
# Handle invalid default IAM integrations in test environments gracefully
dataset = create_dataset_robust(client, name=rand_gen(str))
data_row_ids = []
data_row = dataset.create_data_row(video_data_row)
data_row = wait_for_data_row_processing(client, data_row)
data_row_ids.append(data_row.uid)
yield dataset, data_row_ids
dataset.delete()
def create_video_data_row(rand_gen):
return {
"row_data": "https://storage.googleapis.com/lb-test-data/cataflow/media/test_video_500kb.mp4",
"global_key": f"https://storage.googleapis.com/lb-test-data/cataflow/media/test_video_500kb.mp4-{rand_gen(str)}",
"media_type": "VIDEO",
}
@pytest.fixture
def video_data_100_rows(client, rand_gen, wait_for_data_row_processing):
# Handle invalid default IAM integrations in test environments gracefully
dataset = create_dataset_robust(client, name=rand_gen(str))
data_row_ids = []
for _ in range(100):
data_row = dataset.create_data_row(create_video_data_row(rand_gen))
data_row = wait_for_data_row_processing(client, data_row)
data_row_ids.append(data_row.uid)
yield dataset, data_row_ids
dataset.delete()
@pytest.fixture()
def video_data_row(rand_gen):
return create_video_data_row(rand_gen)
class ExportV2Helpers:
@classmethod
def run_project_export_v2_task(
cls, project, num_retries=5, task_name=None, filters={}, params={}
):
task = None
params = (
params
if params
else {
"project_details": True,
"performance_details": False,
"data_row_details": True,
"label_details": True,
}
)
while num_retries > 0:
task = project.export_v2(
task_name=task_name, filters=filters, params=params
)
task.wait_till_done()
assert task.status == "COMPLETE"
assert task.errors is None
if len(task.result) == 0:
num_retries -= 1
time.sleep(5)
else:
break
return task.result
@classmethod
def run_dataset_export_v2_task(
cls, dataset, num_retries=5, task_name=None, filters={}, params={}
):
task = None
params = (
params
if params
else {"performance_details": False, "label_details": True}
)
while num_retries > 0:
task = dataset.export_v2(
task_name=task_name, filters=filters, params=params
)
task.wait_till_done()
assert task.status == "COMPLETE"
assert task.errors is None
if len(task.result) == 0:
num_retries -= 1
time.sleep(5)
else:
break
return task.result
@classmethod
def run_catalog_export_v2_task(
cls, client, num_retries=5, task_name=None, filters={}, params={}
):
task = None
params = (
params
if params
else {"performance_details": False, "label_details": True}
)
catalog = client.get_catalog()
while num_retries > 0:
task = catalog.export_v2(
task_name=task_name, filters=filters, params=params
)
task.wait_till_done()
assert task.status == "COMPLETE"
assert task.errors is None
if len(task.result) == 0:
num_retries -= 1
time.sleep(5)
else:
break
return task.result
@pytest.fixture
def export_v2_test_helpers() -> Type[ExportV2Helpers]:
return ExportV2Helpers()
@pytest.fixture
def big_dataset_data_row_ids(big_dataset: Dataset):
export_task = big_dataset.export()
export_task.wait_till_done()
stream = export_task.get_buffered_stream()
yield [dr.json["data_row"]["id"] for dr in stream]