test(analytics): cover selfstat 503 guard + document empty-events divergence

This commit is contained in:
guke
2026-07-08 17:14:29 +08:00
parent c386103809
commit 615dab0490
2 changed files with 12 additions and 0 deletions
+10
View File
@@ -31,3 +31,13 @@ def test_selfstat_ingest_empty_events(client: TestClient) -> None:
r = client.post("/api/v1/analytics/selfstat", json=_payload(events=[]))
assert r.status_code == 200, r.text
assert r.json()["ok"] is True
def test_selfstat_ingest_db_error_returns_503(client: TestClient, monkeypatch) -> None:
"""落库异常时端点回 503(计数链路稳定性守卫),不裸奔 500。"""
def _boom(*_args, **_kwargs):
raise RuntimeError("db gone")
monkeypatch.setattr("app.repositories.analytics_selfstat.record_selfstat", _boom)
r = client.post("/api/v1/analytics/selfstat", json=_payload())
assert r.status_code == 503, r.text