Repository navigation
Expand file tree
/
Copy pathtest_projects.py
More file actions
311 lines (259 loc) · 10.5 KB
/
Copy pathtest_projects.py
File metadata and controls
311 lines (259 loc) · 10.5 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
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import io
from logging import Logger
from pathlib import Path
from typing import Optional
import pytest
from cvat_sdk import Client, models
from cvat_sdk.api_client import exceptions
from cvat_sdk.core.proxies.projects import Project
from cvat_sdk.core.proxies.tasks import Task
from cvat_sdk.core.proxies.types import Location
from cvat_sdk.core.utils import filter_dict
from PIL import Image
from pytest_cases import fixture_ref, parametrize
from shared.fixtures.data import CloudStorageAssets
from shared.utils.config import IMPORT_EXPORT_BUCKET_ID
from .common import TestDatasetExport
from .util import make_pbar
class TestProjectUsecases(TestDatasetExport):
@pytest.fixture(autouse=True)
def setup(
self,
tmp_path: Path,
fxt_login: tuple[Client, str],
fxt_logger: tuple[Logger, io.StringIO],
fxt_stdout: io.StringIO,
restore_redis_ondisk_per_function,
):
self.tmp_path = tmp_path
logger, self.logger_stream = fxt_logger
self.stdout = fxt_stdout
self.client, self.user = fxt_login
self.client.logger = logger
api_client = self.client.api_client
for k in api_client.configuration.logger:
api_client.configuration.logger[k] = logger
@pytest.fixture
def fxt_task_with_shapes(self, fxt_new_task: Task):
labels = fxt_new_task.get_labels()
fxt_new_task.set_annotations(
models.LabeledDataRequest(
shapes=[
models.LabeledShapeRequest(
frame=0,
label_id=labels[0].id,
type="rectangle",
points=[1, 1, 2, 2],
),
],
)
)
return fxt_new_task
@pytest.fixture
def fxt_new_project(self):
project = self.client.projects.create(
spec={
"name": "test_project",
"labels": [{"name": "car"}, {"name": "person"}],
},
)
return project
@pytest.fixture
def fxt_new_project_with_target_storage(self):
project = self.client.projects.create(
spec={
"name": "test_project",
"labels": [{"name": "car"}, {"name": "person"}],
"target_storage": {
"location": Location.CLOUD_STORAGE,
"cloud_storage_id": IMPORT_EXPORT_BUCKET_ID,
},
},
)
return project
@pytest.fixture
def fxt_empty_project(self):
return self.client.projects.create(spec={"name": "test_project"})
@pytest.fixture
def fxt_project_with_shapes(self, fxt_task_with_shapes: Task):
project = self.client.projects.create(
spec=models.ProjectWriteRequest(
name="test_project",
labels=[
models.PatchedLabelRequest(
**filter_dict(label.to_dict(), drop=["id", "has_parent"])
)
for label in fxt_task_with_shapes.get_labels()
],
)
)
fxt_task_with_shapes.update(models.PatchedTaskWriteRequest(project_id=project.id))
project.fetch()
return project
@pytest.fixture
def fxt_backup_file(self, fxt_project_with_shapes: Project):
backup_path = self.tmp_path / "backup.zip"
fxt_project_with_shapes.download_backup(str(backup_path))
yield backup_path
def test_can_create_empty_project(self):
project = self.client.projects.create(spec=models.ProjectWriteRequest(name="test project"))
assert project.id != 0
assert project.name == "test project"
def test_can_create_project_with_attribute_with_blank_default(self):
project = self.client.projects.create(
spec=models.ProjectWriteRequest(
name="test project",
labels=[
models.PatchedLabelRequest(
name="text",
attributes=[
models.AttributeRequest(
name="text",
mutable=True,
input_type=models.InputTypeEnum("text"),
values=[],
default_value="",
)
],
)
],
)
)
labels = project.get_labels()
assert labels[0].attributes[0].default_value == ""
def test_can_create_project_from_dataset(self, fxt_coco_dataset: Path):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
project = self.client.projects.create_from_dataset(
spec=models.ProjectWriteRequest(name="project with data"),
dataset_path=fxt_coco_dataset,
dataset_format="COCO 1.0",
pbar=pbar,
)
assert project.get_tasks()[0].size == 1
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""
@pytest.mark.parametrize("convert", [True, False])
def test_can_create_project_from_dataset_with_polygons_to_masks_param(
self, fxt_camvid_dataset: Path, convert: bool
):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
project = self.client.projects.create_from_dataset(
spec=models.ProjectWriteRequest(name="project with data"),
dataset_path=fxt_camvid_dataset,
dataset_format="CamVid 1.0",
conv_mask_to_poly=convert,
pbar=pbar,
)
assert project.get_tasks()[0].size == 1
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""
task = project.get_tasks()[0]
imported_annotations = task.get_annotations()
assert all(
[s.type.value == "polygon" if convert else "mask" for s in imported_annotations.shapes]
)
def test_can_retrieve_project(self, fxt_new_project: Project):
project_id = fxt_new_project.id
project = self.client.projects.retrieve(project_id)
assert project.id == project_id
assert self.stdout.getvalue() == ""
def test_can_list_projects(self, fxt_new_project: Project):
project_id = fxt_new_project.id
projects = self.client.projects.list()
assert any(p.id == project_id for p in projects)
assert self.stdout.getvalue() == ""
def test_can_update_project(self, fxt_new_project: Project):
fxt_new_project.update(models.PatchedProjectWriteRequest(name="foo"))
retrieved_project = self.client.projects.retrieve(fxt_new_project.id)
assert retrieved_project.name == "foo"
assert fxt_new_project.name == retrieved_project.name
assert self.stdout.getvalue() == ""
def test_can_delete_project(self, fxt_new_project: Project):
fxt_new_project.remove()
with pytest.raises(exceptions.NotFoundException):
fxt_new_project.fetch()
assert self.stdout.getvalue() == ""
def test_can_get_tasks(self, fxt_project_with_shapes: Project):
tasks = fxt_project_with_shapes.get_tasks()
assert len(tasks) == 1
assert tasks[0].project_id == fxt_project_with_shapes.id
def test_can_get_labels(self, fxt_project_with_shapes: Project):
expected_labels = {"car", "person"}
received_labels = fxt_project_with_shapes.get_labels()
assert {obj.name for obj in received_labels} == expected_labels
assert self.stdout.getvalue() == ""
def test_can_download_backup(self, fxt_project_with_shapes: Project):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
backup_path = self.tmp_path / "backup.zip"
fxt_project_with_shapes.download_backup(str(backup_path), pbar=pbar)
assert backup_path.stat().st_size > 0
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""
def test_can_create_from_backup(self, fxt_backup_file: Path):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
restored_project = self.client.projects.create_from_backup(fxt_backup_file, pbar=pbar)
assert restored_project.get_tasks()[0].size == 1
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""
@pytest.mark.parametrize("format_name", ("CVAT for images 1.1",))
@pytest.mark.parametrize("include_images", (True, False))
@parametrize(
"project, location",
[
(fixture_ref("fxt_new_project"), None),
(fixture_ref("fxt_new_project"), Location.LOCAL),
(
pytest.param(
fixture_ref("fxt_new_project"),
Location.CLOUD_STORAGE,
marks=pytest.mark.with_external_services,
)
),
(
pytest.param(
fixture_ref("fxt_new_project_with_target_storage"),
None,
marks=pytest.mark.with_external_services,
)
),
(fixture_ref("fxt_new_project_with_target_storage"), Location.LOCAL),
(
pytest.param(
fixture_ref("fxt_new_project_with_target_storage"),
Location.CLOUD_STORAGE,
marks=pytest.mark.with_external_services,
)
),
],
)
def test_can_export_dataset(
self,
format_name: str,
include_images: bool,
project: Project,
location: Optional[Location],
request: pytest.FixtureRequest,
cloud_storages: CloudStorageAssets,
):
file_path = self.tmp_path / f"project_{project.id}-{format_name.lower()}.zip"
self._test_can_export_dataset(
project,
format_name=format_name,
file_path=file_path,
include_images=include_images,
location=location,
request=request,
cloud_storages=cloud_storages,
)
def test_can_download_preview(self, fxt_project_with_shapes: Project):
frame_encoded = fxt_project_with_shapes.get_preview()
(width, height) = Image.open(frame_encoded).size
assert width > 0 and height > 0
assert self.stdout.getvalue() == ""