6
0
Pārlūkot izejas kodu

Unittest for pre-generation of thumbnails

Added a unittest for the pre-generation of thumbnails. Additionally, the dummy image can now be created with a variable size.
blunk 3 gadi atpakaļ
vecāks
revīzija
769b3c8252
1 mainītis faili ar 40 papildinājumiem un 6 dzēšanām
  1. 40 6
      tests/client/file_tests.py

+ 40 - 6
tests/client/file_tests.py

@@ -25,22 +25,22 @@ class _BaseFileTests(_BaseLabelTests):
         for folder in [data_root, root / "temp"]:
             folder.mkdir(exist_ok=True, parents=True)
 
-    def _get_dummy_image_bytes(self):
+    def _get_dummy_image_bytes(self, size=(4000, 6000, 3)):
         byteImgIO = io.BytesIO()
-        byteImg = Image.fromarray(np.zeros([1,1,3]).astype(np.uint8))
+        byteImg = Image.fromarray(np.zeros(size).astype(np.uint8))
         byteImg.save(byteImgIO, "JPEG")
         byteImgIO.seek(0)
         file_content = byteImgIO.read()
 
         return file_content
 
-    def _create_dummy_image(self, file_name):
+    def _create_dummy_image(self, file_name, size=(4000, 6000, 3)):
         absolute_path = os.path.join(self.project.data_folder, file_name)
-        file_content = self._get_dummy_image_bytes()
+        file_content = self._get_dummy_image_bytes(size=size)
         with open(absolute_path, "wb") as f:
             f.write(file_content)
 
-        return file_content
+        return absolute_path, file_content
 
 class FileCreationTests(_BaseFileTests):
 
@@ -169,7 +169,7 @@ class FileGettingTests(_BaseFileTests):
         # without an actual file, this GET request returns 404
         self.get(url, status_code=404)
 
-        content = self._create_dummy_image("image.jpg")
+        _, content = self._create_dummy_image("image.jpg")
 
         response = self.get(url)
 
@@ -411,6 +411,40 @@ class FileResizingTests(_BaseFileTests):
             self.assertEqual(crop.shape, returned_im.shape)
             self._compare_images(crop, returned_im)
 
+    def test_automatic_thumbnail_generation(self):
+
+        img_size = (4000, 6000, 3)
+        self._create_dummy_image("image.jpg", size=img_size)
+
+        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())
+
+        self.assertTrue(os.path.exists(file.absolute_path))
+
+        temp_folder = os.path.join(self.project.root_folder, "temp")
+        for max_width, max_height in [(200, 200), (2000, 800)]:
+            img_path = os.path.join(temp_folder, file_uuid + "_" + str(max_width) + "_" + str(max_height) + ".jpg")
+
+            self.assertTrue(os.path.exists(img_path))
+
+            with Image.open(img_path) as img:
+                width, height = img.size
+
+            self.assertTrue(width == max_width or height == max_height)
+            self.assertLessEqual(width, max_width)
+            self.assertLessEqual(height, max_height)
+            self.assertLessEqual(abs(img_size[1] / img_size[0] - width / height), 0.1)
 
 def _im_from_bytes(data: bytes) -> np.ndarray:
     return np.asarray(Image.open(io.BytesIO(data)))