forked from mtusi2026/mtusi2026lab7
90 lines
3.1 KiB
Python
90 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Focused tests for the required thumbnail transformation."""
|
|
import io
|
|
from pathlib import Path
|
|
import sys
|
|
from unittest.mock import Mock, patch
|
|
|
|
from PIL import Image
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from queueing import JOB_QUEUE, publish_event
|
|
from worker import InvalidImage, parse_event, thumbnail_jpeg
|
|
|
|
|
|
def main():
|
|
event = parse_event(
|
|
b'{"schema_version":1,"job_id":"11111111-1111-4111-8111-111111111111",'
|
|
b'"type":"attachment.process.v1","attachment_id":"22222222-2222-4222-8222-222222222222",'
|
|
b'"pipeline_version":1,"attempt":1,"created_at":"2026-09-01T12:00:00Z"}'
|
|
)
|
|
assert event["attempt"] == 1
|
|
try:
|
|
parse_event(b'{"schema_version":1}')
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError("incomplete job message should be rejected")
|
|
|
|
publisher_event = {
|
|
"schema_version": 1,
|
|
"job_id": "11111111-1111-4111-8111-111111111111",
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": "22222222-2222-4222-8222-222222222222",
|
|
"pipeline_version": 1,
|
|
"attempt": 1,
|
|
"created_at": "2026-09-01T12:00:00Z",
|
|
}
|
|
connection = Mock()
|
|
connection.is_open = True
|
|
channel = connection.channel.return_value
|
|
channel.basic_publish.return_value = True
|
|
with patch("queueing.pika.BlockingConnection", return_value=connection), \
|
|
patch("queueing.connection_parameters", return_value=Mock()):
|
|
publish_event(publisher_event)
|
|
channel.queue_declare.assert_any_call(
|
|
queue=JOB_QUEUE,
|
|
durable=True,
|
|
arguments={
|
|
"x-dead-letter-exchange": "attachment.jobs.dlx",
|
|
"x-dead-letter-routing-key": "attachment.jobs.dead",
|
|
},
|
|
)
|
|
channel.confirm_delivery.assert_called_once()
|
|
published = channel.basic_publish.call_args.kwargs
|
|
assert published["mandatory"] is True
|
|
assert published["properties"].delivery_mode == 2
|
|
assert set(publisher_event) == {
|
|
"schema_version", "job_id", "type", "attachment_id",
|
|
"pipeline_version", "attempt", "created_at",
|
|
}
|
|
|
|
sample = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
|
thumbnail, width, height = thumbnail_jpeg(sample, "image/png")
|
|
assert (width, height) == (256, 160), (width, height)
|
|
with Image.open(io.BytesIO(thumbnail)) as image:
|
|
assert image.format == "JPEG"
|
|
assert image.size == (256, 160)
|
|
|
|
small = Image.new("RGBA", (30, 20), (255, 0, 0, 0))
|
|
small_bytes = io.BytesIO()
|
|
small.save(small_bytes, format="PNG")
|
|
result, width, height = thumbnail_jpeg(small_bytes.getvalue(), "image/png")
|
|
assert (width, height) == (30, 20), (width, height)
|
|
with Image.open(io.BytesIO(result)) as image:
|
|
assert image.getpixel((0, 0)) == (255, 255, 255)
|
|
|
|
try:
|
|
thumbnail_jpeg(b"not an image", "image/png")
|
|
except InvalidImage:
|
|
pass
|
|
else:
|
|
raise AssertionError("corrupt image should be rejected")
|
|
print("PASS: 256x256 contain, no-upscale, alpha-on-white, invalid image")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|