test: pilot core database stub helper (#3685)

This commit is contained in:
Alexandre Teixeira
2026-06-09 21:23:33 +01:00
committed by GitHub
parent b1af29c7bc
commit a22c0fa85e
6 changed files with 169 additions and 27 deletions
+15 -2
View File
@@ -4,17 +4,30 @@ import types
from unittest.mock import MagicMock
def make_core_db_stub(monkeypatch, models=()):
def make_core_db_stub(
monkeypatch,
models=(),
*,
attributes=None,
install_core_package=False,
):
"""Create a core.database stub and inject it via monkeypatch.
Always sets SessionLocal. Pass model class names via `models` to set
each as a MagicMock attribute on the stub.
each as a MagicMock attribute on the stub. Pass `attributes` to override
specific values, and `install_core_package` when the import also needs a
stub parent package.
Returns the stub module for optional further configuration.
"""
if install_core_package:
monkeypatch.setitem(sys.modules, "core", types.ModuleType("core"))
db = types.ModuleType("core.database")
db.SessionLocal = MagicMock()
for name in models:
setattr(db, name, MagicMock())
for name, value in (attributes or {}).items():
setattr(db, name, value)
monkeypatch.setitem(sys.modules, "core.database", db)
return db