Files
bootnode/api/tests/test_db_session.py
zeekay 1ce1924c41 fix: convert DATABASE_URL to asyncpg scheme for async SQLAlchemy engine
The K8s DATABASE_URL env var uses postgresql:// which makes
create_async_engine try to load psycopg2 (sync driver). Add
_ensure_async_dsn() to convert postgresql:// and postgres:// to
postgresql+asyncpg:// at engine creation time. asyncpg is already
in dependencies.
2026-03-03 08:13:33 -08:00

36 lines
1.3 KiB
Python

"""Tests for database session DSN handling."""
from bootnode.db.session import _ensure_async_dsn
def test_postgresql_converted():
"""Standard postgresql:// is converted to asyncpg."""
dsn = "postgresql://user:pass@host:5432/db"
assert _ensure_async_dsn(dsn) == "postgresql+asyncpg://user:pass@host:5432/db"
def test_postgres_converted():
"""Short-form postgres:// is converted to asyncpg."""
dsn = "postgres://user:pass@host:5432/db"
assert _ensure_async_dsn(dsn) == "postgresql+asyncpg://user:pass@host:5432/db"
def test_asyncpg_unchanged():
"""Already correct DSN is returned as-is."""
dsn = "postgresql+asyncpg://user:pass@host:5432/db"
assert _ensure_async_dsn(dsn) == dsn
def test_production_dsn():
"""Matches the actual K8s DATABASE_URL format."""
dsn = "postgresql://bootnode:[email protected]:5432/bootnode"
expected = "postgresql+asyncpg://bootnode:[email protected]:5432/bootnode"
assert _ensure_async_dsn(dsn) == expected
def test_only_first_occurrence_replaced():
"""Only the scheme prefix is replaced, not embedded substrings."""
dsn = "postgresql://user:postgresql@host:5432/postgresql"
result = _ensure_async_dsn(dsn)
assert result == "postgresql+asyncpg://user:postgresql@host:5432/postgresql"