gabriel / musehub public

test_health_schema.py file-level

at sha256:3 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:0 fix: fall back to any indexed mpack in read_object_bytes when push mpac… · gabriel · Jun 17, 2026
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