182 lines
5.7 KiB
Python
182 lines
5.7 KiB
Python
"""Jednotkové testy optimalizace obrázků přes Pillow."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from poznavacky import images
|
|
|
|
|
|
def _make_image_bytes(
|
|
size: tuple[int, int],
|
|
*,
|
|
fmt: str = "JPEG",
|
|
mode: str = "RGB",
|
|
color: tuple[int, ...] = (40, 120, 200),
|
|
exif: Image.Exif | None = None,
|
|
) -> bytes:
|
|
img = Image.new(mode, size, color)
|
|
buf = BytesIO()
|
|
save_kwargs: dict = {"format": fmt}
|
|
if exif is not None:
|
|
save_kwargs["exif"] = exif
|
|
img.save(buf, **save_kwargs)
|
|
return buf.getvalue()
|
|
|
|
|
|
@pytest.mark.parametrize("fmt", ["JPEG", "PNG", "WEBP"])
|
|
def test_supported_formats_produce_only_two_webp_variants(
|
|
tmp_path: Path,
|
|
fmt: str,
|
|
) -> None:
|
|
raw = _make_image_bytes((600, 400), fmt=fmt)
|
|
media_dir = tmp_path / "media"
|
|
|
|
photo = images.process_and_save_photo(raw, media_dir, sada_id=7)
|
|
|
|
assert photo.soubor_plny.startswith("7/")
|
|
assert photo.soubor_plny.endswith(".webp")
|
|
assert photo.soubor_nahled.endswith("_thumb.webp")
|
|
assert photo.sirka == 600
|
|
assert photo.vyska == 400
|
|
|
|
full_path = media_dir / photo.soubor_plny
|
|
thumb_path = media_dir / photo.soubor_nahled
|
|
assert full_path.is_file()
|
|
assert thumb_path.is_file()
|
|
assert photo.velikost_plny_b == full_path.stat().st_size
|
|
assert photo.velikost_nahled_b == thumb_path.stat().st_size
|
|
|
|
created_files = sorted(p.name for p in (media_dir / "7").iterdir())
|
|
assert len(created_files) == 2
|
|
assert all(name.endswith(".webp") for name in created_files)
|
|
|
|
|
|
def test_large_image_resized_and_small_image_not_upscaled(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
media_dir = tmp_path / "media"
|
|
|
|
large_raw = _make_image_bytes((3200, 2400), fmt="JPEG")
|
|
large = images.process_and_save_photo(large_raw, media_dir, sada_id=1)
|
|
assert (large.sirka, large.vyska) == (1600, 1200)
|
|
with Image.open(media_dir / large.soubor_plny) as full_img:
|
|
assert full_img.format == "WEBP"
|
|
assert full_img.size == (1600, 1200)
|
|
with Image.open(media_dir / large.soubor_nahled) as thumb_img:
|
|
assert thumb_img.format == "WEBP"
|
|
assert thumb_img.size == (400, 300)
|
|
|
|
small_raw = _make_image_bytes((300, 200), fmt="PNG")
|
|
small = images.process_and_save_photo(small_raw, media_dir, sada_id=1)
|
|
assert (small.sirka, small.vyska) == (300, 200)
|
|
with Image.open(media_dir / small.soubor_plny) as full_img:
|
|
assert full_img.size == (300, 200)
|
|
with Image.open(media_dir / small.soubor_nahled) as thumb_img:
|
|
assert thumb_img.size == (300, 200)
|
|
|
|
|
|
def test_exif_autorotation_and_full_exif_strip(tmp_path: Path) -> None:
|
|
exif = Image.Exif()
|
|
exif[274] = 6 # Orientation: 90° CW
|
|
exif[271] = "TestCam" # Make
|
|
exif[272] = "Model X" # Model
|
|
exif[306] = "2026:01:02 10:20:30" # DateTime
|
|
exif[34853] = {1: "N", 2: (50.0, 4.0, 0.0)} # GPSInfo
|
|
|
|
raw = _make_image_bytes((1200, 800), fmt="JPEG", exif=exif)
|
|
media_dir = tmp_path / "media"
|
|
|
|
photo = images.process_and_save_photo(raw, media_dir, sada_id=3)
|
|
assert (photo.sirka, photo.vyska) == (800, 1200)
|
|
|
|
for rel in (photo.soubor_plny, photo.soubor_nahled):
|
|
with Image.open(media_dir / rel) as saved:
|
|
assert len(saved.getexif()) == 0
|
|
assert not saved.info.get("exif")
|
|
|
|
with Image.open(media_dir / photo.soubor_plny) as full_img:
|
|
assert full_img.size == (800, 1200)
|
|
with Image.open(media_dir / photo.soubor_nahled) as thumb_img:
|
|
assert thumb_img.size == (267, 400)
|
|
|
|
|
|
def test_png_rgba_preserves_alpha_channel(tmp_path: Path) -> None:
|
|
raw = _make_image_bytes(
|
|
(200, 150),
|
|
fmt="PNG",
|
|
mode="RGBA",
|
|
color=(10, 20, 30, 0),
|
|
)
|
|
media_dir = tmp_path / "media"
|
|
|
|
photo = images.process_and_save_photo(raw, media_dir, sada_id=2)
|
|
with Image.open(media_dir / photo.soubor_plny) as full_img:
|
|
assert full_img.mode == "RGBA"
|
|
assert full_img.getpixel((0, 0))[3] == 0
|
|
|
|
|
|
def test_rejects_invalid_empty_oversized_and_decompression_bomb(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
media_dir = tmp_path / "media"
|
|
|
|
with pytest.raises(images.ImageValidationError, match="prázdný"):
|
|
images.process_and_save_photo(b"", media_dir, sada_id=1)
|
|
|
|
with pytest.raises(images.ImageValidationError, match="neplatný|platný"):
|
|
images.process_and_save_photo(
|
|
b"toto neni obrazek",
|
|
media_dir,
|
|
sada_id=1,
|
|
)
|
|
|
|
valid_raw = _make_image_bytes((200, 200), fmt="PNG")
|
|
with pytest.raises(images.ImageValidationError, match="překračuje"):
|
|
images.process_and_save_photo(
|
|
BytesIO(valid_raw),
|
|
media_dir,
|
|
sada_id=1,
|
|
max_bytes=64,
|
|
)
|
|
|
|
with pytest.raises(images.ImageValidationError, match="limit pixelů"):
|
|
images.process_and_save_photo(
|
|
valid_raw,
|
|
media_dir,
|
|
sada_id=1,
|
|
max_pixels=10_000,
|
|
)
|
|
|
|
assert not media_dir.exists()
|
|
|
|
|
|
def test_cleanup_removes_first_file_when_second_save_fails(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
raw = _make_image_bytes((500, 400), fmt="PNG")
|
|
media_dir = tmp_path / "media"
|
|
|
|
original_save = images._save_webp
|
|
calls = 0
|
|
|
|
def flaky_save(img: Image.Image, path: Path, *, quality: int) -> int:
|
|
nonlocal calls
|
|
calls += 1
|
|
if calls == 2:
|
|
raise OSError("Simulovaná chyba zápisu náhledu")
|
|
return original_save(img, path, quality=quality)
|
|
|
|
monkeypatch.setattr(images, "_save_webp", flaky_save)
|
|
|
|
with pytest.raises(OSError, match="Simulovaná chyba"):
|
|
images.process_and_save_photo(raw, media_dir, sada_id=9)
|
|
|
|
remaining = list((media_dir / "9").glob("*"))
|
|
assert remaining == []
|