Repository navigation
test: fail db_session teardown when the outer transaction was closed - #44
Merged
Merged
Conversation
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.
Closes #23
What changed
db_session's teardown intests/conftest.pyused to roll back onlyif connection.in_transaction():. If something had committed the outer test transaction (for example a session built withoutjoin_transaction_mode="create_savepoint"), the rollback was skipped, the test's writes stayed in the database for later tests, and nothing said so.Teardown now checks
transaction.is_activeon the outer transaction itself:pytest.failreports that the outer transaction was committed instead of nesting a savepoint and points atjoin_transaction_mode="create_savepoint". The test shows up as an error in teardown.finally, so they run on the failing path too.I checked
transaction.is_activeinstead of keepingconnection.in_transaction()on purpose. After the outer transaction is committed, any later statement on that connection autobegins a new transaction, soin_transaction()would be true again even though the original writes were already committed. Looking at the transaction object covers that case as well.create_sessionand the production code are unchanged.Test
tests/test_db_session_fixture.pyusespytester(enabled throughpytest_plugins = ["pytester"]in that module) to run an inner pytest session in-process. The inner file imports the realdb_session,di_containerandappfixtures fromtests.conftest, so it runs against the same container database, and coverage of the teardown branch is attributed totests/conftest.py. The inner session:join_transaction_mode="control_fully"session that runs a statement ondb_session.bind;The outer test expects 2 passed and 1 error, plus the failure message.
Test results
conftest.py, new test):1 failed. The inner run reported2 passed,errors: 0where 1 was expected, so teardown was silent.tests/test_db_session_fixture.pygives1 passed.just test:111 passed in 37.53s, coverage 100.00%. No existing test trips the new check, which confirms that nothing in the suite closes the outer transaction.just lint: eof-fixer andruff formatleft all files unchanged, andruff checkandty checkboth reported "All checks passed!".The order-dependent isolation tests in
tests/test_main.pyare #19 and are not touched here.