mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #33616 -- Allowed registering callbacks that can fail in transaction.on_commit().
Thanks David Wobrock and Mariusz Felisiak for reviews.
This commit is contained in:
committed by
Mariusz Felisiak
parent
be63c78760
commit
4a1150b41d
@@ -2285,6 +2285,32 @@ class CaptureOnCommitCallbacksTests(TestCase):
|
||||
|
||||
self.assertEqual(callbacks, [branch_1, branch_2, leaf_3, leaf_1, leaf_2])
|
||||
|
||||
def test_execute_robust(self):
|
||||
class MyException(Exception):
|
||||
pass
|
||||
|
||||
def hook():
|
||||
self.callback_called = True
|
||||
raise MyException("robust callback")
|
||||
|
||||
with self.assertLogs("django.test", "ERROR") as cm:
|
||||
with self.captureOnCommitCallbacks(execute=True) as callbacks:
|
||||
transaction.on_commit(hook, robust=True)
|
||||
|
||||
self.assertEqual(len(callbacks), 1)
|
||||
self.assertIs(self.callback_called, True)
|
||||
|
||||
log_record = cm.records[0]
|
||||
self.assertEqual(
|
||||
log_record.getMessage(),
|
||||
"Error calling CaptureOnCommitCallbacksTests.test_execute_robust.<locals>."
|
||||
"hook in on_commit() (robust callback).",
|
||||
)
|
||||
self.assertIsNotNone(log_record.exc_info)
|
||||
raised_exception = log_record.exc_info[1]
|
||||
self.assertIsInstance(raised_exception, MyException)
|
||||
self.assertEqual(str(raised_exception), "robust callback")
|
||||
|
||||
|
||||
class DisallowedDatabaseQueriesTests(SimpleTestCase):
|
||||
def test_disallowed_database_connections(self):
|
||||
|
||||
Reference in New Issue
Block a user