import os import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(ROOT / "tools")) import mockserver # noqa: E402 from mediashelf import ingest, providers # noqa: E402 from mediashelf.app import create_app # noqa: E402 from mediashelf.config import Config # noqa: E402 from mediashelf.db import Database # noqa: E402 @pytest.fixture(scope="session") def fake_server(): with mockserver.FakeServer() as srv: yield srv @pytest.fixture def cfg(fake_server, tmp_path, monkeypatch): env = { "PLEX_BASE_URL": fake_server.url, "PLEX_TOKEN": "testtoken", "TAUTULLI_BASE_URL": fake_server.url, "TAUTULLI_API_KEY": "testkey", "DATABASE_PATH": str(tmp_path / "test.db"), "PLEX_PAGE_SIZE": "25", "TAUTULLI_PAGE_SIZE": "300", "PLEX_TIMEOUT_S": "10", "TAUTULLI_TIMEOUT_S": "10", "SCHEDULER_ENABLED": "0", "KEEP_ALL_LIBRARIES": "", } for k, v in env.items(): monkeypatch.setenv(k, v) return Config.from_env() @pytest.fixture def db(cfg): d = Database(cfg.database_path) d.migrate() from mediashelf.scoring import register_sqlite_functions register_sqlite_functions(d.conn) yield d d.close() @pytest.fixture def scanned(db, cfg): """A database with one completed full scan against the fake server.""" media = providers.build_media_provider(cfg) history, _ = providers.build_history_provider(cfg, media) result = ingest.Ingest(db, cfg, media, history).run("full", "manual") assert result.status == "succeeded", result.error return db @pytest.fixture def rescan(db, cfg): def _go(mode="full"): media = providers.build_media_provider(cfg) history, _ = providers.build_history_provider(cfg, media) return ingest.Ingest(db, cfg, media, history).run(mode, "manual") return _go @pytest.fixture def client(cfg, scanned): app = create_app(cfg, start_scheduler=False) app.config["TESTING"] = True with app.test_client() as c: yield c