worker 侧从 Mock 替换为真实算法: - face_analysis 包:detector(MediaPipe 478点) / pose(solvePnP 姿态) / calibration(虹膜直径法) / hair_segmenter+bisenet_model(方案B 头发分割) / measure(方案A兜底+B/A决策+七眼+换算) / annotation(numpy渐变线+中文标注) - app.py:/api/v1/face/measure 接真实实现,返回 annotated_image_base64 (不落盘不拼URL,落盘由网关做);加 X-Internal-Token 鉴权、/health 就绪态、 可配置分辨率门槛、异常兜底 - 部署:start.sh/run_worker.sh/hair-worker.service 监听 8187;worker_config 示例 - 测试 tests/:Tier1合成真值<1e-6 + Tier2缩放不变 + Tier3叠加 + 错误码集成 + 数值回归,pytest 24 项全绿 - 文档补实测基线表 + RTX5090/torch 说明 注:worker 为 RTX 5090(sm_120),pinned torch 2.2.2(cu121) 只到 sm_90, BiSeNet 已自动回退 CPU(方案B 正常);要用 GPU 需换 torch cu128(≥2.7)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
"""接口集成测试(FastAPI TestClient):错误码 + 鉴权 + 正常用例结构。"""
|
||
import base64
|
||
|
||
import pytest
|
||
from fastapi.testclient import TestClient
|
||
from conftest import fixture
|
||
|
||
import app as app_module
|
||
|
||
URL = "/api/v1/face/measure"
|
||
H = {"X-Internal-Token": "testpass"} # 与 worker_config.json 一致
|
||
|
||
|
||
@pytest.fixture(scope="module")
|
||
def client():
|
||
# with 触发 lifespan:加载模型单例(detector + 尽力加载 segmenter)
|
||
with TestClient(app_module.app) as c:
|
||
yield c
|
||
|
||
|
||
def _post(client, fixture_name=None, headers=H, data=None, extra_files=None):
|
||
files = {}
|
||
if fixture_name:
|
||
files["image_file"] = (fixture_name, open(fixture(fixture_name), "rb"), "application/octet-stream")
|
||
if extra_files:
|
||
files.update(extra_files)
|
||
return client.post(URL, headers=headers, files=files or None, data=data)
|
||
|
||
|
||
def test_auth_missing_token_401(client):
|
||
r = _post(client, "frontal.jpg", headers={})
|
||
assert r.status_code == 401
|
||
|
||
|
||
def test_health_no_token_200(client):
|
||
r = client.get("/health")
|
||
assert r.status_code == 200
|
||
assert r.json()["status"] == "ok"
|
||
|
||
|
||
def test_param_none_provided_1007(client):
|
||
r = client.post(URL, headers=H)
|
||
assert r.json()["code"] == 1007
|
||
|
||
|
||
def test_param_multiple_provided_1007(client):
|
||
r = _post(client, "frontal.jpg", data={"image_url": "http://example.com/x.jpg"})
|
||
assert r.json()["code"] == 1007
|
||
|
||
|
||
def test_oversize_1006(client, oversize_file):
|
||
files = {"image_file": ("oversize.bin", open(oversize_file, "rb"), "application/octet-stream")}
|
||
r = client.post(URL, headers=H, files=files)
|
||
assert r.json()["code"] == 1006
|
||
|
||
|
||
def test_lowres_1002(client):
|
||
r = _post(client, "lowres.png")
|
||
assert r.json()["code"] == 1002
|
||
|
||
|
||
def test_no_face_1001(client):
|
||
r = _post(client, "landscape.jpg")
|
||
assert r.json()["code"] == 1001
|
||
|
||
|
||
def test_corrupt_1008(client):
|
||
r = _post(client, "corrupt.bin")
|
||
assert r.json()["code"] == 1008
|
||
|
||
|
||
def test_success_structure(client):
|
||
r = _post(client, "frontal.jpg")
|
||
body = r.json()
|
||
assert body["code"] == 0, body
|
||
data = body["data"]
|
||
# 业务字段对齐文档
|
||
assert set(["face_total_height_cm", "four_courts", "seven_eyes",
|
||
"landmarks", "hairline_source", "head_pose",
|
||
"annotated_image_base64"]).issubset(data.keys())
|
||
assert set(["top_court_cm", "upper_court_cm", "middle_court_cm",
|
||
"lower_court_cm", "ratios"]).issubset(data["four_courts"].keys())
|
||
assert set(["eye_width_cm", "face_width_cm", "inter_eye_distance_cm",
|
||
"ratios"]).issubset(data["seven_eyes"].keys())
|
||
assert data["hairline_source"] in ("segmentation", "estimated")
|
||
# base64 解码为合法 PNG(非 URL)
|
||
assert "annotated_image_url" not in data
|
||
png = base64.b64decode(data["annotated_image_base64"])
|
||
assert png[:8] == b"\x89PNG\r\n\x1a\n"
|