|
@@ -0,0 +1,127 @@
|
|
|
+import io
|
|
|
+import os
|
|
|
+import uuid
|
|
|
+
|
|
|
+from flask import url_for
|
|
|
+from pathlib import Path
|
|
|
+from pycs.database.File import File
|
|
|
+from tests.base import pаtch_tpool_execute
|
|
|
+from tests.client.label_tests import _BaseLabelTests
|
|
|
+
|
|
|
+
|
|
|
+class _BaseFileTests(_BaseLabelTests):
|
|
|
+
|
|
|
+ def setupModels(self):
|
|
|
+ super().setupModels()
|
|
|
+ root = Path(self.project.root_folder)
|
|
|
+ data_root = Path(self.project.data_folder)
|
|
|
+
|
|
|
+ for folder in [data_root, root / "temp"]:
|
|
|
+ folder.mkdir(exist_ok=True, parents=True)
|
|
|
+
|
|
|
+
|
|
|
+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)
|
|
|
+
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+
|
|
|
+ self.project.external_data = True
|
|
|
+ self.project.commit()
|
|
|
+
|
|
|
+ self.post(url,
|
|
|
+ data=dict(file=(io.BytesIO(file_content), "image.jpg")),
|
|
|
+ content_type="multipart/form-data",
|
|
|
+ status_code=400,
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+
|
|
|
+ @pаtch_tpool_execute
|
|
|
+ def test_file_upload(self, mocked_execute=None):
|
|
|
+
|
|
|
+ url = url_for("upload_file", project_id=4242)
|
|
|
+ self.post(url, data=dict(), status_code=404)
|
|
|
+
|
|
|
+ file_content = b"some content+1"
|
|
|
+ url = url_for("upload_file", project_id=self.project.id)
|
|
|
+
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+
|
|
|
+ self.post(url, data=dict(),
|
|
|
+ status_code=400)
|
|
|
+ self.assertEqual(0, File.query.count())
|
|
|
+
|
|
|
+ self.post(url,
|
|
|
+ data=dict(file=(io.BytesIO(file_content), "image.jpg")),
|
|
|
+ content_type="multipart/form-data",
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(1, File.query.count())
|
|
|
+
|
|
|
+ # this does not work, if we do not set the CONTENT_LENGTH by ourself
|
|
|
+ # file = File.query.first()
|
|
|
+ # self.assertEqual(len(file_content), file.size)
|
|
|
+
|
|
|
+
|
|
|
+class FileDeletionTests(_BaseFileTests):
|
|
|
+
|
|
|
+ def test_file_removal(self):
|
|
|
+
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = self.project.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"name",
|
|
|
+ filename=f"image",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertTrue(is_new)
|
|
|
+
|
|
|
+ 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)
|
|
|
+ self.post(url, json=dict(), status_code=400)
|
|
|
+ self.post(url, json=dict(remove=False), status_code=400)
|
|
|
+ self.post(url, json=dict(remove=True))
|
|
|
+ self.assertEqual(0, self.project.files.count())
|
|
|
+ self.assertFalse(os.path.exists(file.absolute_path))
|
|
|
+
|
|
|
+ url = url_for("remove_file", file_id=4242)
|
|
|
+ self.post(url, json=dict(remove=True), status_code=404)
|
|
|
+
|
|
|
+ def test_file_removal_from_project_with_external_data(self):
|
|
|
+
|
|
|
+ file_uuid = str(uuid.uuid1())
|
|
|
+ file, is_new = self.project.add_file(
|
|
|
+ uuid=file_uuid,
|
|
|
+ file_type="image",
|
|
|
+ name=f"name",
|
|
|
+ filename=f"image",
|
|
|
+ extension=".jpg",
|
|
|
+ size=32*1024,
|
|
|
+ )
|
|
|
+
|
|
|
+ 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)
|
|
|
+
|
|
|
+ self.assertEqual(1, self.project.files.count())
|
|
|
+ self.post(url, json=dict(remove=True), status_code=400)
|
|
|
+ self.assertEqual(1, self.project.files.count())
|