|
@@ -25,12 +25,27 @@ class _BaseFileTests(_BaseLabelTests):
|
|
|
for folder in [data_root, root / "temp"]:
|
|
|
folder.mkdir(exist_ok=True, parents=True)
|
|
|
|
|
|
+ def _get_dummy_image_bytes(self):
|
|
|
+ byteImgIO = io.BytesIO()
|
|
|
+ byteImg = Image.fromarray(np.zeros([1,1,3]).astype(np.uint8))
|
|
|
+ byteImg.save(byteImgIO, "JPEG")
|
|
|
+ byteImgIO.seek(0)
|
|
|
+ file_content = byteImgIO.read()
|
|
|
+
|
|
|
+ return file_content
|
|
|
+
|
|
|
+ def _create_dummy_image(self, file_name):
|
|
|
+ absolute_path = os.path.join(self.project.data_folder, file_name)
|
|
|
+ file_content = self._get_dummy_image_bytes()
|
|
|
+ with open(absolute_path, "wb") as f:
|
|
|
+ f.write(file_content)
|
|
|
+
|
|
|
+ return file_content
|
|
|
|
|
|
class FileCreationTests(_BaseFileTests):
|
|
|
|
|
|
@pаtch_tpool_execute
|
|
|
def test_file_upload_project_with_external_data(self, mocked_execute=None):
|
|
|
-
|
|
|
file_content = b"some content+1"
|
|
|
url = url_for("upload_file", project_id=self.project.id)
|
|
|
|
|
@@ -53,7 +68,8 @@ class FileCreationTests(_BaseFileTests):
|
|
|
url = url_for("upload_file", project_id=4242)
|
|
|
self.post(url, data=dict(), status_code=404)
|
|
|
|
|
|
- file_content = b"some content+1"
|
|
|
+ # Creating a dummy image with proper dummy content.
|
|
|
+ file_content = self._get_dummy_image_bytes()
|
|
|
url = url_for("upload_file", project_id=self.project.id)
|
|
|
|
|
|
self.assertEqual(0, File.query.count())
|
|
@@ -78,6 +94,8 @@ class FileDeletionTests(_BaseFileTests):
|
|
|
|
|
|
def test_file_removal(self):
|
|
|
|
|
|
+ self._create_dummy_image("image.jpg")
|
|
|
+
|
|
|
file_uuid = str(uuid.uuid1())
|
|
|
file, is_new = self.project.add_file(
|
|
|
uuid=file_uuid,
|
|
@@ -92,9 +110,6 @@ class FileDeletionTests(_BaseFileTests):
|
|
|
|
|
|
self.assertEqual(1, self.project.files.count())
|
|
|
|
|
|
- with open(file.absolute_path, "w"):
|
|
|
- pass
|
|
|
-
|
|
|
self.assertTrue(os.path.exists(file.absolute_path))
|
|
|
|
|
|
url = url_for("remove_file", file_id=file.id)
|
|
@@ -109,6 +124,8 @@ class FileDeletionTests(_BaseFileTests):
|
|
|
|
|
|
def test_file_removal_from_project_with_external_data(self):
|
|
|
|
|
|
+ self._create_dummy_image("image.jpg")
|
|
|
+
|
|
|
file_uuid = str(uuid.uuid1())
|
|
|
file, is_new = self.project.add_file(
|
|
|
uuid=file_uuid,
|
|
@@ -121,9 +138,6 @@ class FileDeletionTests(_BaseFileTests):
|
|
|
|
|
|
self.assertTrue(is_new)
|
|
|
|
|
|
- with open(file.absolute_path, "w"):
|
|
|
- pass
|
|
|
-
|
|
|
self.project.external_data = True
|
|
|
self.assertTrue(os.path.exists(file.absolute_path))
|
|
|
url = url_for("remove_file", file_id=file.id)
|
|
@@ -135,7 +149,6 @@ class FileDeletionTests(_BaseFileTests):
|
|
|
|
|
|
class FileGettingTests(_BaseFileTests):
|
|
|
|
|
|
-
|
|
|
def test_get_file_getting(self):
|
|
|
|
|
|
file_uuid = str(uuid.uuid1())
|
|
@@ -156,9 +169,7 @@ class FileGettingTests(_BaseFileTests):
|
|
|
# without an actual file, this GET request returns 404
|
|
|
self.get(url, status_code=404)
|
|
|
|
|
|
- content = b"some text"
|
|
|
- with open(file.absolute_path, "wb") as f:
|
|
|
- f.write(content)
|
|
|
+ content = self._create_dummy_image("image.jpg")
|
|
|
|
|
|
response = self.get(url)
|
|
|
|