|
16 | 16 |
|
17 | 17 | import os
|
18 | 18 |
|
19 |
| -import create_annotation_spec_set |
20 |
| -import create_instruction |
21 |
| -from google.api_core.client_options import ClientOptions |
22 |
| -from google.cloud import datalabeling_v1beta1 as datalabeling |
23 |
| -import import_data |
24 |
| -import label_image |
25 |
| -import manage_dataset |
| 19 | +import backoff |
| 20 | +from google.api_core.exceptions import DeadlineExceeded |
26 | 21 | import pytest
|
27 | 22 |
|
| 23 | +import label_image |
| 24 | +import testing_lib |
| 25 | + |
| 26 | + |
28 | 27 | PROJECT_ID = os.getenv('GCLOUD_PROJECT')
|
29 | 28 | INPUT_GCS_URI = 'gs://cloud-samples-data/datalabeling/image/image_dataset.csv'
|
| 29 | +INSTRUCTION_GCS_URI = ('gs://cloud-samples-data/datalabeling' |
| 30 | + '/instruction/test.pdf') |
30 | 31 |
|
31 | 32 |
|
32 |
| -@pytest.fixture(scope='function') |
| 33 | +@pytest.fixture(scope='module') |
33 | 34 | def dataset():
|
34 | 35 | # create a temporary dataset
|
35 |
| - dataset = manage_dataset.create_dataset(PROJECT_ID) |
36 |
| - |
37 |
| - # import some data to it |
38 |
| - import_data.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI) |
| 36 | + dataset = testing_lib.create_dataset(PROJECT_ID) |
39 | 37 |
|
| 38 | + testing_lib.import_data(dataset.name, 'IMAGE', INPUT_GCS_URI) |
40 | 39 | yield dataset
|
41 | 40 |
|
42 | 41 | # tear down
|
43 |
| - manage_dataset.delete_dataset(dataset.name) |
| 42 | + testing_lib.delete_dataset(dataset.name) |
44 | 43 |
|
45 | 44 |
|
46 |
| -@pytest.fixture(scope='function') |
| 45 | +@pytest.fixture(scope='module') |
47 | 46 | def annotation_spec_set():
|
48 | 47 | # create a temporary annotation_spec_set
|
49 |
| - response = create_annotation_spec_set.create_annotation_spec_set( |
50 |
| - PROJECT_ID) |
| 48 | + response = testing_lib.create_annotation_spec_set(PROJECT_ID) |
51 | 49 |
|
52 | 50 | yield response
|
53 | 51 |
|
54 |
| - # tear down |
55 |
| - client = datalabeling.DataLabelingServiceClient() |
56 |
| - |
57 |
| - # If provided, use a provided test endpoint - this will prevent tests on |
58 |
| - # this snippet from triggering any action by a real human |
59 |
| - if 'DATALABELING_ENDPOINT' in os.environ: |
60 |
| - opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT')) |
61 |
| - client = datalabeling.DataLabelingServiceClient(client_options=opts) |
62 |
| - |
63 |
| - client.delete_annotation_spec_set(response.name) |
| 52 | + testing_lib.delete_annotation_spec_set(response.name) |
64 | 53 |
|
65 | 54 |
|
66 |
| -@pytest.fixture(scope='function') |
| 55 | +@pytest.fixture(scope='module') |
67 | 56 | def instruction():
|
68 | 57 | # create a temporary instruction
|
69 |
| - instruction = create_instruction.create_instruction( |
70 |
| - PROJECT_ID, 'IMAGE', |
71 |
| - 'gs://cloud-samples-data/datalabeling/instruction/test.pdf') |
| 58 | + instruction = testing_lib.create_instruction( |
| 59 | + PROJECT_ID, 'IMAGE', INSTRUCTION_GCS_URI) |
72 | 60 |
|
73 | 61 | yield instruction
|
74 | 62 |
|
75 | 63 | # tear down
|
76 |
| - client = datalabeling.DataLabelingServiceClient() |
| 64 | + testing_lib.delete_instruction(instruction.name) |
77 | 65 |
|
78 |
| - # If provided, use a provided test endpoint - this will prevent tests on |
79 |
| - # this snippet from triggering any action by a real human |
80 |
| - if 'DATALABELING_ENDPOINT' in os.environ: |
81 |
| - opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT')) |
82 |
| - client = datalabeling.DataLabelingServiceClient(client_options=opts) |
83 | 66 |
|
84 |
| - client.delete_instruction(instruction.name) |
| 67 | +@pytest.fixture(scope='module') |
| 68 | +def cleaner(): |
| 69 | + resource_names = [] |
| 70 | + |
| 71 | + yield resource_names |
| 72 | + |
| 73 | + for resource_name in resource_names: |
| 74 | + testing_lib.cancel_operation(resource_name) |
85 | 75 |
|
86 | 76 |
|
87 | 77 | # Passing in dataset as the last argument in test_label_image since it needs
|
88 | 78 | # to be deleted before the annotation_spec_set can be deleted.
|
89 |
| -@pytest.mark.slow |
90 |
| -def test_label_image(capsys, annotation_spec_set, instruction, dataset): |
91 |
| - |
92 |
| - # Start labeling. |
93 |
| - response = label_image.label_image( |
94 |
| - dataset.name, |
95 |
| - instruction.name, |
96 |
| - annotation_spec_set.name |
97 |
| - ) |
98 |
| - out, _ = capsys.readouterr() |
99 |
| - assert 'Label_image operation name: ' in out |
100 |
| - operation_name = response.operation.name |
| 79 | +def test_label_image( |
| 80 | + capsys, annotation_spec_set, instruction, dataset, cleaner): |
101 | 81 |
|
102 |
| - # Cancels the labeling operation. |
103 |
| - response.cancel() |
104 |
| - assert response.cancelled() is True |
| 82 | + @backoff.on_exception( |
| 83 | + backoff.expo, DeadlineExceeded, max_time=testing_lib.RETRY_DEADLINE) |
| 84 | + def run_sample(): |
| 85 | + # Start labeling. |
| 86 | + return label_image.label_image( |
| 87 | + dataset.name, instruction.name, annotation_spec_set.name) |
105 | 88 |
|
106 |
| - client = datalabeling.DataLabelingServiceClient() |
| 89 | + response = run_sample() |
| 90 | + cleaner.append(response.operation.name) |
107 | 91 |
|
108 |
| - # If provided, use a provided test endpoint - this will prevent tests on |
109 |
| - # this snippet from triggering any action by a real human |
110 |
| - if 'DATALABELING_ENDPOINT' in os.environ: |
111 |
| - opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT')) |
112 |
| - client = datalabeling.DataLabelingServiceClient(client_options=opts) |
113 |
| - |
114 |
| - client.transport._operations_client.cancel_operation( |
115 |
| - operation_name) |
| 92 | + out, _ = capsys.readouterr() |
| 93 | + assert 'Label_image operation name: ' in out |
0 commit comments