From d282416a34f105494b432ca64586cc1bd8d863cf Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Sun, 19 Apr 2026 11:56:46 -0600 Subject: [PATCH] test(health): cover 503 on DB failure (#26) --- tests/test_health.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_health.py b/tests/test_health.py index 79c973b..a19ab73 100644 --- a/tests/test_health.py +++ b/tests/test_health.py @@ -1,7 +1,41 @@ from __future__ import annotations +from sqlalchemy.exc import OperationalError + +from quartermaster.db import get_session +from quartermaster.main import create_app +from fastapi.testclient import TestClient + def test_healthz_returns_ok(client): response = client.get("/healthz") assert response.status_code == 200 assert response.json() == {"status": "ok"} + + +def test_healthz_returns_503_when_db_check_raises(): + app = create_app() + + def broken_session(): + class BrokenSession: + def execute(self, *args, **kwargs): + raise OperationalError("boom", None, Exception("boom")) + + def close(self): + pass + + session = BrokenSession() + try: + yield session + finally: + session.close() + + app.dependency_overrides[get_session] = broken_session + + with TestClient(app) as client: + response = client.get("/healthz") + + assert response.status_code == 503 + body = response.json() + assert body["status"] == "error" + assert body["detail"] == "OperationalError"