mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-06-17 02:05:22 -04:00
test: pilot core database stub helper (#3685)
This commit is contained in:
committed by
GitHub
parent
b1af29c7bc
commit
a22c0fa85e
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user