|
| 1 | +"""Schema validation tests against real PostgreSQL with pgvector. |
| 2 | +
|
| 3 | +These run in CI via the Tekton pipeline's Postgres sidecar. |
| 4 | +Future migration stages add tests here for ALTER TABLE + backfill scripts. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncpg |
| 8 | +import pytest |
| 9 | + |
| 10 | +from conftest import SCHEMA_PATH |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_db_connection(db): |
| 15 | + result = await db.fetchval("SELECT 1") |
| 16 | + assert result == 1 |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_schema_applies(db): |
| 21 | + schema = SCHEMA_PATH.read_text() |
| 22 | + await db.execute(schema) |
| 23 | + |
| 24 | + tables = await db.fetch( |
| 25 | + "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" |
| 26 | + ) |
| 27 | + table_names = {t["tablename"] for t in tables} |
| 28 | + |
| 29 | + expected = { |
| 30 | + "tasks", |
| 31 | + "memories", |
| 32 | + "bot_status", |
| 33 | + "cycles", |
| 34 | + "cycle_runs", |
| 35 | + "slack_notifications", |
| 36 | + "bot_instances", |
| 37 | + } |
| 38 | + missing = expected - table_names |
| 39 | + assert not missing, f"Missing tables: {missing}" |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_schema_idempotent(db): |
| 44 | + schema = SCHEMA_PATH.read_text() |
| 45 | + await db.execute(schema) |
| 46 | + await db.execute(schema) |
| 47 | + count = await db.fetchval("SELECT COUNT(*) FROM tasks") |
| 48 | + assert count == 0 |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.asyncio |
| 52 | +async def test_task_insert_read(db): |
| 53 | + schema = SCHEMA_PATH.read_text() |
| 54 | + await db.execute(schema) |
| 55 | + |
| 56 | + await db.execute( |
| 57 | + """ |
| 58 | + INSERT INTO tasks (jira_key, status, repo, branch) |
| 59 | + VALUES ($1, $2, $3, $4) |
| 60 | + """, |
| 61 | + "TEST-1", |
| 62 | + "in_progress", |
| 63 | + "test-repo", |
| 64 | + "bot/TEST-1", |
| 65 | + ) |
| 66 | + |
| 67 | + task = await db.fetchrow("SELECT * FROM tasks WHERE jira_key = $1", "TEST-1") |
| 68 | + assert task is not None |
| 69 | + assert task["status"] == "in_progress" |
| 70 | + assert task["repo"] == "test-repo" |
| 71 | + assert task["branch"] == "bot/TEST-1" |
| 72 | + assert task["pr_number"] is None |
| 73 | + assert task["pr_url"] is None |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_task_unique_constraint(db): |
| 78 | + schema = SCHEMA_PATH.read_text() |
| 79 | + await db.execute(schema) |
| 80 | + |
| 81 | + await db.execute( |
| 82 | + "INSERT INTO tasks (jira_key, status, repo, branch) VALUES ($1, $2, $3, $4)", |
| 83 | + "DUP-1", |
| 84 | + "in_progress", |
| 85 | + "repo", |
| 86 | + "bot/DUP-1", |
| 87 | + ) |
| 88 | + |
| 89 | + with pytest.raises(asyncpg.UniqueViolationError): |
| 90 | + await db.execute( |
| 91 | + "INSERT INTO tasks (jira_key, status, repo, branch) VALUES ($1, $2, $3, $4)", |
| 92 | + "DUP-1", |
| 93 | + "pr_open", |
| 94 | + "repo2", |
| 95 | + "bot/DUP-1-2", |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_foreign_keys(db): |
| 101 | + schema = SCHEMA_PATH.read_text() |
| 102 | + await db.execute(schema) |
| 103 | + |
| 104 | + await db.execute( |
| 105 | + "INSERT INTO tasks (jira_key, status, repo, branch) VALUES ($1, $2, $3, $4)", |
| 106 | + "FK-1", |
| 107 | + "in_progress", |
| 108 | + "repo", |
| 109 | + "bot/FK-1", |
| 110 | + ) |
| 111 | + task_id = await db.fetchval("SELECT id FROM tasks WHERE jira_key = $1", "FK-1") |
| 112 | + |
| 113 | + await db.execute( |
| 114 | + """ |
| 115 | + INSERT INTO cycle_runs (task_id, cycle_type, instance_id) |
| 116 | + VALUES ($1, $2, $3) |
| 117 | + """, |
| 118 | + task_id, |
| 119 | + "task_work", |
| 120 | + "test-instance", |
| 121 | + ) |
| 122 | + cycle = await db.fetchrow("SELECT * FROM cycle_runs WHERE task_id = $1", task_id) |
| 123 | + assert cycle is not None |
| 124 | + assert cycle["cycle_type"] == "task_work" |
| 125 | + |
| 126 | + with pytest.raises(asyncpg.ForeignKeyViolationError): |
| 127 | + await db.execute( |
| 128 | + """ |
| 129 | + INSERT INTO cycle_runs (task_id, cycle_type, instance_id) |
| 130 | + VALUES ($1, $2, $3) |
| 131 | + """, |
| 132 | + 99999, |
| 133 | + "task_work", |
| 134 | + "test-instance", |
| 135 | + ) |
0 commit comments