42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
|
|
|
|
def test_health() -> None:
|
|
response = TestClient(app).get("/health")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok", "service": "mobilityops-api"}
|
|
|
|
|
|
def test_liveness_is_process_only() -> None:
|
|
response = TestClient(app).get("/health/live")
|
|
assert response.status_code == 200
|
|
assert response.json()["status"] == "ok"
|
|
|
|
|
|
def test_readiness_checks_the_canonical_database() -> None:
|
|
response = TestClient(app).get("/health/ready")
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"status": "ready",
|
|
"service": "mobilityops-api",
|
|
"database": "up",
|
|
}
|
|
|
|
|
|
def test_readiness_degrades_when_database_is_unavailable(monkeypatch) -> None:
|
|
import app.main as main_module
|
|
|
|
class BrokenSession:
|
|
def __enter__(self):
|
|
raise ConnectionError("database unavailable")
|
|
|
|
def __exit__(self, *_args):
|
|
return False
|
|
|
|
monkeypatch.setattr(main_module, "SessionLocal", BrokenSession)
|
|
response = TestClient(app).get("/health/ready")
|
|
assert response.status_code == 503
|
|
assert response.json()["database"] == "down"
|