test(health): cover 503 on DB failure (#26)
This commit is contained in:
parent
7e59d3432d
commit
d282416a34
1 changed files with 34 additions and 0 deletions
|
|
@ -1,7 +1,41 @@
|
||||||
from __future__ import annotations
|
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):
|
def test_healthz_returns_ok(client):
|
||||||
response = client.get("/healthz")
|
response = client.get("/healthz")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json() == {"status": "ok"}
|
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"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue