1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #30457 -- Added TestCase.captureOnCommitCallbacks().

This commit is contained in:
Adam Johnson
2020-05-20 11:04:36 +01:00
committed by Mariusz Felisiak
parent ca6c5e5fc2
commit e906ff6fca
5 changed files with 137 additions and 4 deletions

View File

@@ -1229,6 +1229,21 @@ class TestCase(TransactionTestCase):
not connection.needs_rollback and connection.is_usable()
)
@classmethod
@contextmanager
def captureOnCommitCallbacks(cls, *, using=DEFAULT_DB_ALIAS, execute=False):
"""Context manager to capture transaction.on_commit() callbacks."""
callbacks = []
start_count = len(connections[using].run_on_commit)
try:
yield callbacks
finally:
run_on_commit = connections[using].run_on_commit[start_count:]
callbacks[:] = [func for sids, func in run_on_commit]
if execute:
for callback in callbacks:
callback()
class CheckCondition:
"""Descriptor class for deferred condition checking."""