|
| 1 | +""" |
| 2 | +Unit tests for schema routes (Issue #31: Multi-DB registry wiring). |
| 3 | +""" |
| 4 | + |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | +from fastapi import FastAPI |
| 9 | +from fastapi.testclient import TestClient |
| 10 | + |
| 11 | +from app.routes import router |
| 12 | +from db.schema import SchemaInfo |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def app() -> FastAPI: |
| 17 | + """Create test FastAPI app.""" |
| 18 | + from app.error_handlers import setup_exception_handlers |
| 19 | + from app.security import limiter |
| 20 | + |
| 21 | + test_app = FastAPI() |
| 22 | + test_app.state.limiter = limiter |
| 23 | + setup_exception_handlers(test_app) |
| 24 | + test_app.include_router(router) |
| 25 | + |
| 26 | + return test_app |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def client(app: FastAPI, auth_headers: dict[str, str]) -> TestClient: |
| 31 | + """Create test client.""" |
| 32 | + client = TestClient(app) |
| 33 | + client.headers.update(auth_headers) |
| 34 | + return client |
| 35 | + |
| 36 | + |
| 37 | +class TestSchemaRoutes: |
| 38 | + """Tests for schema registration endpoints.""" |
| 39 | + |
| 40 | + def test_register_schema_registers_and_caches( |
| 41 | + self, |
| 42 | + client: TestClient, |
| 43 | + ) -> None: |
| 44 | + """Ensure schema registration wires registry and cache.""" |
| 45 | + schema_info = SchemaInfo( |
| 46 | + database_id="analytics", |
| 47 | + dialect="postgresql", |
| 48 | + tables=[], |
| 49 | + ) |
| 50 | + |
| 51 | + mock_introspector = MagicMock() |
| 52 | + mock_introspector.get_schema = AsyncMock(return_value=schema_info) |
| 53 | + mock_introspector.serialize_for_prompt.return_value = "schema" |
| 54 | + |
| 55 | + registry = MagicMock() |
| 56 | + registry.database_count = 0 |
| 57 | + registry.register_database = AsyncMock() |
| 58 | + registered = MagicMock() |
| 59 | + registered.engine = MagicMock() |
| 60 | + registry.register_database.return_value = registered |
| 61 | + |
| 62 | + settings = MagicMock() |
| 63 | + settings.multi_database.enabled = True |
| 64 | + settings.multi_database.max_databases = 50 |
| 65 | + settings.multi_database.default_pool_size = 5 |
| 66 | + settings.multi_database.default_max_overflow = 10 |
| 67 | + settings.multi_database.default_pool_timeout = 30 |
| 68 | + settings.multi_database.allow_mutations = False |
| 69 | + settings.multi_database.require_connection_test = True |
| 70 | + settings.cache.enabled = True |
| 71 | + |
| 72 | + with ( |
| 73 | + patch("app.routes.get_settings", return_value=settings), |
| 74 | + patch("db.registry.get_database_registry", return_value=registry), |
| 75 | + patch("db.schema.SchemaIntrospector", return_value=mock_introspector), |
| 76 | + patch("app.cache.cache_schema", new_callable=AsyncMock) as cache_schema, |
| 77 | + ): |
| 78 | + response = client.post( |
| 79 | + "/api/v1/schema/register", |
| 80 | + json={ |
| 81 | + "database_id": "analytics", |
| 82 | + "connection_string": "sqlite:///test.db", |
| 83 | + "dialect": "postgresql", |
| 84 | + }, |
| 85 | + ) |
| 86 | + |
| 87 | + assert response.status_code == 200 |
| 88 | + payload = response.json() |
| 89 | + assert payload["status"] == "registered" |
| 90 | + assert payload["database_id"] == "analytics" |
| 91 | + assert payload["dialect"] == "postgresql" |
| 92 | + |
| 93 | + registry.register_database.assert_awaited_once() |
| 94 | + args, kwargs = registry.register_database.call_args |
| 95 | + config = args[0] |
| 96 | + assert config.database_id == "analytics" |
| 97 | + assert config.connection_string == "sqlite:///test.db" |
| 98 | + assert config.pool_size == 5 |
| 99 | + assert kwargs["test_connection"] is True |
| 100 | + |
| 101 | + mock_introspector.get_schema.assert_awaited_once_with("analytics") |
| 102 | + cache_schema.assert_awaited_once() |
0 commit comments