"""Tests for image processing and thumbnail generation.""" import io from uuid import uuid4 import pytest from PIL import Image as PILImage from app.images.processing import generate_thumbnails class TestThumbnailGeneration: """Tests for thumbnail generation.""" def test_generate_thumbnails_creates_all_sizes(self): """Test that thumbnails are generated for all quality levels.""" # Create a test image image_id = uuid4() image = PILImage.new("RGB", (2000, 1500), color="red") buffer = io.BytesIO() image.save(buffer, format="JPEG") contents = buffer.getvalue() # Mock storage client to avoid actual uploads from unittest.mock import MagicMock, patch with patch("app.images.processing.get_storage_client") as mock_storage: mock_storage.return_value.put_object = MagicMock() # Generate thumbnails thumbnail_paths = generate_thumbnails(image_id, "test/path.jpg", contents) # Verify all sizes created assert "low" in thumbnail_paths assert "medium" in thumbnail_paths assert "high" in thumbnail_paths # Verify storage was called assert mock_storage.return_value.put_object.call_count >= 2 def test_skip_thumbnail_for_small_images(self): """Test that thumbnails are skipped if image is smaller than target size.""" # Create a small test image (smaller than low quality threshold) image_id = uuid4() image = PILImage.new("RGB", (500, 375), color="blue") buffer = io.BytesIO() image.save(buffer, format="JPEG") contents = buffer.getvalue() from unittest.mock import MagicMock, patch with patch("app.images.processing.get_storage_client") as mock_storage: mock_storage.return_value.put_object = MagicMock() # Generate thumbnails thumbnail_paths = generate_thumbnails(image_id, "test/small.jpg", contents) # Should use original path for all sizes assert thumbnail_paths["low"] == "test/small.jpg" def test_handles_transparent_images(self): """Test conversion of transparent images to RGB.""" # Create RGBA image image_id = uuid4() image = PILImage.new("RGBA", (2000, 1500), color=(255, 0, 0, 128)) buffer = io.BytesIO() image.save(buffer, format="PNG") contents = buffer.getvalue() from unittest.mock import MagicMock, patch with patch("app.images.processing.get_storage_client") as mock_storage: mock_storage.return_value.put_object = MagicMock() # Should not raise exception thumbnail_paths = generate_thumbnails(image_id, "test/transparent.png", contents) assert len(thumbnail_paths) > 0