|
| 1 | +# =============================================================================== |
| 2 | +# Copyright 2025 ross |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# =============================================================================== |
| 16 | +from db.engine import get_db_session |
| 17 | +from tests import client |
| 18 | + |
| 19 | + |
| 20 | +def test_health_ok(): |
| 21 | + """A healthy service pings the DB and reports db=ok with a 200.""" |
| 22 | + response = client.get("/health") |
| 23 | + assert response.status_code == 200 |
| 24 | + body = response.json() |
| 25 | + assert body["status"] == "ok" |
| 26 | + assert body["db"] == "ok" |
| 27 | + assert "version" in body |
| 28 | + |
| 29 | + |
| 30 | +def test_health_db_down_returns_503(): |
| 31 | + """When the DB ping fails, /health returns 503 and reports db=error.""" |
| 32 | + |
| 33 | + class _BadSession: |
| 34 | + def execute(self, *args, **kwargs): |
| 35 | + raise Exception("simulated database outage") |
| 36 | + |
| 37 | + def _bad_session(): |
| 38 | + yield _BadSession() |
| 39 | + |
| 40 | + client.app.dependency_overrides[get_db_session] = _bad_session |
| 41 | + try: |
| 42 | + response = client.get("/health") |
| 43 | + assert response.status_code == 503 |
| 44 | + body = response.json() |
| 45 | + assert body["status"] == "degraded" |
| 46 | + assert body["db"] == "error" |
| 47 | + finally: |
| 48 | + client.app.dependency_overrides.pop(get_db_session, None) |
0 commit comments