From a092384180016377c57f6e5a98e8d58dffca51c2 Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Thu, 13 Mar 2025 14:08:47 +0100 Subject: [PATCH] Update unit tests' psycopg to v3 --- test/conftest.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 0c1dff0cf..233a0921e 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -169,9 +169,9 @@ def db_uri(): database_host = os.getenv("DATABASE_HOST", "localhost") if worker_id: - return f"postgresql://nwa:nwa@{database_host}/gso-test-db_{worker_id}" + return f"postgresql+psycopg://nwa:nwa@{database_host}/gso-test-db_{worker_id}" - return os.environ.get("DATABASE_URI_TEST", f"postgresql://nwa:nwa@{database_host}/gso-test-db") + return os.environ.get("DATABASE_URI_TEST", f"postgresql+psycopg://nwa:nwa@{database_host}/gso-test-db") def run_migrations(db_uri: str) -> None: @@ -212,15 +212,14 @@ def _database(db_uri): engine = create_engine(url) with engine.connect() as conn: - conn.execute(text("COMMIT;")) conn.execute( text("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname=:db_name").bindparams( db_name=db_to_create, ), ) - - conn.execute(text(f'DROP DATABASE IF EXISTS "{db_to_create}";')) - conn.execute(text("COMMIT;")) + conn.commit() + conn.execution_options(isolation_level="AUTOCOMMIT").execute(text(f'DROP DATABASE IF EXISTS "{db_to_create}";')) + conn.commit() conn.execute(text(f'CREATE DATABASE "{db_to_create}";')) run_migrations(db_uri) @@ -231,12 +230,13 @@ def _database(db_uri): finally: db.wrapped_database.engine.dispose() with engine.connect() as conn: - conn.execute(text("COMMIT;")) - # Terminate all connections to the database conn.execute( text(f"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='{db_to_create}';"), # noqa: S608 ) - conn.execute(text(f'DROP DATABASE IF EXISTS "{db_to_create}";')) + conn.commit() + conn.execution_options(isolation_level="AUTOCOMMIT").execute( + text(f'DROP DATABASE IF EXISTS "{db_to_create}";') + ) @pytest.fixture(autouse=True) -- GitLab