54 lines
2.1 KiB
Python
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.video, shortstats.chart, shortstats.collect_run, shortstats.channel")
|
|
conn.execute("DELETE FROM shortstats.theme")
|
|
conn.execute("""INSERT INTO shortstats.theme (name, keywords) VALUES
|
|
('Apothecary Diaries', ARRAY['apothecary diaries']),
|
|
('applepie', ARRAY['applepie'])""")
|
|
return db_urls
|
|
|
|
|
|
def add_channel(url: str, channel_id: str, author_id: str, active: bool = True,
|
|
checked: bool = False) -> None:
|
|
"""Ajoute une chaîne suivie et son utilisateur A (comme la page « Chaînes » du site)."""
|
|
with psycopg.connect(url, autocommit=True) as conn:
|
|
conn.execute(
|
|
"""INSERT INTO shortstats.channel (channel_id, author_channel_id, active, checked_at, youtube_title, author_title)
|
|
VALUES (%s, %s, %s, CASE WHEN %s THEN now() END, CASE WHEN %s THEN 'Chaîne' END, CASE WHEN %s THEN 'A' END)""",
|
|
(channel_id, author_id, active, checked, checked, checked),
|
|
)
|