Fix transactional_db when requested via getfixturevalue - #1318
Open
cpruijsen wants to merge 1 commit into
Open
Conversation
_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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_django_db_helpernow also walks the pytest request parent chain, the same chain pytest uses to know which fixture is currently executing, when choosingTestCasevsTransactionTestCase. Fixtures obtained withrequest.getfixturevalue()(including@pytest.mark.parametrize(..., indirect=True)as in #1157) are not inrequest.fixturenamesuntil they finish, sotransactional_dbalways ran asTestCase.live_serverwas already special-cased for this pattern; user fixtures cannot be.If
django_db_setupis requested but collection found no database aliases, the default alias is set up. A file that only requestsdbdynamically 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")leftconnection.in_atomic_blocktrue.Decision: walk the parent request chain (plus default-alias fallback when
django_db_setupruns with an empty alias set). Alternative: drop_django_db_helperas a declared dependency oftransactional_db, set a node stash flag, thengetfixturevaluethe helper; ifdbis 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_rollbackdiscovery and for requestingtransactional_dbonly 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)
tests/test_database.py,tests/test_db_setup.py,tests/test_fixtures.py