22
33import re
44from typing import TYPE_CHECKING
5- from unittest import mock
65
76import pytest
87from django import db as django_db
1413
1514if TYPE_CHECKING :
1615 import contextlib
17- from collections .abc import Callable , Generator
16+ from collections .abc import Callable
1817 from typing import Protocol
1918
2019 import pytest_django
@@ -60,28 +59,6 @@ def _parametrize_transaction_testcase(func: Callable[..., None]) -> MarkDecorato
6059 return parametrize (usefixtures (func )) # type: ignore[no-any-return]
6160
6261
63- @pytest .fixture (autouse = True )
64- def _prevent_after_commit_callbacks (
65- request : pytest .FixtureRequest ,
66- ) -> Generator [None , None , None ]:
67- """
68- Disable automatically running after-commit callbacks in marked tests.
69-
70- If a test relies on after-commit callbacks never being called, it may be
71- marked with `deprecated_ignore_after_commit_callbacks` to disable test simulation
72- of how after-commit callbacks are naturally run after transactions.
73-
74- TODO: Allow users of this library to do this in their own tests.
75-
76- See Note [Running after-commit callbacks in tests]
77- """
78- if "deprecated_ignore_after_commit_callbacks" in request .keywords :
79- with mock .patch .object (db , "_RUN_AFTER_COMMIT_CALLBACKS_IN_TESTS" , new = False ):
80- yield
81- else :
82- yield
83-
84-
8562class Counter :
8663 def __init__ (self ) -> None :
8764 self .count = 0
@@ -463,25 +440,24 @@ def test_not_executed_if_rolled_back(self) -> None:
463440 assert counter .count == 0
464441
465442
466- class TestRunAfterCommitDeprecatedTestBehaviour :
467- @pytest .mark .deprecated_ignore_after_commit_callbacks
443+ class TestRunAfterCommitCallbacksSettingBehaviour :
468444 @override_settings (SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION = False )
469445 def test_does_not_execute_when_in_testcase_transaction_if_callbacks_disabled (
470446 self ,
471447 ) -> None :
472448 """
473- `run_after_commit` should not ignore testcase transactions when test marked with `deprecated_ignore_after_commit_callbacks` .
449+ `run_after_commit` should not ignore testcase transactions when SUBATOMIC_RUN_AFTER_COMMIT_CALLBACKS_IN_TESTS is False .
474450
475451 We have to disable `SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION`, because
476452 otherwise an error would be raised when trying to register the callback.
477453 """
478454 counter = Counter ()
479455
480- db .run_after_commit (counter .increment )
456+ with override_settings (SUBATOMIC_RUN_AFTER_COMMIT_CALLBACKS_IN_TESTS = False ):
457+ db .run_after_commit (counter .increment )
481458
482459 assert counter .count == 0
483460
484- @pytest .mark .deprecated_ignore_after_commit_callbacks
485461 @pytest .mark .parametrize (
486462 "transaction_context" ,
487463 (
@@ -493,16 +469,17 @@ def test_does_not_execute_after_commit_if_decorated(
493469 self , transaction_context : DBContextManager
494470 ) -> None :
495471 """
496- `run_after_commit` should not execute the callback when test marked with `deprecated_ignore_after_commit_callbacks` .
472+ `run_after_commit` should not execute the callback when SUBATOMIC_RUN_AFTER_COMMIT_CALLBACKS_IN_TESTS is False .
497473 """
498474 counter = Counter ()
499475
500- with transaction_context ():
501- db .run_after_commit (counter .increment )
476+ with override_settings (SUBATOMIC_RUN_AFTER_COMMIT_CALLBACKS_IN_TESTS = False ):
477+ with transaction_context ():
478+ db .run_after_commit (counter .increment )
502479
503- assert counter .count == 0
480+ assert counter .count == 0
504481
505- assert counter .count == 0
482+ assert counter .count == 0
506483
507484
508485class TestInTransaction :
0 commit comments