"""Tests for create_database_engine URL normalization. Bare `postgres://` and legacy `postgres+pq://` schemes must be normalized to the explicit `postgresql+psycopg://` dialect, otherwise SQLAlchemy raises "NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres" even when psycopg is installed. """ from app.db import create_database_engine def test_normalizes_bare_postgres_scheme(): engine = create_database_engine("postgres://user:pass@db.example.com:5432/mydb") try: assert str(engine.url).startswith("postgresql+psycopg://") assert "user:***@db.example.com:5432/mydb" in str(engine.url) finally: engine.dispose() def test_normalizes_legacy_psycopg2_scheme(): engine = create_database_engine("postgres+pq://user:pass@localhost:5432/db") try: assert str(engine.url).startswith("postgresql+psycopg://") finally: engine.dispose() def test_leaves_explicit_psycopg_scheme_unchanged(): engine = create_database_engine("postgresql+psycopg://user:pass@localhost:5432/db") try: assert str(engine.url).startswith("postgresql+psycopg://") finally: engine.dispose() def test_leaves_sqlite_unchanged(): engine = create_database_engine("sqlite:////tmp/db-test.sqlite") try: assert str(engine.url).startswith("sqlite:") finally: engine.dispose()