56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
import base64
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
# 1x1 透明 PNG
|
|
TINY_PNG = base64.b64decode(
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
|
|
)
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_health():
|
|
r = client.get("/health")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["status"] == "ok"
|
|
assert body["model"] in ("mock", "onnx")
|
|
|
|
|
|
def test_detect_ok():
|
|
r = client.post("/detect", files={"file": ("a.png", TINY_PNG, "image/png")})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["model"] == "mock"
|
|
assert len(body["detections"]) >= 1
|
|
d = body["detections"][0]
|
|
assert d["class"] in ("healthy", "sick")
|
|
assert 0 <= d["confidence"] <= 1
|
|
assert d["bbox"]["w"] > 0
|
|
|
|
|
|
def test_detect_empty_file_rejected():
|
|
r = client.post("/detect", files={"file": ("a.png", b"", "image/png")})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_detect_invalid_image_rejected():
|
|
r = client.post("/detect", files={"file": ("a.png", b"junk", "image/png")})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_stream_detect_missing_url():
|
|
r = client.post("/stream-detect", json={})
|
|
assert r.status_code == 400
|
|
|
|
|
|
def test_metrics_shape():
|
|
r = client.get("/metrics")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
for key in ("model", "uptimeSeconds", "requests", "avgLatencyMs", "gpu"):
|
|
assert key in body
|