99 lines
3.8 KiB
Python
99 lines
3.8 KiB
Python
"""Unit tests for ImageRepository class."""
|
|
|
|
import unittest
|
|
from unittest.mock import patch, MagicMock, _patch_dict
|
|
from uuid import UUID
|
|
from PIL import Image
|
|
|
|
from data_store.repositories.image_repository import ImageRepository
|
|
|
|
|
|
class TestImageRepository(unittest.TestCase):
|
|
"""Unit tests for ImageRepository class."""
|
|
|
|
patcher: _patch_dict
|
|
repo: ImageRepository
|
|
image: Image.Image
|
|
bad_image: str
|
|
image_id: UUID
|
|
bad_image_id: str
|
|
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
"""Set up test case with a mocked MinioAdapter."""
|
|
# Patch environment variables for MinioAdapter
|
|
cls.patcher = patch.dict(
|
|
"os.environ",
|
|
{
|
|
"MINIO_ENDPOINT": "localhost:9000",
|
|
"MINIO_ACCESS_KEY": "minioadmin",
|
|
"MINIO_SECRET_KEY": "minioadmin",
|
|
"MINIO_BUCKET": "test-bucket",
|
|
},
|
|
)
|
|
cls.patcher.start()
|
|
# Prepare ImageRepository with mocked MinioAdapter
|
|
cls.repo = ImageRepository()
|
|
cls.repo._get = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
cls.repo._put = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
cls.repo._delete = MagicMock(return_value=None) # type: ignore[method-assign]
|
|
# Prepare other test variables
|
|
cls.image = Image.new("RGB", (10, 10))
|
|
cls.image_id = UUID("d87022f8-4169-4d69-8512-bbd65cc1cb23")
|
|
cls.bad_image = "not_an_image"
|
|
cls.bad_image_id = "not-a-uuid"
|
|
|
|
@classmethod
|
|
def tearDownClass(cls) -> None:
|
|
# Stop patching
|
|
cls.patcher.stop()
|
|
|
|
def setUp(self) -> None:
|
|
"""Set up each test with a fresh ImageRepository instance."""
|
|
self.repo._get.reset_mock() # type: ignore[attr-defined]
|
|
self.repo._put.reset_mock() # type: ignore[attr-defined]
|
|
self.repo._delete.reset_mock() # type: ignore[attr-defined]
|
|
|
|
def test_should_adhere_to_interface(self) -> None:
|
|
"""Test that ImageRepository adheres to ImageRepositoryInterface."""
|
|
# Instantiation fails if interface not adhered to
|
|
_ = ImageRepository()
|
|
|
|
def test_should_have_image_format_set(self) -> None:
|
|
"""Test that ImageRepository has image_format set."""
|
|
self.assertTrue(hasattr(self.repo, "image_format"))
|
|
|
|
def test_should_have_folder_name_set(self) -> None:
|
|
"""Test that ImageRepository has folder_name set."""
|
|
self.assertTrue(hasattr(self.repo, "folder_name"))
|
|
|
|
def test_object_name_invalid_image_id_raises_value_error(self) -> None:
|
|
"""Test that _object_name() raises ValueError for invalid image_id."""
|
|
with self.assertRaises(ValueError):
|
|
_ = self.repo._object_name(self.bad_image_id) # type: ignore
|
|
|
|
def test_get_invalid_image_id_raises_value_error(self) -> None:
|
|
"""Test getting an image with invalid image_id."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.get(self.bad_image_id) # type: ignore
|
|
|
|
def test_put_invalid_image_id_raises_value_error(self) -> None:
|
|
"""Test putting an image with invalid image_id."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.put(self.image, self.bad_image_id) # type: ignore
|
|
|
|
def test_put_invalid_image_type_raises_value_error(self) -> None:
|
|
"""Test putting an invalid image type."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.put(self.bad_image, self.bad_image_id) # type: ignore
|
|
|
|
def test_delete_invalid_image_id_raises_value_error(self) -> None:
|
|
"""Test deleting an image with invalid image_id."""
|
|
with self.assertRaises(ValueError):
|
|
self.repo.delete(self.bad_image_id) # type: ignore
|
|
|
|
|
|
# Allows local debugging by running file as script
|
|
if __name__ == "__main__":
|
|
unittest.main()
|