"""Migration 0141 against real Postgres: every promoted idea still on a project's record moves onto a family record with no project. The lane migrates an empty database, so the migration's work is driven here directly: rows are seeded the way a promotion made them before promotion lifted, then the migration's own function runs on the test connection. """ import importlib.util from pathlib import Path import pytest import pytest_asyncio from sqlalchemy import select from grimoire.models import async_session from grimoire.models.family import ( FamilyAdoption, FamilyDecision, FamilyIdea, FamilyIdeaPlatform, FamilyIdeaReference, Platform, ProjectPlatform, ) from grimoire.models.note import Note from grimoire.models.project import Project from grimoire.models.share import NoteShare, ProjectShare from grimoire.models.user import User from grimoire.services import access from grimoire.services import family as family_svc from tests.helpers import ensure_user pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")] OWNER = "family_records_owner" READER = "family_records_reader" _MIGRATION = Path(__file__).resolve().parents[1] / "alembic" / "versions" / "0141_family_records.py" def _migration(): spec = importlib.util.spec_from_file_location("migration_0141", _MIGRATION) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module async def _purge(username: str) -> None: """SETUP ONLY: a database call after a `yield` in a fixture orphans a pooled connection. Notes and projects cascade to every family row.""" async with async_session() as s: for user in (await s.execute(select(User).where(User.username == username))).scalars(): for note in (await s.execute(select(Note).where(Note.user_id == user.id))).scalars(): await s.delete(note) for project in (await s.execute(select(Project).where(Project.user_id == user.id))).scalars(): await s.delete(project) await s.commit() @pytest_asyncio.fixture async def promoted(): """A Kotlin snippet in the first of two Android apps, promoted to canon the way promotion stored it before it lifted: the idea on the snippet, an unassessed row for each app, the first app shared with a reader.""" await _purge(OWNER) await _purge(READER) async with async_session() as s: android = await s.scalar(select(Platform.id).where( Platform.slug == "android-app", Platform.deleted_at.is_(None))) owner = await ensure_user(s, OWNER) reader = await ensure_user(s, READER) a = Project(user_id=owner.id, title="android one") b = Project(user_id=owner.id, title="android two") s.add_all([a, b]) await s.flush() s.add_all([ProjectPlatform(project_id=a.id, platform_id=android, state="declared"), ProjectPlatform(project_id=b.id, platform_id=android, state="declared"), ProjectShare(project_id=a.id, shared_with_user_id=reader.id, permission="viewer", invited_by=owner.id)]) snippet = Note(user_id=owner.id, project_id=a.id, note_type="snippet", title="ReleaseSigner", body="fun sign(apk: File) = ...", tags=["android"], data={"language": "kotlin"}) candidate = Note(user_id=owner.id, project_id=b.id, note_type="snippet", title="Unproven", body="...") s.add_all([snippet, candidate]) await s.flush() s.add_all([ FamilyIdea(note_id=snippet.id, status="canon", canon_version=1, applies_when="any Android app that ships its own APK"), FamilyIdea(note_id=candidate.id, status="candidate"), ]) await s.flush() s.add_all([ FamilyIdeaPlatform(note_id=snippet.id, platform_id=android), FamilyAdoption(project_id=a.id, idea_id=snippet.id, status="unassessed"), FamilyAdoption(project_id=b.id, idea_id=snippet.id, status="unassessed"), FamilyDecision(idea_id=snippet.id, action="propose", reason="repeated", decided_via="system", user_id=owner.id), FamilyDecision(idea_id=snippet.id, action="promote", reason="proven", decided_via="agent", user_id=owner.id), FamilyDecision(idea_id=candidate.id, action="propose", reason="repeated", decided_via="system", user_id=owner.id), ]) await s.commit() return {"owner": owner.id, "reader": reader.id, "a": a.id, "b": b.id, "snippet": snippet.id, "candidate": candidate.id} async def _lift() -> list[tuple[int, int]]: module = _migration() async with async_session() as s: conn = await s.connection() moved = await conn.run_sync(module.lift_promoted_ideas) await s.commit() return moved async def test_a_promoted_idea_moves_onto_a_family_record_and_a_candidate_stays(promoted): moved = await _lift() [(origin, family_id)] = [m for m in moved if m[0] == promoted["snippet"]] async with async_session() as s: record = await s.get(Note, family_id) snippet = await s.get(Note, origin) assert await s.get(FamilyIdea, origin) is None assert (await s.get(FamilyIdea, family_id)).status == "canon" assert (await s.get(FamilyIdea, promoted["candidate"])).status == "candidate" actions = (await s.execute(select(FamilyDecision.action).where( FamilyDecision.idea_id == family_id, FamilyDecision.project_id.is_(None)) .order_by(FamilyDecision.id))).scalars().all() platforms = (await s.execute(select(FamilyIdeaPlatform.note_id))).scalars().all() refs = (await s.execute(select(FamilyIdeaReference.snippet_id).where( FamilyIdeaReference.idea_id == family_id))).scalars().all() assert record.project_id is None and record.user_id == promoted["owner"] assert record.note_type == "note" and record.arose_from_id == origin assert record.tags == ["android"] and f"#{origin}" in record.body assert snippet.project_id == promoted["a"] and snippet.body == "fun sign(apk: File) = ..." assert actions == ["propose", "promote"] assert family_id in platforms and origin not in platforms assert refs == [origin] assert await family_svc.idea_key(origin) == family_id async def test_the_project_holding_the_reference_is_answered_and_the_other_still_owes(promoted): [(_, family_id)] = [m for m in await _lift() if m[0] == promoted["snippet"]] async with async_session() as s: rows = dict((await s.execute(select(FamilyAdoption.project_id, FamilyAdoption.status) .where(FamilyAdoption.idea_id == family_id))).all()) answer = (await s.execute(select(FamilyDecision).where( FamilyDecision.idea_id == family_id, FamilyDecision.project_id == promoted["a"]))).scalars().one() assert rows == {promoted["a"]: "adopted", promoted["b"]: "unassessed"} assert answer.decided_via == "system" and answer.evidence["source"] == "reference" assert answer.after["status"] == "adopted" and answer.before["status"] == "unassessed" async def test_whoever_read_it_through_its_project_reads_the_family_record(promoted): [(_, family_id)] = [m for m in await _lift() if m[0] == promoted["snippet"]] assert await access.get_note_permission(promoted["reader"], family_id) == "viewer" async with async_session() as s: shares = (await s.execute(select(NoteShare).where(NoteShare.note_id == family_id))).scalars().all() assert [(sh.shared_with_user_id, sh.permission) for sh in shares] == [(promoted["reader"], "viewer")] async def test_running_it_again_lifts_nothing(promoted): first = await _lift() assert promoted["snippet"] in [o for o, _ in first] assert promoted["snippet"] not in [o for o, _ in await _lift()]