diff options
| author | SirAbhi13 <abhinav.sny.2002@gmail.com> | 2022-06-07 21:20:46 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-09-06 12:21:36 +0200 |
| commit | 4a1150b41d1312feacd4316b2691227f5a509ae9 (patch) | |
| tree | 53a97fbac9f66b504c861cea48dba55495e60e50 /tests/test_utils | |
| parent | be63c78760924e1335603c36babd0ad6cfaea3c4 (diff) | |
Fixed #33616 -- Allowed registering callbacks that can fail in transaction.on_commit().
Thanks David Wobrock and Mariusz Felisiak for reviews.
Diffstat (limited to 'tests/test_utils')
| -rw-r--r-- | tests/test_utils/tests.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index fb19d6e464..709060d76f 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -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): |
