Files
mtusi2026lab6/student-tests/test_pipeline.py
T

47 lines
1.5 KiB
Python

from io import BytesIO
import pytest
from PIL import Image
from messenger.image_pipeline import fitted_size, render
@pytest.mark.parametrize(
"source,expected",
[((319, 200), (256, 160)), ((320, 200), (256, 160)), ((10, 4), (10, 4)), ((1, 1000), (1, 256))],
)
def test_fits_contract(source, expected):
assert fitted_size(*source, 256) == expected
def test_variants_alpha_and_metadata(tmp_path):
source = BytesIO()
Image.new("RGBA", (319, 200), (255, 0, 0, 0)).save(source, "PNG")
source.seek(0)
variants = render(source, tmp_path)
assert {v["name"] for v in variants} == {"thumbnail", "crop", "optimized"}
with Image.open(tmp_path / "thumbnail") as thumb:
assert thumb.format == "JPEG"
assert thumb.size == (256, 160)
assert all(channel > 245 for channel in thumb.getpixel((0, 0)))
assert not thumb.getexif()
with Image.open(tmp_path / "crop") as crop:
assert crop.size == (128, 128)
with Image.open(tmp_path / "optimized") as optimized:
assert optimized.format == "WEBP"
assert optimized.size == (319, 200)
def test_exif_orientation_without_upscale(tmp_path):
source = BytesIO()
exif = Image.Exif()
exif[274] = 6
Image.new("RGB", (80, 40), "blue").save(source, "JPEG", exif=exif)
original = source.getvalue()
source.seek(0)
render(source, tmp_path)
assert source.getvalue() == original
with Image.open(tmp_path / "thumbnail") as thumbnail:
assert thumbnail.size == (40, 80)
assert not thumbnail.getexif()