Skip to content

refactor: extract test-transaction seam from production session#53

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/extract-test-transaction-seam
Jun 26, 2026
Merged

refactor: extract test-transaction seam from production session#53
lesnik512 merged 1 commit into
mainfrom
refactor/extract-test-transaction-seam

Conversation

@lesnik512

Copy link
Copy Markdown
Member

What

CustomAsyncSession existed for one reason: so the per-test rollback worked. Its close() became a no-op (expunge_all) when the session was bound to a connection — i.e. a test concern leaking into the production data-access module (app/resources/db.py).

This removes it. Test-transaction ownership now lives where it belongs (the db_session fixture), using SQLAlchemy 2.0's native join_transaction_mode="create_savepoint".

Ports modern-python/litestar-sqlalchemy-template#28 to this template.

How

With create_savepoint, each session owns its own savepoint. Advanced-alchemy's auto_commit releases that savepoint while the fixture's outer real transaction survives and is rolled back at the end of each test. This obsoletes both the close() override and the begin_nested() that the old conditional_savepoint default required.

The kwarg is inert in production: it only takes effect when the session is bound to a connection already in a transaction, which production never does (it binds to an engine).

Changes

  • app/resources/db.py — delete CustomAsyncSession; create_session returns a plain AsyncSession(..., join_transaction_mode="create_savepoint") (commented to explain the prod-inert rationale).
  • tests/conftest.py — drop connection.begin_nested(); the fixture's session takes the same mode.
  • CLAUDE.md — update the Database-layer and Tests notes that documented CustomAsyncSession/expunge_all/the nested savepoint.

Verification

  • just test → 19 passed, 100% coverage.
  • just lint (ruff format / ruff check / ty check) clean.

🤖 Generated with Claude Code

CustomAsyncSession existed only so the per-test rollback worked: its
close() became a no-op (expunge_all) when bound to a connection — a test
concern leaking into the production data-access module.

Remove it. Test-transaction ownership now lives in the db_session fixture
via SQLAlchemy 2.0's native join_transaction_mode="create_savepoint". Each
session owns its own savepoint; advanced-alchemy's auto_commit releases it
while the fixture's outer transaction survives and is rolled back per test.
This obsoletes both the close() override and the begin_nested() the old
conditional_savepoint default required.

The kwarg is inert in production: it only takes effect when the session is
bound to a connection already in a transaction, which production never does
(it binds to an engine).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@lesnik512 lesnik512 merged commit fd1a892 into main Jun 26, 2026
2 checks passed
@lesnik512 lesnik512 deleted the refactor/extract-test-transaction-seam branch June 26, 2026 09:35
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