clipwright/tests/conftest.py
pedro-bento ab5cdb87a9 feat(channels): store channel profiles in local SQLite with CRUD API
Replace the committed channels/<slug>.json files as the runtime source of
truth with a local SQLite database at the repo root (clipwright.db),
overridable via CLIPWRIGHT_DB. Existing JSON profiles are imported once on
first bootstrap, then the store is authoritative.

- add clipwright/storage/channel_profiles.py (stdlib sqlite3, WAL + lock)
- add Profile/TitleRules/ThumbnailRules.to_dict() for round-tripping
- load_profile/list_profiles read the store; channels_dir stays a file seam
- /api/channels CRUD plus import/export endpoints
- untrack channels/*.json, ignore the DB files
- seed a temp DB in tests/conftest.py
2026-10-06 11:54:29 +01:00

117 lines
3.4 KiB
Python

"""Shared pytest configuration.
The repo-root ``clipwright/`` package is made importable by
``[tool.pytest.ini_options] pythonpath = ["."]`` in the root ``pyproject.toml``,
so no manual ``sys.path`` manipulation is needed here.
The unit test environment deliberately omits the heavy ML stack
(torch/ultralytics/mediapipe/faster-whisper) so CI stays fast.
``clipwright.pipeline.main`` imports ``ultralytics`` and ``mediapipe`` at module
level, which made every test that does ``pytest.importorskip("...main")`` skip.
To keep that coverage without pulling gigabytes into CI, we install minimal
stand-ins *only when the real packages are absent* (the production image and a
developer machine with the real deps installed are unaffected).
"""
from __future__ import annotations
import os
import sys
import types
from types import SimpleNamespace
import pytest
import _channel_seed
class _FakeYOLO:
def __init__(self, *args, **kwargs):
self.args = args
def __call__(self, *args, **kwargs):
return []
def predict(self, *args, **kwargs):
return []
def track(self, *args, **kwargs):
return []
class _FakeDetection:
def __init__(self, *args, **kwargs):
pass
def process(self, *args, **kwargs):
return SimpleNamespace(detections=None)
def close(self):
pass
def _is_importable(name: str) -> bool:
try:
__import__(name)
return True
except Exception:
return False
def _install_heavy_stubs() -> None:
if not _is_importable("ultralytics"):
ultralytics = types.ModuleType("ultralytics")
ultralytics.YOLO = _FakeYOLO
sys.modules["ultralytics"] = ultralytics
if not _is_importable("mediapipe"):
mediapipe = types.ModuleType("mediapipe")
solutions = types.ModuleType("mediapipe.solutions")
def _solution(name: str) -> types.ModuleType:
mod = types.ModuleType(f"mediapipe.solutions.{name}")
mod.FaceDetection = _FakeDetection
mod.Detection = _FakeDetection
sys.modules[f"mediapipe.solutions.{name}"] = mod
return mod
for name in (
"face_detection",
"face_mesh",
"pose",
"hands",
"holistic",
"selfie_segmentation",
):
setattr(solutions, name, _solution(name))
mediapipe.solutions = solutions
sys.modules["mediapipe"] = mediapipe
sys.modules["mediapipe.solutions"] = solutions
_install_heavy_stubs()
@pytest.fixture(scope="session", autouse=True)
def _channel_profile_db(tmp_path_factory):
"""Point channel-profile storage at a temp SQLite DB seeded with cseea/pedro.
Autouse and session-scoped so every test (including ``tests/e2e/**``, which
inherits this conftest) reads the same DB-backed profiles instead of the
legacy ``channels/*.json`` files. The ``CLIPWRIGHT_DB`` override is removed
on teardown so nothing leaks into a later run.
"""
from clipwright.pipeline import profiles
db_dir = tmp_path_factory.mktemp("channel-db")
os.environ["CLIPWRIGHT_DB"] = str(db_dir / "clipwright.db")
try:
# First ``save_profile`` also runs the one-time file migration, but we
# seed explicitly so the DB never depends on the checkout's files.
for slug, data in _channel_seed.SEED.items():
profiles.save_profile(slug, data)
yield
finally:
os.environ.pop("CLIPWRIGHT_DB", None)