test_health_schema.py
python
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠ breaking
1 day ago
| 1 | """Tests for GET /api/health/schema endpoint in musehub. |
| 2 | |
| 3 | Covers: |
| 4 | - 200 {"ok": true} when schema matches ORM |
| 5 | - 503 {"ok": false, "drift": [...]} when schema drift is detected |
| 6 | - 503 on unexpected engine errors |
| 7 | |
| 8 | Run targeted: |
| 9 | docker compose exec musehub pytest tests/test_health_schema.py -v |
| 10 | """ |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | from unittest.mock import AsyncMock, MagicMock, patch |
| 14 | |
| 15 | import pytest |
| 16 | from httpx import AsyncClient |
| 17 | |
| 18 | |
| 19 | async def test_health_schema_returns_200_when_ok(client: AsyncClient) -> None: |
| 20 | """Returns 200 {"ok": true} when assert_schema_matches_orm succeeds.""" |
| 21 | with ( |
| 22 | patch("musehub.db.database.get_engine", return_value=MagicMock()), |
| 23 | patch( |
| 24 | "musehub.db.schema_check.assert_schema_matches_orm", |
| 25 | new_callable=AsyncMock, |
| 26 | ), |
| 27 | ): |
| 28 | resp = await client.get("/api/health/schema") |
| 29 | assert resp.status_code == 200 |
| 30 | assert resp.json() == {"ok": True} |
| 31 | |
| 32 | |
| 33 | async def test_health_schema_returns_503_on_drift(client: AsyncClient) -> None: |
| 34 | """Returns 503 with drift list when assert_schema_matches_orm raises RuntimeError.""" |
| 35 | error_msg = ( |
| 36 | "Schema drift detected — 1 mismatch(es):\n" |
| 37 | " • musehub_proposals.'breakage_count': nullable mismatch (ORM=False, DB=True)" |
| 38 | ) |
| 39 | with ( |
| 40 | patch("musehub.db.database.get_engine", return_value=MagicMock()), |
| 41 | patch( |
| 42 | "musehub.db.schema_check.assert_schema_matches_orm", |
| 43 | new_callable=AsyncMock, |
| 44 | side_effect=RuntimeError(error_msg), |
| 45 | ), |
| 46 | ): |
| 47 | resp = await client.get("/api/health/schema") |
| 48 | assert resp.status_code == 503 |
| 49 | body = resp.json() |
| 50 | assert body["ok"] is False |
| 51 | assert isinstance(body["drift"], list) |
| 52 | assert len(body["drift"]) == 1 |
| 53 | assert "breakage_count" in body["drift"][0] |
| 54 | |
| 55 | |
| 56 | async def test_health_schema_returns_503_when_engine_unavailable(client: AsyncClient) -> None: |
| 57 | """Returns 503 when the engine is not initialised yet.""" |
| 58 | with patch( |
| 59 | "musehub.db.database.get_engine", |
| 60 | side_effect=RuntimeError("Database not initialized"), |
| 61 | ): |
| 62 | resp = await client.get("/api/health/schema") |
| 63 | assert resp.status_code == 503 |
| 64 | body = resp.json() |
| 65 | assert body["ok"] is False |
File History
1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2
feat: add repair-commit wire endpoint (API parity with repa…
Opus 4.8
minor
⚠
1 day ago