acra-backend / code /tests /test_api.py
Zeref8425's picture
Deploy backend snapshot
78661be
Raw History Blame Contribute Delete
1.42 kB
import io
import pytest
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient
from PIL import Image
@pytest.fixture(scope="module")
def client():
from main import app
with TestClient(app) as test_client:
yield test_client
def _png_bytes(size=(16, 16), color=(255, 0, 0)):
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format="PNG")
buf.seek(0)
return buf
def _png_bytes(size=(16, 16), color=(255, 0, 0)):
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format="PNG")
buf.seek(0)
return buf
def test_health(client):
response = client.get("/health")
assert response.status_code == 200
body = response.json()
assert body["status"] == "ok"
assert "model_loaded" in body
def test_process_rejects_missing_image(client):
response = client.post("/process", data={
"cvd_type": "deutan",
"severity": "0.8",
"conf_threshold": "0.3",
})
assert response.status_code == 422
def test_process_rejects_invalid_cvd_type(client):
response = client.post(
"/process",
data={
"cvd_type": "tritan",
"severity": "0.8",
"conf_threshold": "0.3",
},
files={"image": ("swatch.png", _png_bytes(), "image/png")},
)
assert response.status_code == 400
assert "cvd_type" in response.json()["detail"]