mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-06-15 17:25:26 -04:00
refactor(tests): add shared CLI test helpers
Adds shared test helpers for CLI script loading and scoped core.database stubs, then converts a low-conflict pilot set of CLI tests. Part of #2523.
This commit is contained in:
committed by
GitHub
parent
cf5c5118d8
commit
dd1fa7e1c4
@@ -0,0 +1,25 @@
|
||||
"""Shared loader for CLI scripts under scripts/."""
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
_SCRIPTS_DIR = Path(__file__).resolve().parents[2] / "scripts"
|
||||
|
||||
|
||||
def load_script(script_name):
|
||||
"""Load a script from scripts/ by name and return it as a module.
|
||||
|
||||
The module name is derived from the script name (hyphens become underscores,
|
||||
with a _cli suffix) giving each script a stable, unique import identity.
|
||||
|
||||
Any sys.modules stubs the script needs at import time must be injected via
|
||||
monkeypatch before calling this function.
|
||||
"""
|
||||
module_name = script_name.replace("-", "_") + "_cli"
|
||||
path = _SCRIPTS_DIR / script_name
|
||||
loader = importlib.machinery.SourceFileLoader(module_name, str(path))
|
||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(module)
|
||||
return module
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Shared database stub helpers for CLI and unit tests."""
|
||||
import sys
|
||||
import types
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def make_core_db_stub(monkeypatch, models=()):
|
||||
"""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.
|
||||
|
||||
Returns the stub module for optional further configuration.
|
||||
"""
|
||||
db = types.ModuleType("core.database")
|
||||
db.SessionLocal = MagicMock()
|
||||
for name in models:
|
||||
setattr(db, name, MagicMock())
|
||||
monkeypatch.setitem(sys.modules, "core.database", db)
|
||||
return db
|
||||
Reference in New Issue
Block a user