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:
Alexandre Teixeira
2026-06-04 15:44:25 +01:00
committed by GitHub
parent cf5c5118d8
commit dd1fa7e1c4
12 changed files with 87 additions and 208 deletions

View File

@@ -1,30 +1,10 @@
import importlib.machinery
import importlib.util
import sys
import types
from pathlib import Path
from unittest.mock import MagicMock
ROOT = Path(__file__).resolve().parents[1]
def _load_cli(monkeypatch):
db = types.ModuleType("core.database")
db.SessionLocal = MagicMock()
db.Document = MagicMock()
db.DocumentVersion = MagicMock()
monkeypatch.setitem(sys.modules, "core.database", db)
path = ROOT / "scripts" / "odysseus-docs"
loader = importlib.machinery.SourceFileLoader("odysseus_docs_cli", str(path))
spec = importlib.util.spec_from_loader(loader.name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
from tests.helpers.cli_loader import load_script
from tests.helpers.db_stubs import make_core_db_stub
def test_text_len_ignores_non_string_values(monkeypatch):
cli = _load_cli(monkeypatch)
make_core_db_stub(monkeypatch, models=["Document", "DocumentVersion"])
cli = load_script("odysseus-docs")
assert cli._text_len("hello") == 5
assert cli._text_len(None) == 0