shortstats/tests/conftest.py
ploush 9206143dc5
All checks were successful
build / test (push) Successful in 6m28s
build / images (collector) (push) Successful in 21s
build / images (web) (push) Successful in 19s
upgrade 1.3.0
2026-11-06 19:45:47 +01:00

54 lines
2.1 KiB
Python

"""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
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.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),
)