Repository navigation
test: prove db_session rollback with one self-contained test - #45
Merged
Merged
Conversation
lesnik512
force-pushed
the
test/self-contained-rollback-proof
branch
from
October 4, 2026 10:06
4281a20 to
391b2db
Compare
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 #19
What changed
tests/conftest.py: the body of thedb_sessionfixture moved unchanged into an async context manager,rolled_back_db_session(di_container).db_sessionnow just enters it and yields the session, so every other test gets the same setup and teardown as before. Theif connection.in_transaction():guard is untouched (that's Per-test rollback is fail-silent on an unexpected commit #23). The lines were moved without edits so the conflict with Per-test rollback is fail-silent on an unexpected commit #23 stays small.tests/test_main.py: removed the order-dependent pairtest_db_session_insert_is_visible_within_testandtest_db_session_rolls_back_between_tests. They're replaced by one test,test_db_session_teardown_rolls_back_committed_writes. It writes a user throughrolled_back_db_sessionand callscommit()(not justflush). It then checks the row is visible inside the block, so the test can't pass on a table nothing wrote to. After the context manager's teardown runs, it queries from a fresh engine and connection and expects no row.Why
test_db_session_rolls_back_between_testsonly proved anything when it ran after its partner. Run on its own with-k, it passed against an empty table that nothing had written to. The new test does the write, the teardown and the check itself, so it doesn't depend on test order.About the acceptance criterion
The brief said the test must fail "if the teardown rollback is removed". As worded, no test can meet that. The next line of teardown,
await connection.close(), rolls back any open transaction anyway, because SQLAlchemy resets the connection when it goes back to the pool. Deleting the explicitawait transaction.rollback()therefore changes nothing a test can see. I tried it, and the new test still passes. What the criterion meant was: the test fails if teardown lets the outer transaction persist.There's no red-before-green here either. The fixture already rolls back correctly, so the new test passes the moment it's written. The evidence that it can catch a leak comes from the mutations below.
Test results
All runs use
just test. The subset runs add--no-cov, because the 100% coverage gate fails on any subset.test_db_session_rolls_back_between_testsalone with-k: 1 passed, against a table nothing had written to.-k teardown_rolls_back): 1 passed.close()rolls back.transaction.rollback()changed totransaction.commit(): new test fails alone withassert [1] == [].just test: 109 passed, coverage 100.00%. That's 110 before, minus the 2 removed tests, plus the 1 new one.just lint: eof-fixer, ruff format, ruff check and ty check all clean, no auto-fixes.