Skip to content

Fix transactional_db when requested via getfixturevalue - #1318

Open
cpruijsen wants to merge 1 commit into
pytest-dev:mainfrom
cpruijsen:fix/issue-1157
Open

Fix transactional_db when requested via getfixturevalue#1318
cpruijsen wants to merge 1 commit into
pytest-dev:mainfrom
cpruijsen:fix/issue-1157

Conversation

@cpruijsen

Copy link
Copy Markdown

Summary

_django_db_helper now also walks the pytest request parent chain, the same chain pytest uses to know which fixture is currently executing, when choosing TestCase vs TransactionTestCase. Fixtures obtained with request.getfixturevalue() (including @pytest.mark.parametrize(..., indirect=True) as in #1157) are not in request.fixturenames until they finish, so transactional_db always ran as TestCase. live_server was already special-cased for this pattern; user fixtures cannot be.

If django_db_setup is requested but collection found no database aliases, the default alias is set up. A file that only requests db dynamically no longer fails with "unable to open database file" (the v4.11 static-analysis gap for the default database).

Provenance: the reporter's example in
#1157 (no comments).
Reproduced on this tree: getfixturevalue("transactional_db") left
connection.in_atomic_block true.

Decision: walk the parent request chain (plus default-alias fallback when django_db_setup runs with an empty alias set). Alternative: drop _django_db_helper as a declared dependency of transactional_db, set a node stash flag, then getfixturevalue the helper; if db is also requested statically, the helper may already have run before the fixture body, and the parent chain is visible while the helper is still a dependency. Can switch to the stash design, or add a collection-time parametrize heuristic so these tests sort with other transactional tests (they sort as non-db). The v4.11 note still applies for multi-db / serialized_rollback discovery and for requesting transactional_db only after the helper has already been cached.

Test plan

  • tests/test_database.py::TestIndirectDbParametrization (reporter pattern)
  • tests/test_database.py::TestDatabaseFixtures::test_transactions_match_dynamic_db_fixture
    (22 / 23 for db, transactional_db, reset_sequences,
    serialized_rollback)
  • full tests/test_database.py, tests/test_db_setup.py, tests/test_fixtures.py
  • same new tests on pytest 7.0.1 (plugin lower bound)

_django_db_helper decided TestCase vs TransactionTestCase from
request.fixturenames, which does not include fixtures requested via
getfixturevalue() until they finish. Walk the parent request chain so
indirect parametrization of db vs transactional_db uses the right wrapper.

Closes pytest-dev#1157
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant