|
| 1 | +import io |
| 2 | +import os |
| 3 | +from os.path import dirname |
| 4 | + |
| 5 | +from src.superannotate import SAClient |
| 6 | +from tests.integration.base import BaseTestCase |
| 7 | + |
| 8 | +sa = SAClient() |
| 9 | + |
| 10 | + |
| 11 | +class TestSingleImageUpload(BaseTestCase): |
| 12 | + PROJECT_NAME = "single_image_upload" |
| 13 | + PROJECT_DESCRIPTION = "Desc" |
| 14 | + PROJECT_TYPE = "Vector" |
| 15 | + TEST_FOLDER_PTH = "data_set" |
| 16 | + TEST_FOLDER_PATH = "data_set/sample_project_vector" |
| 17 | + |
| 18 | + @property |
| 19 | + def folder_path(self): |
| 20 | + return os.path.join(dirname(dirname(__file__)), self.TEST_FOLDER_PATH) |
| 21 | + |
| 22 | + @property |
| 23 | + def classes_path(self): |
| 24 | + return os.path.join( |
| 25 | + dirname(dirname(__file__)), self.TEST_FOLDER_PATH, "classes/classes.json" |
| 26 | + ) |
| 27 | + |
| 28 | + def test_single_image_upload(self): |
| 29 | + sa.upload_image_to_project( |
| 30 | + self.PROJECT_NAME, |
| 31 | + self.folder_path + "/example_image_1.jpg", |
| 32 | + annotation_status="InProgress", |
| 33 | + ) |
| 34 | + assert len(sa.search_items(self.PROJECT_NAME)) == 1 |
| 35 | + |
| 36 | + with open(self.folder_path + "/example_image_1.jpg", "rb") as f: |
| 37 | + img = io.BytesIO(f.read()) |
| 38 | + |
| 39 | + sa.upload_image_to_project( |
| 40 | + self.PROJECT_NAME, img, image_name="rr.jpg", annotation_status="InProgress" |
| 41 | + ) |
| 42 | + |
| 43 | + assert len(sa.search_items(self.PROJECT_NAME)) == 2 |
| 44 | + |
| 45 | + |
| 46 | +class TestMultipleImageUpload(BaseTestCase): |
| 47 | + PROJECT_NAME = "test_multiple_image_upload" |
| 48 | + PROJECT_DESCRIPTION = "Desc" |
| 49 | + PROJECT_TYPE = "Vector" |
| 50 | + TEST_FOLDER_PTH = "data_set" |
| 51 | + TEST_FOLDER_PATH = "data_set/sample_project_vector" |
| 52 | + |
| 53 | + @property |
| 54 | + def folder_path(self): |
| 55 | + return os.path.join(dirname(dirname(__file__)), self.TEST_FOLDER_PATH) |
| 56 | + |
| 57 | + @property |
| 58 | + def classes_path(self): |
| 59 | + return os.path.join( |
| 60 | + dirname(dirname(__file__)), self.TEST_FOLDER_PATH, "classes/classes.json" |
| 61 | + ) |
| 62 | + |
| 63 | + def test_multiple_image_upload(self): |
| 64 | + (uploaded, could_not_upload, existing_images,) = sa.upload_images_to_project( |
| 65 | + self.PROJECT_NAME, |
| 66 | + [ |
| 67 | + f"{self.folder_path}/example_image_1.jpg", |
| 68 | + f"{self.folder_path}/example_image_2.jpg", |
| 69 | + f"{self.folder_path}/example_image_3.jpg", |
| 70 | + f"{self.folder_path}/example_image_4.jpg", |
| 71 | + ], |
| 72 | + annotation_status="InProgress", |
| 73 | + ) |
| 74 | + self.assertEqual(len(uploaded), 4) |
| 75 | + self.assertEqual(len(could_not_upload), 0) |
| 76 | + self.assertEqual(len(existing_images), 0) |
| 77 | + |
| 78 | + (uploaded, could_not_upload, existing_images,) = sa.upload_images_to_project( |
| 79 | + self.PROJECT_NAME, |
| 80 | + [ |
| 81 | + f"{self.folder_path}/example_image_1.jpg", |
| 82 | + f"{self.folder_path}/example_image_2.jpg", |
| 83 | + f"{self.folder_path}/example_image_3.jpg", |
| 84 | + f"{self.folder_path}/example_image_4.jpg", |
| 85 | + ], |
| 86 | + annotation_status="InProgress", |
| 87 | + ) |
| 88 | + self.assertEqual(len(uploaded), 0) |
| 89 | + self.assertEqual(len(could_not_upload), 0) |
| 90 | + self.assertEqual(len(existing_images), 4) |
| 91 | + |
| 92 | + uploaded, could_not_upload, existing_images = sa.upload_images_to_project( |
| 93 | + self.PROJECT_NAME, [f"{self.folder_path}/dd.jpg"] |
| 94 | + ) |
| 95 | + self.assertEqual(len(uploaded), 0) |
| 96 | + self.assertEqual(len(could_not_upload), 1) |
| 97 | + self.assertEqual(len(existing_images), 0) |
| 98 | + |
| 99 | + def test_multiple_image_upload_with_duplicates(self): |
| 100 | + with self.assertLogs("sa", level="INFO") as mock_log: |
| 101 | + ( |
| 102 | + uploaded, |
| 103 | + could_not_upload, |
| 104 | + existing_images, |
| 105 | + ) = sa.upload_images_to_project( |
| 106 | + self.PROJECT_NAME, |
| 107 | + [ |
| 108 | + f"{self.folder_path}/example_image_1.jpg", |
| 109 | + f"{self.folder_path}/example_image_1.jpg", |
| 110 | + f"{self.folder_path}/example_image_1.jpg", |
| 111 | + f"{self.folder_path}/example_image_3.jpg", |
| 112 | + f"{self.folder_path}/example_image_4.jpg", |
| 113 | + ], |
| 114 | + annotation_status="InProgress", |
| 115 | + ) |
| 116 | + assert ( |
| 117 | + mock_log.records[0].msg |
| 118 | + == "2 duplicated images found that won't be uploaded." |
| 119 | + ) |
| 120 | + assert ( |
| 121 | + mock_log.records[1].msg |
| 122 | + == "Uploading 3 images to project test_multiple_image_upload." |
| 123 | + ) |
| 124 | + self.assertEqual(len(uploaded), 3) |
| 125 | + self.assertEqual(len(could_not_upload), 0) |
| 126 | + self.assertEqual(len(existing_images), 2) |
0 commit comments