71 lines
1.6 KiB
Python
71 lines
1.6 KiB
Python
import io
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import yaml
|
|
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"
|
|
config_path = tmp / "config.yaml"
|
|
|
|
# Write a minimal config so ConfigManager writes into the temp dir
|
|
with open(config_path, "w") as f:
|
|
yaml.dump({"display": {"saturation": 0.5}}, f)
|
|
|
|
old_environ = os.environ.get("SPECTRA_CACHE_DIR")
|
|
os.environ["SPECTRA_CACHE_DIR"] = str(cache_dir)
|
|
|
|
app = create_app(config_path=str(config_path))
|
|
|
|
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
|