Files
mtusi2026lab6/student-tests/test_pipeline.py
T
2026-11-10 18:09:59 +03:00

73 lines
3.0 KiB
Python

"""Real image transformations, no mock storage."""
import hashlib
import io
import json
import subprocess
import sys
from PIL import Image
def test_worker_variants_and_original(tmp_path):
source=tmp_path/'input'
original=Image.new('RGBA',(320,200),(30,90,150,70))
original.save(source,format='PNG')
before=hashlib.sha256(source.read_bytes()).hexdigest()
result=subprocess.run([sys.executable,'-m','messenger.pipeline',str(source),str(tmp_path)],capture_output=True)
assert result.returncode==0, result.stderr
manifest=json.loads(result.stdout)
assert hashlib.sha256(source.read_bytes()).hexdigest()==before
for kind,fmt,size in [('thumbnail','JPEG',(256,160)),('crop','JPEG',(128,128)),('optimized','WEBP',(320,200))]:
item=manifest[kind]
with Image.open(tmp_path/kind) as image:
assert image.format==fmt and image.size==size
assert not image.getexif()
raw=(tmp_path/kind).read_bytes()
assert item['sha256']==hashlib.sha256(raw).hexdigest()
assert item['size_bytes']==len(raw)
def test_no_upscale_thumbnail_and_exif(tmp_path):
source=tmp_path/'input'
image=Image.new('RGB',(80,40),'red')
exif=Image.Exif();exif[274]=6
image.save(source,format='JPEG',exif=exif)
result=subprocess.run([sys.executable,'-m','messenger.pipeline',str(source),str(tmp_path)],capture_output=True)
assert result.returncode==0, result.stderr
with Image.open(tmp_path/'thumbnail') as output:
assert output.size==(40,80)
assert not output.getexif()
def test_deadline_covers_slow_s3(monkeypatch):
import contextlib
import http.server
import os
import threading
import time
import uuid
import pytest
from messenger.jobs import process_objects
class Slow(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Length','100')
self.end_headers()
try:
for _ in range(100):
self.wfile.write(b'x');self.wfile.flush();time.sleep(.05)
except (BrokenPipeError,ConnectionResetError): pass
def log_message(self,*args): pass
server=http.server.ThreadingHTTPServer(('127.0.0.1',0),Slow)
thread=threading.Thread(target=server.serve_forever,daemon=True);thread.start()
monkeypatch.delenv('S3_ENDPOINT_FILE',raising=False)
monkeypatch.setenv('S3_ENDPOINT','http://127.0.0.1:'+str(server.server_port))
monkeypatch.setenv('PROCESS_TIMEOUT_SECONDS','2')
row={'id':uuid.uuid4(),'staging_key':'staging/slow','object_key':'originals/slow',
'size_bytes':100,'sha256':hashlib.sha256(b'x'*100).hexdigest(),'content_type':'image/png'}
started=time.monotonic()
try:
with pytest.raises(subprocess.TimeoutExpired): process_objects(__import__("messenger.object_store",fromlist=["ObjectStore"]).ObjectStore(),row)
assert time.monotonic()-started<4
finally:
server.shutdown();server.server_close()