feat(agent): agentdb bootstrap, migration runner, v1 schema
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import itertools
|
||||
|
||||
import pytest
|
||||
from testcontainers.postgres import PostgresContainer
|
||||
|
||||
from agent.config import AgentConfig
|
||||
|
||||
_db_counter = itertools.count()
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def pg():
|
||||
with PostgresContainer("postgres:16-alpine") as container:
|
||||
yield container
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cfg(pg) -> AgentConfig:
|
||||
"""AgentConfig pointing at a fresh, uniquely named database per test."""
|
||||
return AgentConfig(
|
||||
db_host=pg.get_container_host_ip(),
|
||||
db_port=int(pg.get_exposed_port(5432)),
|
||||
db_user=pg.username,
|
||||
db_password=pg.password,
|
||||
agent_db_name=f"agentdb_test_{next(_db_counter)}",
|
||||
firefly_url="http://firefly.test",
|
||||
firefly_token="test-token",
|
||||
heartbeat_interval_minutes=60,
|
||||
)
|
||||
@@ -0,0 +1,39 @@
|
||||
from agent.db import connect, ensure_database, run_migrations
|
||||
|
||||
EXPECTED_TABLES = {"schema_migrations", "sync_runs", "documents", "bills", "approvals"}
|
||||
|
||||
|
||||
def test_ensure_database_creates_and_is_idempotent(cfg):
|
||||
ensure_database(cfg)
|
||||
ensure_database(cfg) # second call must not raise
|
||||
with connect(cfg) as conn:
|
||||
assert conn.execute("SELECT 1").fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_run_migrations_applies_each_migration_exactly_once(cfg):
|
||||
ensure_database(cfg)
|
||||
|
||||
first = run_migrations(cfg)
|
||||
assert first == ["0001_initial.sql"]
|
||||
|
||||
second = run_migrations(cfg)
|
||||
assert second == []
|
||||
|
||||
with connect(cfg) as conn:
|
||||
rows = conn.execute(
|
||||
"SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
|
||||
).fetchall()
|
||||
assert EXPECTED_TABLES <= {r[0] for r in rows}
|
||||
|
||||
|
||||
def test_sync_runs_rejects_bad_status(cfg):
|
||||
import psycopg
|
||||
import pytest
|
||||
|
||||
ensure_database(cfg)
|
||||
run_migrations(cfg)
|
||||
with connect(cfg) as conn, pytest.raises(psycopg.errors.CheckViolation):
|
||||
conn.execute(
|
||||
"INSERT INTO sync_runs (connector, started_at, status)"
|
||||
" VALUES ('x', now(), 'bogus')"
|
||||
)
|
||||
Reference in New Issue
Block a user