"""Fixtures communes. Les tests qui touchent PostgreSQL ont besoin de trois URL (base déjà créée avec sql/bootstrap.sql) : TEST_OWNER_URL, TEST_COLLECTOR_URL, TEST_WEB_URL Sans elles, ces tests sont ignorés ; les tests purs tournent toujours. """ from __future__ import annotations import os import psycopg import pytest from shortstats import migrate from shortstats.config import MigrateConfig from . import progression from .progression import pytest_addoption # noqa: F401 (options --progression) def pytest_configure(config): progression.register(config) OWNER = os.environ.get("TEST_OWNER_URL") COLLECTOR = os.environ.get("TEST_COLLECTOR_URL") WEB = os.environ.get("TEST_WEB_URL") @pytest.fixture(scope="session") def db_urls(): if not (OWNER and COLLECTOR and WEB): pytest.skip("TEST_OWNER_URL / TEST_COLLECTOR_URL / TEST_WEB_URL non définies") migrate.run(MigrateConfig(database_url=OWNER, timezone="Europe/Paris", collector_role="shortstats_collector", web_role="shortstats_web")) return {"owner": OWNER, "collector": COLLECTOR, "web": WEB} @pytest.fixture() def clean_db(db_urls): with psycopg.connect(db_urls["owner"], autocommit=True) as conn: conn.execute("TRUNCATE shortstats.comment_estimate, shortstats.snapshot_theme, shortstats.comment_theme, shortstats.comment, shortstats.video_snapshot, shortstats.video, shortstats.chart, shortstats.collect_request, shortstats.collect_run, shortstats.channel, shortstats.user_password") conn.execute("DELETE FROM shortstats.theme") conn.execute("""INSERT INTO shortstats.theme (name, patterns) VALUES ('Apothecary Diaries', ARRAY['apothecary diaries']), ('applepie', ARRAY['applepie'])""") return db_urls def add_channel(url: str, channel_id: str, active: bool = True, checked: bool = False, scope: str = "top") -> None: """Ajoute une chaîne suivie (comme la page « Chaînes » du site).""" with psycopg.connect(url, autocommit=True) as conn: conn.execute( """INSERT INTO shortstats.channel (channel_id, active, checked_at, youtube_title, comment_scope) VALUES (%s, %s, CASE WHEN %s THEN now() END, CASE WHEN %s THEN 'Chaîne' END, %s)""", (channel_id, active, checked, checked, scope), )