From 3a0adf6699566bc487e4855c9aca14d4027c727e Mon Sep 17 00:00:00 2001 From: diversemix Date: Tue, 9 Apr 2024 09:22:56 +0100 Subject: [PATCH 1/4] Speed up data_db - uses transaction around tests --- tests/conftest.py | 65 ++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8fb19864..b0e597a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ from moto import mock_s3 from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker +from sqlalchemy.engine import Connection from sqlalchemy_utils import create_database, database_exists, drop_database from db_client.models.organisation import AppUser @@ -128,52 +129,42 @@ def test_db(scope="function"): drop_database(test_db_url) -@pytest.fixture -def data_db(scope="function"): - """ - Create a fresh test database for each test. - - This will populate the db using the alembic migrations. - Therefore it is slower but contains data. - - Note: use with `data_client` - - """ +@pytest.fixture(scope="session") +def data_db_connection() -> t.Generator[Connection, None, None]: test_db_url = get_test_db_url() - # Create the test database if database_exists(test_db_url): drop_database(test_db_url) create_database(test_db_url) - # Save DATABASE_URL - saved = os.environ["DATABASE_URL"] + + saved_db_url = os.environ["DATABASE_URL"] os.environ["DATABASE_URL"] = test_db_url - connection = None - test_session = None - try: - test_engine = create_engine(test_db_url) - connection = test_engine.connect() - run_migrations(test_engine) + test_engine = create_engine(test_db_url) - test_session_maker = sessionmaker( - autocommit=False, - autoflush=False, - bind=test_engine, - ) - test_session = test_session_maker() + run_migrations(test_engine) + connection = test_engine.connect() - # Run the tests - yield test_session - finally: - # restore DATABASE_URL - os.environ["DATABASE_URL"] = saved - if test_session is not None: - test_session.close() - if connection is not None: - connection.close() - # Drop the test database - drop_database(test_db_url) + yield connection + connection.close() + + os.environ["DATABASE_URL"] = saved_db_url + drop_database(test_db_url) + + +@pytest.fixture(scope="function") +def data_db(data_db_connection): + transaction = data_db_connection.begin() + + SessionLocal = sessionmaker( + autocommit=False, autoflush=False, bind=data_db_connection + ) + session = SessionLocal() + + yield session + + session.close() + transaction.rollback() @pytest.fixture From b2e9fb7ea675fab454e302b24a7e620e588830c8 Mon Sep 17 00:00:00 2001 From: diversemix Date: Tue, 9 Apr 2024 10:09:22 +0100 Subject: [PATCH 2/4] fix formatting --- tests/conftest.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b0e597a9..234347de 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,17 +5,16 @@ import pytest from cpr_data_access.embedding import Embedder from cpr_data_access.search_adaptors import Vespa, VespaSearchAdapter +from db_client import run_migrations +from db_client.models import Base +from db_client.models.organisation import AppUser from fastapi.testclient import TestClient from moto import mock_s3 from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker from sqlalchemy.engine import Connection +from sqlalchemy.orm import sessionmaker from sqlalchemy_utils import create_database, database_exists, drop_database -from db_client.models.organisation import AppUser -from db_client.models import Base -from db_client import run_migrations - from app.core import security from app.core.aws import S3Client, get_s3_client from app.db.session import get_db From 102a7df89b364b9078699838c8328c90044a20ff Mon Sep 17 00:00:00 2001 From: diversemix Date: Tue, 9 Apr 2024 10:37:17 +0100 Subject: [PATCH 3/4] Fix test relying on id=1 --- tests/routes/test_document_families.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/routes/test_document_families.py b/tests/routes/test_document_families.py index 62ea7402..ac459ec8 100644 --- a/tests/routes/test_document_families.py +++ b/tests/routes/test_document_families.py @@ -51,12 +51,8 @@ def test_physical_doc_languages_not_visible( data_db: Session, ): setup_with_two_docs(data_db) - # setup_with_multiple_docs( - # data_db, doc_data=TWO_DFC_ROW_ONE_LANGUAGE, event_data=TWO_EVENT_ROWS - # ) data_db.execute( update(PhysicalDocumentLanguage) - .where(PhysicalDocumentLanguage.document_id == 1) .values(visible=False) ) From a358d5670b840d84f1b483c467c2280564c73b5d Mon Sep 17 00:00:00 2001 From: diversemix Date: Tue, 9 Apr 2024 11:42:54 +0100 Subject: [PATCH 4/4] update formatting --- tests/routes/test_document_families.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/routes/test_document_families.py b/tests/routes/test_document_families.py index a4c4c7d2..98fd8671 100644 --- a/tests/routes/test_document_families.py +++ b/tests/routes/test_document_families.py @@ -53,10 +53,7 @@ def test_physical_doc_languages_not_visible( data_db: Session, ): setup_with_two_docs(data_db) - data_db.execute( - update(PhysicalDocumentLanguage) - .values(visible=False) - ) + data_db.execute(update(PhysicalDocumentLanguage).values(visible=False)) response = data_client.get( "/api/v1/documents/DocSlug1",