2026-04-19 11:53:09 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-19 11:56:46 -06:00
|
|
|
from sqlalchemy.exc import OperationalError
|
|
|
|
|
|
|
|
|
|
from quartermaster.db import get_session
|
|
|
|
|
from quartermaster.main import create_app
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
2026-04-19 11:53:09 -06:00
|
|
|
|
|
|
|
|
def test_healthz_returns_ok(client):
|
|
|
|
|
response = client.get("/healthz")
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.json() == {"status": "ok"}
|
2026-04-19 11:56:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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"
|