gabriel / musehub public
test_health_schema.py python
65 lines 2.2 KB
Raw
sha256:bba4b69de173366aeb7d490d687ccffb6db9726374033addece89cf2b87642d8 docs: add production launch discovery report and infra laun… Sonnet 5 8 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 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 2 commits
sha256:bba4b69de173366aeb7d490d687ccffb6db9726374033addece89cf2b87642d8 docs: add production launch discovery report and infra laun… Sonnet 5 8 days ago
sha256:eb0928124669c933c0ef852bea1ad0c56649cf21bd7e9663c3b51004771b0591 docs: add MuseHub cloud identity/AWS operating model and Go… Sonnet 5 patch 8 days ago