Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

- Added MariaDB and SQLite to the test matrix.
- Disallowed nesting of `part_of_a_transaction` to prevent accidental
simulation of nested transaction behaviour in tests. Fixes #150.

## [1.0.0] - 2026-04-16

Expand Down
2 changes: 1 addition & 1 deletion src/django_subatomic/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def part_of_a_transaction(using: str | None = None) -> Generator[None]:
Note that this does not handle after-commit callback simulation. If you need that,
use [`transaction`][django_subatomic.db.transaction] instead.
"""
with transaction.atomic(using=using):
with transaction.atomic(using=using, durable=True):
yield
44 changes: 44 additions & 0 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,50 @@ def test_creates_savepoint(
assert release_savepoint["sql"].startswith("RELEASE SAVEPOINT ")


class TestPartOfATransaction:
Comment thread
meshy marked this conversation as resolved.
@pytest.mark.django_db(transaction=True)
Comment thread
meshy marked this conversation as resolved.
def test_fails_when_nested_inside_another_part_of_a_transaction(self) -> None:
"""
`part_of_a_transaction` cannot be nested inside another `part_of_a_transaction`.
"""
with pytest.raises(
RuntimeError,
match=re.escape(
"A durable atomic block cannot be nested within another atomic block."
),
):
with test.part_of_a_transaction():
with test.part_of_a_transaction():
...
Comment thread
meshy marked this conversation as resolved.

@pytest.mark.django_db(transaction=True)
def test_fails_when_nested_inside_an_atomic_block(self) -> None:
"""
`part_of_a_transaction` cannot be nested inside an existing atomic block.
"""
with pytest.raises(
RuntimeError,
match=re.escape(
"A durable atomic block cannot be nested within another atomic block."
),
):
with django_transaction.atomic():
Comment thread
meshy marked this conversation as resolved.
with test.part_of_a_transaction():
...

@pytest.mark.django_db(transaction=True)
def test_creates_transaction(self) -> None:
"""
A transaction is active within the context manager.
"""
assert django_transaction.get_autocommit() is True
Comment thread
meshy marked this conversation as resolved.

with test.part_of_a_transaction():
assert django_transaction.get_autocommit() is False

assert django_transaction.get_autocommit() is True


class TestTransactionIfNotAlready:
def test_transaction_already_exists(
self, django_assert_num_queries: pytest_django.DjangoAssertNumQueries
Expand Down
Loading