36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
"""Phase 1 冒烟测试:应用可创建、健康检查可用、错误体格式正确。"""
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
def _client() -> TestClient:
|
|
# with 语句确保 lifespan(配置校验、data 目录创建)真实执行
|
|
return TestClient(app)
|
|
|
|
|
|
def test_liveness() -> None:
|
|
with _client() as client:
|
|
resp = client.get("/api/healthz")
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {"status": "ok"}
|
|
|
|
|
|
def test_readiness_all_components() -> None:
|
|
with _client() as client:
|
|
resp = client.get("/api/health")
|
|
assert resp.status_code == 200
|
|
body = resp.json()
|
|
assert body["status"] == "ok"
|
|
assert set(body["components"]) == {"database", "storage"}
|
|
assert all(c["status"] == "ok" for c in body["components"].values())
|
|
|
|
|
|
def test_404_uses_unified_error_envelope() -> None:
|
|
with _client() as client:
|
|
resp = client.get("/api/nonexistent")
|
|
assert resp.status_code == 404
|
|
body = resp.json()
|
|
assert body["code"] == "NOT_FOUND"
|
|
assert isinstance(body["message"], str) |