gabriel / musehub public
test_health_schema.py python
68 lines 2.2 KB
Raw
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 171 days 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 @pytest.mark.anyio
20 async def test_health_schema_returns_200_when_ok(client: AsyncClient) -> None:
21 """Returns 200 {"ok": true} when assert_schema_matches_orm succeeds."""
22 with (
23 patch("musehub.db.database.get_engine", return_value=MagicMock()),
24 patch(
25 "musehub.db.schema_check.assert_schema_matches_orm",
26 new_callable=AsyncMock,
27 ),
28 ):
29 resp = await client.get("/api/health/schema")
30 assert resp.status_code == 200
31 assert resp.json() == {"ok": True}
32
33
34 @pytest.mark.anyio
35 async def test_health_schema_returns_503_on_drift(client: AsyncClient) -> None:
36 """Returns 503 with drift list when assert_schema_matches_orm raises RuntimeError."""
37 error_msg = (
38 "Schema drift detected — 1 mismatch(es):\n"
39 " • musehub_proposals.'breakage_count': nullable mismatch (ORM=False, DB=True)"
40 )
41 with (
42 patch("musehub.db.database.get_engine", return_value=MagicMock()),
43 patch(
44 "musehub.db.schema_check.assert_schema_matches_orm",
45 new_callable=AsyncMock,
46 side_effect=RuntimeError(error_msg),
47 ),
48 ):
49 resp = await client.get("/api/health/schema")
50 assert resp.status_code == 503
51 body = resp.json()
52 assert body["ok"] is False
53 assert isinstance(body["drift"], list)
54 assert len(body["drift"]) == 1
55 assert "breakage_count" in body["drift"][0]
56
57
58 @pytest.mark.anyio
59 async def test_health_schema_returns_503_when_engine_unavailable(client: AsyncClient) -> None:
60 """Returns 503 when the engine is not initialised yet."""
61 with patch(
62 "musehub.db.database.get_engine",
63 side_effect=RuntimeError("Database not initialized"),
64 ):
65 resp = await client.get("/api/health/schema")
66 assert resp.status_code == 503
67 body = resp.json()
68 assert body["ok"] is False
File History 1 commit
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 171 days ago