Files
spectra/tests/conftest.py

66 lines
1.4 KiB
Python

import io
import json
import os
import tempfile
from pathlib import Path
import pytest
from PIL import Image as PILImage
from spectra.web.server import create_app
TEST_IMAGE_SIZE = (800, 600)
@pytest.fixture
def app():
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
cache_dir = tmp / "cache"
old_environ = os.environ.get("SPECTRA_CACHE_DIR")
os.environ["SPECTRA_CACHE_DIR"] = str(cache_dir)
app = create_app()
yield app
if old_environ is None:
os.environ.pop("SPECTRA_CACHE_DIR", None)
else:
os.environ["SPECTRA_CACHE_DIR"] = old_environ
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def sample_image_bytes():
img = PILImage.new("RGB", TEST_IMAGE_SIZE, color=(100, 150, 200))
buf = io.BytesIO()
img.save(buf, format="JPEG")
buf.seek(0)
return buf
@pytest.fixture
def uploaded_image(client, sample_image_bytes):
resp = client.post(
"/api/images",
data={"file": (sample_image_bytes, "test.jpg")},
content_type="multipart/form-data",
)
assert resp.status_code == 201
return resp.get_json()
def make_image_bytes(width, height, color=(100, 150, 200), fmt="JPEG"):
img = PILImage.new("RGB", (width, height), color=color)
buf = io.BytesIO()
img.save(buf, format=fmt)
buf.seek(0)
return buf, img