test(health): assert healthz_failed log on 503 (#26)

This commit is contained in:
Jeff Smith 2026-04-19 12:00:12 -06:00
parent d282416a34
commit 75c825924b

View file

@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
import logging
from sqlalchemy.exc import OperationalError from sqlalchemy.exc import OperationalError
from quartermaster.db import get_session from quartermaster.db import get_session
@ -13,7 +15,7 @@ def test_healthz_returns_ok(client):
assert response.json() == {"status": "ok"} assert response.json() == {"status": "ok"}
def test_healthz_returns_503_when_db_check_raises(): def test_healthz_returns_503_when_db_check_raises(caplog):
app = create_app() app = create_app()
def broken_session(): def broken_session():
@ -32,6 +34,7 @@ def test_healthz_returns_503_when_db_check_raises():
app.dependency_overrides[get_session] = broken_session app.dependency_overrides[get_session] = broken_session
caplog.set_level(logging.WARNING, logger="quartermaster.health")
with TestClient(app) as client: with TestClient(app) as client:
response = client.get("/healthz") response = client.get("/healthz")
@ -39,3 +42,8 @@ def test_healthz_returns_503_when_db_check_raises():
body = response.json() body = response.json()
assert body["status"] == "error" assert body["status"] == "error"
assert body["detail"] == "OperationalError" assert body["detail"] == "OperationalError"
failed = [r for r in caplog.records if getattr(r, "event", None) == "healthz_failed"]
assert len(failed) == 1
assert failed[0].levelname == "WARNING"
assert failed[0].error_class == "OperationalError"