77import pytest
88from django import db as django_db
99from django .db import transaction as django_transaction
10+ from django .test import override_settings
1011
1112from django_subatomic import db , test
1213
@@ -59,32 +60,6 @@ def _parametrize_transaction_testcase(func: Callable[..., None]) -> MarkDecorato
5960 return parametrize (usefixtures (func )) # type: ignore[no-any-return]
6061
6162
62- @pytest .fixture (autouse = True )
63- def _require_transactions_for_after_commit_callbacks (
64- request : pytest .FixtureRequest ,
65- ) -> Generator [None ]:
66- """
67- Ensure after-commit callbacks are executed from within a transaction.
68-
69- This ensures that we do not accidentally write code which looks like it defers execution,
70- but is actually running immediately.
71-
72- This fixture enables that new behaviour for all new tests by default,
73- but offers an escape-hatch for tests which have not yet been fixed.
74-
75- TODO: Allow users of this library to do this in their own tests.
76-
77- See Note [After-commit callbacks require a transaction].
78- """
79- if "deprecated_run_after_commit_outside_transaction" in request .keywords :
80- with mock .patch .object (
81- db , "_REQUIRE_TRANSACTION_FOR_AFTER_COMMIT_CALLBACKS" , new = False
82- ):
83- yield
84- else :
85- yield
86-
87-
8863@pytest .fixture (autouse = True )
8964def _prevent_after_commit_callbacks (
9065 request : pytest .FixtureRequest ,
@@ -387,6 +362,10 @@ def test_unclosed_manual_transaction(self, db_name: str) -> None:
387362
388363
389364class TestRunAfterCommit :
365+ """
366+ Tests for `run_after_commit`.
367+ """
368+
390369 @_parametrize_transaction_testcase
391370 def test_outside_transaction_error (self ) -> None :
392371 """
@@ -403,18 +382,16 @@ def test_outside_transaction_error(self) -> None:
403382 assert counter .count == 0
404383
405384 @_parametrize_transaction_testcase
406- @pytest .mark .deprecated_run_after_commit_outside_transaction
407- def test_executes_immediately (self ) -> None :
385+ def test_transaction_check_can_be_disabled (self ) -> None :
408386 """
409- `run_after_commit` executes the callback immediately if there is no transaction...
410-
411- ... and if the test is marked with `deprecated_run_after_commit_outside_transaction`.
387+ Callbacks run immediately if there is no transaction and `SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION` is False.
412388
413389 See Note [After-commit callbacks require a transaction]
414390 """
415391 counter = Counter ()
416392
417- db .run_after_commit (counter .increment )
393+ with override_settings (SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION = False ):
394+ db .run_after_commit (counter .increment )
418395
419396 assert counter .count == 1
420397
@@ -487,29 +464,16 @@ def test_not_executed_if_rolled_back(self) -> None:
487464
488465
489466class TestRunAfterCommitDeprecatedTestBehaviour :
490- @pytest .mark .deprecated_run_after_commit_outside_transaction
491- def test_does_not_raise_error_when_marked (self ) -> None :
492- """
493- No error is raised in test marked with `deprecated_run_after_commit_outside_transaction`.
494-
495- Note: this would usually fail because we're enqueuing an after-commit callback without a
496- transaction.
497- """
498- try :
499- db .run_after_commit (object )
500- except Exception : # noqa: BLE001
501- pytest .fail ("An error should not have been raised." )
502-
503467 @pytest .mark .deprecated_ignore_after_commit_callbacks
504- @pytest . mark . deprecated_run_after_commit_outside_transaction
468+ @override_settings ( SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION = False )
505469 def test_does_not_execute_when_in_testcase_transaction_if_callbacks_disabled (
506470 self ,
507471 ) -> None :
508472 """
509473 `run_after_commit` should not ignore testcase transactions when test marked with `deprecated_ignore_after_commit_callbacks`.
510474
511- Note: this test also requires `deprecated_run_after_commit_outside_transaction`,
512- because otherwise an error would be raised when trying to run the callback.
475+ We have to disable `SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION`, because
476+ otherwise an error would be raised when trying to register the callback.
513477 """
514478 counter = Counter ()
515479
0 commit comments