diff --git a/tests/test_image_utils.py b/tests/test_image_utils.py index acd24d9..9949a36 100644 --- a/tests/test_image_utils.py +++ b/tests/test_image_utils.py @@ -1,3 +1,4 @@ +import sys import tempfile import unittest import uuid @@ -5,6 +6,7 @@ import numpy as np +sys.path.append(str(Path(__file__).resolve().parent.joinpath(".."))) from utils.image_utils import ( load_image_array_from_bytes, load_image_array_from_path, @@ -19,51 +21,112 @@ def setUp(self): dir=Path(__file__).resolve().parent, prefix=".tmp", suffix=self.__class__.__name__ ) self.tmp_dir = Path(self._tmp_dir.name) - self.data_dir = Path(__file__).parent.joinpath("tutorial", "data") - assert self.data_dir.is_dir(), "Missing tutorial data" + self.data_dir = Path(__file__).parents[1].joinpath("tutorial", "data") + assert self.data_dir.is_dir(), f"Missing tutorial data {self.data_dir}" def test_load_color_from_path(self): image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg") image = load_image_array_from_path(image_path, mode="color") self.assertIsInstance(image, np.ndarray) + self.assertEqual(image.ndim, 3) self.assertEqual(image.shape[-1], 3) self.assertGreaterEqual(image.min(), 0) self.assertLessEqual(image.max(), 255) self.assertIsInstance(image[0, 0, 0], np.uint8) + def test_load_grayscale_from_path(self): + image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg") + image = load_image_array_from_path(image_path, mode="grayscale") + self.assertIsInstance(image, np.ndarray) + self.assertEqual(image.ndim, 2) + self.assertGreaterEqual(image.min(), 0) + self.assertLessEqual(image.max(), 255) + self.assertIsInstance(image[0, 0], np.uint8) + def test_load_color_from_bytes(self): image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg") with image_path.open(mode="rb") as f: image_bytes = f.read() image = load_image_array_from_bytes(image_bytes, image_path, mode="color") self.assertIsInstance(image, np.ndarray) + self.assertEqual(image.ndim, 3) self.assertEqual(image.shape[-1], 3) self.assertGreaterEqual(image.min(), 0) self.assertLessEqual(image.max(), 255) self.assertIsInstance(image[0, 0, 0], np.uint8) - def test_save_color(self): + def test_load_grayscale_from_bytes(self): + image_path = self.data_dir.joinpath("inference", "NL-HaNA_1.01.02_3112_0395.jpg") + with image_path.open(mode="rb") as f: + image_bytes = f.read() + image = load_image_array_from_bytes(image_bytes, image_path, mode="grayscale") + self.assertIsInstance(image, np.ndarray) + self.assertEqual(image.ndim, 2) + self.assertGreaterEqual(image.min(), 0) + self.assertLessEqual(image.max(), 255) + self.assertIsInstance(image[0, 0], np.uint8) + + def test_save_color_jpg(self): image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8) image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg") save_image_array_to_path(image_path, image) self.assertTrue(image_path.is_file()) - def test_save_grayscale(self): + def test_save_grayscale_jpg(self): image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8) image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg") save_image_array_to_path(image_path, image) self.assertTrue(image_path.is_file()) - def test_save_load_color(self): + def test_save_load_color_jpg(self): image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8) image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg") save_image_array_to_path(image_path, image) - image2 = load_image_array_from_path(image_path) - self.assertEqual(image, image2) + image2 = load_image_array_from_path(image_path, mode="color") + self.assertIsNotNone(image2) + self.assertTrue(np.array_equal(image, image2, equal_nan=True)) - def test_save_load_grayscale(self): + def test_save_load_grayscale_jpg(self): image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8) image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg") save_image_array_to_path(image_path, image) - image2 = load_image_array_from_path(image_path) - self.assertEqual(image, image2) + image2 = load_image_array_from_path(image_path, mode="grayscale") + self.assertIsNotNone(image2) + self.assertTrue(np.array_equal(image, image2, equal_nan=True)) + + def test_save_color_png(self): + image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8) + image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png") + save_image_array_to_path(image_path, image) + self.assertTrue(image_path.is_file()) + + def test_save_grayscale_png(self): + image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8) + image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png") + save_image_array_to_path(image_path, image) + self.assertTrue(image_path.is_file()) + + def test_save_load_color_png(self): + image = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8) + image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png") + save_image_array_to_path(image_path, image) + image2 = load_image_array_from_path(image_path, mode="color") + self.assertIsNotNone(image2) + self.assertTrue(np.array_equal(image, image2, equal_nan=True)) + + def test_save_load_grayscale_png(self): + image = np.random.randint(0, 255, size=(64, 64), dtype=np.uint8) + image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.png") + save_image_array_to_path(image_path, image) + image2 = load_image_array_from_path(image_path, mode="grayscale") + self.assertIsNotNone(image2) + self.assertTrue(np.array_equal(image, image2, equal_nan=True)) + + def test_corrupt_image(self): + image_path = self.tmp_dir.joinpath(f"{uuid.uuid4()}.jpg") + image = load_image_array_from_path(image_path) + self.assertIsNone(image) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_input_utils.py b/tests/test_input_utils.py new file mode 100644 index 0000000..59cb0e2 --- /dev/null +++ b/tests/test_input_utils.py @@ -0,0 +1,45 @@ +import sys +import unittest +from pathlib import Path + +sys.path.append(str(Path(__file__).resolve().parent.joinpath(".."))) +from utils.input_utils import clean_input_paths + + +class TestInputUtils(unittest.TestCase): + def test_single_string_input(self): + input_path = "/path/to/single/file.jpg" + expected_output = [Path("/path/to/single/file.jpg")] + self.assertEqual(clean_input_paths(input_path), expected_output) + + def test_single_path_input(self): + input_path = Path("/path/to/single/file.jpg") + expected_output = [Path("/path/to/single/file.jpg")] + self.assertEqual(clean_input_paths(input_path), expected_output) + + def test_sequence_string_input(self): + input_paths = ["/path/to/file1.jpg", "/path/to/file2.jpg"] + expected_output = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")] + self.assertEqual(clean_input_paths(input_paths), expected_output) + + def test_sequence_path_input(self): + input_paths = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")] + expected_output = [Path("/path/to/file1.jpg"), Path("/path/to/file2.jpg")] + self.assertEqual(clean_input_paths(input_paths), expected_output) + + def test_empty_input(self): + with self.assertRaises(ValueError): + clean_input_paths("") + + def test_invalid_input_type(self): + with self.assertRaises(TypeError): + clean_input_paths(123) + + def test_mixed_type_input(self): + mixed_input = [Path("/path/to/file1.jpg"), "/path/to/file2.jpg", 123] + with self.assertRaises(TypeError): + clean_input_paths(mixed_input) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/input_utils.py b/utils/input_utils.py index 9041329..746a018 100644 --- a/utils/input_utils.py +++ b/utils/input_utils.py @@ -33,7 +33,7 @@ def clean_input_paths( Raises: ValueError: Must provide input path - NotImplementedError: given input paths are the wrong class + TypeError: given input paths are the wrong class Returns: list[Path]: output paths of images @@ -53,9 +53,9 @@ def clean_input_paths( elif isinstance(path, Path): output.append(path) else: - raise NotImplementedError + raise TypeError(f"Unsupported type: {type(path)}") else: - raise NotImplementedError + raise TypeError(f"Unsupported type: {type(input_paths)}") return output