From f197cee1a31d5d2473c63ada4f9ff49fd789d815 Mon Sep 17 00:00:00 2001 From: "Kruchinin, Dmitryx" Date: Fri, 26 Nov 2021 13:03:00 +0300 Subject: [PATCH 1/3] Added test --- cvat/apps/engine/tests/test_rest_api.py | 89 +++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/cvat/apps/engine/tests/test_rest_api.py b/cvat/apps/engine/tests/test_rest_api.py index d9786cb742e..20e5105378c 100644 --- a/cvat/apps/engine/tests/test_rest_api.py +++ b/cvat/apps/engine/tests/test_rest_api.py @@ -1349,6 +1349,7 @@ def test_api_v1_projects_delete_label(self): }] } self._check_api_v1_project(data) + class ProjectListOfTasksAPITestCase(APITestCase): def setUp(self): self.client = APIClient() @@ -1395,6 +1396,94 @@ def test_api_v1_projects_id_tasks_no_auth(self): response = self._run_api_v1_projects_id_tasks(None, project.id) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) +class ProjectExportAPITestCase(APITestCase): + def setUp(self): + self.client = APIClient() + + @classmethod + def setUpTestData(cls): + create_db_users(cls) + project_data = { + "name": "Project for check tasks in a xml", + "owner": cls.admin, + "labels": [{ + "name": "car" + }] + } + + db_project = create_db_project(project_data) + create_dummy_db_tasks(cls, db_project) + cls.projects = db_project + + def _run_api_v1_project_id_export(self, pid, user, annotation_format=""): + with ForceLogin(user, self.client): + response = self.client.get( + '/api/v1/projects/{}/annotations?format={}'.format(pid, annotation_format), + format="json") + return response + + def _run_api_v1_tasks_id(self, tid, user): + with ForceLogin(user, self.client): + response = self.client.delete('/api/v1/tasks/{}'.format(tid), format="json") + return response + + def test_api_v1_projects_remove_task_export(self): + project = self.projects + pid = project.id + user = self.admin + tasks_id = [task.id for task in project.tasks.all()] + + self.assertEqual(len(tasks_id), 4) + + annotation_format = "CVAT for images 1.1" + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) + + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + annotation_format = "CVAT for images 1.1&action=download" + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + content = io.BytesIO(b"".join(response.streaming_content)) + content.seek(0) + + with tempfile.TemporaryDirectory() as tmp_dir: + zipfile.ZipFile(content).extractall(tmp_dir) + xml = osp.join(tmp_dir, 'annotations.xml') + self.assertTrue(xml) + root = ET.parse(xml).getroot() + tasks = root.findall('meta/project/tasks/task/name') + self.assertEqual(len(tasks), 4) + + response = self._run_api_v1_tasks_id(tasks_id[0], self.admin) + tasks_id = [task.id for task in project.tasks.all()] + + self.assertEqual(len(tasks_id), 3) + + annotation_format = "CVAT for images 1.1" + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) + + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + + annotation_format = "CVAT for images 1.1&action=download" + response = self._run_api_v1_project_id_export(pid, user, annotation_format) + self.assertEqual(response.status_code, status.HTTP_200_OK) + + content = io.BytesIO(b"".join(response.streaming_content)) + content.seek(0) + + with tempfile.TemporaryDirectory() as tmp_dir: + zipfile.ZipFile(content).extractall(tmp_dir) + xml = osp.join(tmp_dir, 'annotations.xml') + self.assertTrue(xml) + root = ET.parse(xml).getroot() + tasks = root.findall('meta/project/tasks/task/name') + self.assertEqual(len(tasks), 3) + class TaskListAPITestCase(APITestCase): def setUp(self): From 52c389f35e8eee0025d440e1acd35c89a1dd077a Mon Sep 17 00:00:00 2001 From: "Kruchinin, Dmitryx" Date: Fri, 26 Nov 2021 16:22:40 +0300 Subject: [PATCH 2/3] Code refactoring --- cvat/apps/engine/tests/test_rest_api.py | 45 ++++++++++--------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/cvat/apps/engine/tests/test_rest_api.py b/cvat/apps/engine/tests/test_rest_api.py index 313a00d875f..c3162e11359 100644 --- a/cvat/apps/engine/tests/test_rest_api.py +++ b/cvat/apps/engine/tests/test_rest_api.py @@ -1427,14 +1427,15 @@ def _run_api_v1_tasks_id_delete(self, tid, user): response = self.client.delete('/api/v1/tasks/{}'.format(tid), format="json") return response - def test_api_v1_projects_remove_task_export(self): - project = self.projects - pid = project.id - user = self.admin + def _get_tasks_count(self, project): tasks_id = [task.id for task in project.tasks.all()] + return tasks_id - self.assertEqual(len(tasks_id), 4) + def _check_tasks_count(self, project, expected_result): + tasks_id = self._get_tasks_count(project) + self.assertEqual(len(tasks_id), expected_result) + def _check_xml(self, pid, user, expected_result): annotation_format = "CVAT for images 1.1" response = self._run_api_v1_project_id_export(pid, user, annotation_format) self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) @@ -1455,34 +1456,22 @@ def test_api_v1_projects_remove_task_export(self): self.assertTrue(xml) root = ET.parse(xml).getroot() tasks = root.findall('meta/project/tasks/task/name') - self.assertEqual(len(tasks), 4) - - response = self._run_api_v1_tasks_id_delete(tasks_id[0], self.admin) - tasks_id = [task.id for task in project.tasks.all()] - - self.assertEqual(len(tasks_id), 3) + self.assertEqual(len(tasks), expected_result) - annotation_format = "CVAT for images 1.1" - response = self._run_api_v1_project_id_export(pid, user, annotation_format) - self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED) - response = self._run_api_v1_project_id_export(pid, user, annotation_format) - self.assertEqual(response.status_code, status.HTTP_201_CREATED) + def test_api_v1_projects_remove_task_export(self): + project = self.projects + pid = project.id + user = self.admin - annotation_format = "CVAT for images 1.1&action=download" - response = self._run_api_v1_project_id_export(pid, user, annotation_format) - self.assertEqual(response.status_code, status.HTTP_200_OK) + self._check_tasks_count(project, 4) + self._check_xml(pid, user, 4) - content = io.BytesIO(b"".join(response.streaming_content)) - content.seek(0) + response = self._run_api_v1_tasks_id_delete(self._get_tasks_count(project)[0], self.admin) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) - with tempfile.TemporaryDirectory() as tmp_dir: - zipfile.ZipFile(content).extractall(tmp_dir) - xml = osp.join(tmp_dir, 'annotations.xml') - self.assertTrue(xml) - root = ET.parse(xml).getroot() - tasks = root.findall('meta/project/tasks/task/name') - self.assertEqual(len(tasks), 3) + self._check_tasks_count(project, 3) + self._check_xml(pid, user, 3) class TaskListAPITestCase(APITestCase): From 1ac09648f2e74ed17ea0bd2caf12e8635facfc15 Mon Sep 17 00:00:00 2001 From: "Kruchinin, Dmitryx" Date: Tue, 30 Nov 2021 11:23:27 +0300 Subject: [PATCH 3/3] Applying comments. --- cvat/apps/engine/tests/test_rest_api.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cvat/apps/engine/tests/test_rest_api.py b/cvat/apps/engine/tests/test_rest_api.py index c3162e11359..6262ff99641 100644 --- a/cvat/apps/engine/tests/test_rest_api.py +++ b/cvat/apps/engine/tests/test_rest_api.py @@ -1413,7 +1413,7 @@ def setUpTestData(cls): db_project = create_db_project(project_data) create_dummy_db_tasks(cls, db_project) - cls.projects = db_project + cls.project = db_project def _run_api_v1_project_id_export(self, pid, user, annotation_format=""): with ForceLogin(user, self.client): @@ -1427,12 +1427,8 @@ def _run_api_v1_tasks_id_delete(self, tid, user): response = self.client.delete('/api/v1/tasks/{}'.format(tid), format="json") return response - def _get_tasks_count(self, project): - tasks_id = [task.id for task in project.tasks.all()] - return tasks_id - def _check_tasks_count(self, project, expected_result): - tasks_id = self._get_tasks_count(project) + tasks_id = [task.id for task in project.tasks.all()] self.assertEqual(len(tasks_id), expected_result) def _check_xml(self, pid, user, expected_result): @@ -1458,16 +1454,16 @@ def _check_xml(self, pid, user, expected_result): tasks = root.findall('meta/project/tasks/task/name') self.assertEqual(len(tasks), expected_result) - def test_api_v1_projects_remove_task_export(self): - project = self.projects + project = self.project pid = project.id user = self.admin self._check_tasks_count(project, 4) self._check_xml(pid, user, 4) - response = self._run_api_v1_tasks_id_delete(self._get_tasks_count(project)[0], self.admin) + tasks_id = [task.id for task in project.tasks.all()] + response = self._run_api_v1_tasks_id_delete(tasks_id[0], self.admin) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self._check_tasks_count(project, 3)