"""Migration 0116's data step, DB-lane: an explored channel someone already follows becomes the catalog's own, and no video stays flagged as an exploration's on a channel that is not one. The schema half is checked by test_migrations.py; a flag left on is invisible there. """ from alembic import command from alembic.config import Config from sqlalchemy import text from app.db import engine def test_followed_explored_channels_and_their_videos_are_the_catalogs_own(db, make_user): follower = make_user().id cfg = Config("alembic.ini") command.downgrade(cfg, "0115_takeout_channels") try: with engine.begin() as conn: def channel(cid, explored): conn.execute( text("INSERT INTO channels (id, from_explore) VALUES (:c, :e)"), {"c": cid, "e": explored}, ) conn.execute( text( "INSERT INTO videos (id, channel_id, published_at, via_explore)" " VALUES (:v, :c, now(), true)" ), {"v": f"v-{cid}", "c": cid}, ) channel("UCfollowed", True) # explored, then followed on youtube.com channel("UCexplored", True) # explored, nobody follows it: stays the exploration's channel("UCorganic", False) # a video flagged after a subscribe had cleared the channel conn.execute( text("INSERT INTO subscriptions (user_id, channel_id) VALUES (:u, 'UCfollowed')"), {"u": follower}, ) finally: command.upgrade(cfg, "head") with engine.connect() as conn: channels = dict(conn.execute(text("SELECT id, from_explore FROM channels")).tuples().all()) videos = dict( conn.execute(text("SELECT channel_id, via_explore FROM videos")).tuples().all() ) assert channels == {"UCfollowed": False, "UCexplored": True, "UCorganic": False} assert videos == {"UCfollowed": False, "UCexplored": True, "UCorganic": False}