Files
webref/backend/tests/images/test_processing.py
Danilo Reyes 948fe591dc
All checks were successful
CI/CD Pipeline / VM Test - security (push) Successful in 7s
CI/CD Pipeline / Backend Linting (push) Successful in 4s
CI/CD Pipeline / VM Test - backend-integration (push) Successful in 11s
CI/CD Pipeline / VM Test - full-stack (push) Successful in 8s
CI/CD Pipeline / VM Test - performance (push) Successful in 8s
CI/CD Pipeline / Nix Flake Check (push) Successful in 38s
CI/CD Pipeline / CI Summary (push) Successful in 0s
CI/CD Pipeline / Frontend Linting (push) Successful in 17s
phase 13
2025-11-02 14:48:03 -06:00

79 lines
2.8 KiB
Python

"""Tests for image processing and thumbnail generation."""
import io
from uuid import uuid4
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