"""Integration tests for src/my_deepagent/persistence/checkpointer.py. v0.2 PR #1: rewritten for AsyncPostgresSaver (LangGraph Postgres checkpointer). The legacy SqliteSaver / Path-based API is removed. Requires the docker-compose `devflow-postgres` container; the ``pg_db_url`` fixture from ``tests/conftest.py`` creates a fresh DB per test. """ from __future__ import annotations import pytest from my_deepagent.persistence.checkpointer import _to_psycopg_dsn, get_checkpointer_ctx class TestToPsycopgDsn: """Pure-function tests for the SQLAlchemy → libpq DSN converter.""" def test_strips_asyncpg_prefix(self) -> None: url = "postgresql+asyncpg://u:p@h:1/d" assert _to_psycopg_dsn(url) == "postgresql://u:p@h:1/d" def test_strips_psycopg_prefix(self) -> None: url = "postgresql+psycopg://u:p@h:1/d" assert _to_psycopg_dsn(url) == "postgresql://u:p@h:1/d" def test_bare_postgres_url_passes_through(self) -> None: url = "postgresql://u:p@h:1/d" assert _to_psycopg_dsn(url) == url def test_non_postgres_url_passes_through(self) -> None: url = "sqlite:///x" assert _to_psycopg_dsn(url) == url @pytest.mark.integration class TestGetCheckpointerCtx: """Tests for the async get_checkpointer_ctx context manager.""" @pytest.mark.asyncio async def test_ctx_yields_saver(self, pg_db_url: str) -> None: """Entering the async context yields a non-None saver.""" async with get_checkpointer_ctx(pg_db_url) as saver: assert saver is not None @pytest.mark.asyncio async def test_setup_is_idempotent(self, pg_db_url: str) -> None: """``saver.setup()`` is invoked on entry; entering twice must not error.""" async with get_checkpointer_ctx(pg_db_url) as first: assert first is not None # A second open against the same DB must not raise — setup() is idempotent. async with get_checkpointer_ctx(pg_db_url) as second: assert second is not None @pytest.mark.asyncio async def test_accepts_sqlalchemy_url(self, pg_db_url: str) -> None: """SQLAlchemy-style ``postgresql+asyncpg://`` URLs are accepted.""" assert pg_db_url.startswith("postgresql+asyncpg://") async with get_checkpointer_ctx(pg_db_url) as saver: assert saver is not None