Skip to content

Tests using the same database as pytest_alembic are failing on 0.10.7 #91

@thurse93

Description

@thurse93

Hi!

we are currently trying to upgrade from version 0.10.6 to 0.10.7 in our project. Unfortunately we are experiencing some problems, since the pytest_alembic tests seem to produce side effects between tests which are not getting rolled back in the new version.

An excerpt of our migration test module:

import pytest
from pytest_alembic import MigrationContext, tests
from sqlalchemy import Engine


@pytest.fixture()
def alembic_engine(our_custom_engine_fixture: Engine) -> Engine:
    return our_custom_engine_fixture


@pytest.fixture()
def alembic_config() -> dict:
    return {
        "at_revision_data": {...}
    }


def test_single_head_revision(alembic_runner: MigrationContext) -> None:
    tests.test_single_head_revision(alembic_runner)


def test_upgrade(alembic_runner: MigrationContext) -> None:
    tests.test_upgrade(alembic_runner)


def test_model_definitions_match_ddl(alembic_runner: MigrationContext) -> None:
    tests.test_model_definitions_match_ddl(alembic_runner)


def test_up_down_consistency(alembic_runner: MigrationContext) -> None:
    tests.test_up_down_consistency(alembic_runner)

We are using our own database engine throughout the entire test session, the changes are getting rolled back after each test. Before version 0.10.7 pytest_alembic rolled the database back by itself. Now our own tests are failing because there are unexpected tables in the database. We assume thats because of the following change in 0.10.7, where connection.connect() is replaced by connection.begin():

v0.10.6...v0.10.7#diff-0108f216ff6e1670b0a351075109c35eb9af378fc93b0442c06a0dd1232607b5L183-R186

We found a workaround for our use case which simply drops the alembic_version table and all our schemas after the migration test module ran:

@pytest.fixture(autouse=True, scope="module")
def _teardown_tables(our_custom_engine_fixture: Engine) -> None:
    yield
    with our_custom_engine_fixture.connect() as connection:
        Base.metadata.drop_all(bind=connection)
        connection.execute(text("DROP TABLE IF EXISTS alembic_version"))
        connection.commit()

Question is: Is this the intended behaviour of pytest_alembic?
We are aware that our setup with a single database for the entire pytest session is a bit of a special case.

Best regards and thank you for your work providing this nice lib!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions