|
| 1 | +"""Guards for convention-based SQLModel discovery used by DB bootstrap.""" |
| 2 | + |
| 3 | +import ast |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from bisheng.core.database import model_discovery |
| 11 | +from bisheng.core.database.model_discovery import discover_sqlmodel_module_names, import_all_sqlmodel_models |
| 12 | + |
| 13 | +_BISHENG_ROOT = Path(__file__).resolve().parents[2] / "bisheng" |
| 14 | + |
| 15 | + |
| 16 | +def _declares_sqlmodel_table(path: Path) -> bool: |
| 17 | + tree = ast.parse(path.read_text(encoding="utf-8")) |
| 18 | + return any( |
| 19 | + isinstance(node, ast.ClassDef) |
| 20 | + and any( |
| 21 | + keyword.arg == "table" and isinstance(keyword.value, ast.Constant) and keyword.value.value is True |
| 22 | + for keyword in node.keywords |
| 23 | + ) |
| 24 | + for node in ast.walk(tree) |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def _module_name(path: Path) -> str: |
| 29 | + module_parts = list(path.relative_to(_BISHENG_ROOT.parent).with_suffix("").parts) |
| 30 | + if module_parts[-1] == "__init__": |
| 31 | + module_parts.pop() |
| 32 | + return ".".join(module_parts) |
| 33 | + |
| 34 | + |
| 35 | +def test_discovery_covers_every_literal_sqlmodel_table_module(): |
| 36 | + discovered_modules = {_module_name(path) for path in _BISHENG_ROOT.rglob("*.py") if _declares_sqlmodel_table(path)} |
| 37 | + convention_modules = set(discover_sqlmodel_module_names()) |
| 38 | + |
| 39 | + missing_modules = discovered_modules - convention_modules |
| 40 | + |
| 41 | + assert not missing_modules, ( |
| 42 | + f"SQLModel table modules outside the model-directory conventions: {sorted(missing_modules)}" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def test_strict_model_loading_registers_space_channel_member(): |
| 47 | + result = subprocess.run( |
| 48 | + [ |
| 49 | + sys.executable, |
| 50 | + "-c", |
| 51 | + "from sqlmodel import SQLModel; " |
| 52 | + "from bisheng.core.database.model_discovery import import_all_sqlmodel_models; " |
| 53 | + "import_all_sqlmodel_models(); " |
| 54 | + "assert 'space_channel_member' in SQLModel.metadata.tables", |
| 55 | + ], |
| 56 | + check=False, |
| 57 | + capture_output=True, |
| 58 | + text=True, |
| 59 | + ) |
| 60 | + |
| 61 | + assert result.returncode == 0, result.stderr |
| 62 | + |
| 63 | + |
| 64 | +def test_strict_model_loading_raises_when_a_discovered_module_cannot_import(monkeypatch): |
| 65 | + missing_module = "bisheng.common.models.does_not_exist" |
| 66 | + monkeypatch.setattr(model_discovery, "discover_sqlmodel_module_names", lambda: (missing_module,)) |
| 67 | + |
| 68 | + with pytest.raises(RuntimeError, match=missing_module): |
| 69 | + import_all_sqlmodel_models() |
| 70 | + |
| 71 | + |
| 72 | +def test_strict_model_loading_rejects_empty_discovery(monkeypatch): |
| 73 | + monkeypatch.setattr(model_discovery, "discover_sqlmodel_module_names", tuple) |
| 74 | + |
| 75 | + with pytest.raises(RuntimeError, match="No SQLModel table modules"): |
| 76 | + import_all_sqlmodel_models() |
0 commit comments