"""Neon-style URLs use libpq's sslmode, which asyncpg does not understand.""" import pytest from app.db import normalize_async_url @pytest.mark.parametrize( ("raw", "expected"), [ ( "postgresql+asyncpg://u:p@ep-x.eu-central-1.aws.neon.tech/neondb?sslmode=require", "postgresql+asyncpg://u:p@ep-x.eu-central-1.aws.neon.tech/neondb?ssl=require", ), # Already asyncpg-shaped, or nothing to do. ( "postgresql+asyncpg://u:p@h/db?ssl=require", "postgresql+asyncpg://u:p@h/db?ssl=require", ), ("postgresql+asyncpg://u:p@h:5432/db", "postgresql+asyncpg://u:p@h:5432/db"), # Only the asyncpg driver needs the rewrite. ("postgresql+psycopg://u:p@h/db?sslmode=require", "postgresql+psycopg://u:p@h/db?sslmode=require"), ], ) def test_normalize_async_url(raw: str, expected: str) -> None: assert normalize_async_url(raw) == expected def test_disable_is_preserved() -> None: assert normalize_async_url("postgresql+asyncpg://u:p@h/db?sslmode=disable").endswith("ssl=disable") def test_unix_socket_url_is_untouched() -> None: raw = "postgresql+asyncpg://postgres@/postgres?host=/tmp/pg" assert normalize_async_url(raw) == raw