summaryrefslogtreecommitdiff
path: root/tests/transaction_hooks
diff options
context:
space:
mode:
authorSirAbhi13 <abhinav.sny.2002@gmail.com>2022-06-07 21:20:46 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-09-06 12:21:36 +0200
commit4a1150b41d1312feacd4316b2691227f5a509ae9 (patch)
tree53a97fbac9f66b504c861cea48dba55495e60e50 /tests/transaction_hooks
parentbe63c78760924e1335603c36babd0ad6cfaea3c4 (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/transaction_hooks')
-rw-r--r--tests/transaction_hooks/tests.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/transaction_hooks/tests.py b/tests/transaction_hooks/tests.py
index 75cac5a3e9..938e92575f 100644
--- a/tests/transaction_hooks/tests.py
+++ b/tests/transaction_hooks/tests.py
@@ -43,6 +43,47 @@ class TestConnectionOnCommit(TransactionTestCase):
self.do(1)
self.assertDone([1])
+ def test_robust_if_no_transaction(self):
+ def robust_callback():
+ raise ForcedError("robust callback")
+
+ with self.assertLogs("django.db.backends.base", "ERROR") as cm:
+ transaction.on_commit(robust_callback, robust=True)
+ self.do(1)
+
+ self.assertDone([1])
+ log_record = cm.records[0]
+ self.assertEqual(
+ log_record.getMessage(),
+ "Error calling TestConnectionOnCommit.test_robust_if_no_transaction."
+ "<locals>.robust_callback in on_commit() (robust callback).",
+ )
+ self.assertIsNotNone(log_record.exc_info)
+ raised_exception = log_record.exc_info[1]
+ self.assertIsInstance(raised_exception, ForcedError)
+ self.assertEqual(str(raised_exception), "robust callback")
+
+ def test_robust_transaction(self):
+ def robust_callback():
+ raise ForcedError("robust callback")
+
+ with self.assertLogs("django.db.backends", "ERROR") as cm:
+ with transaction.atomic():
+ transaction.on_commit(robust_callback, robust=True)
+ self.do(1)
+
+ self.assertDone([1])
+ log_record = cm.records[0]
+ self.assertEqual(
+ log_record.getMessage(),
+ "Error calling TestConnectionOnCommit.test_robust_transaction.<locals>."
+ "robust_callback in on_commit() during transaction (robust callback).",
+ )
+ self.assertIsNotNone(log_record.exc_info)
+ raised_exception = log_record.exc_info[1]
+ self.assertIsInstance(raised_exception, ForcedError)
+ self.assertEqual(str(raised_exception), "robust callback")
+
def test_delays_execution_until_after_transaction_commit(self):
with transaction.atomic():
self.do(1)