| 1 |
from django.db import connection, transaction |
| 2 |
from django.test import TransactionTestCase, skipUnlessDBFeature |
| 3 |
|
| 4 |
from .models import Thing |
| 5 |
|
| 6 |
|
| 7 |
class ForcedError(Exception): |
| 8 |
pass |
| 9 |
|
| 10 |
|
| 11 |
@skipUnlessDBFeature("supports_transactions") |
| 12 |
class TestConnectionOnCommit(TransactionTestCase): |
| 13 |
""" |
| 14 |
Tests for transaction.on_commit(). |
| 15 |
|
| 16 |
Creation/checking of database objects in parallel with callback tracking is |
| 17 |
to verify that the behavior of the two match in all tested cases. |
| 18 |
""" |
| 19 |
|
| 20 |
available_apps = ["transaction_hooks"] |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
self.notified = [] |
| 24 |
|
| 25 |
def notify(self, id_): |
| 26 |
if id_ == "error": |
| 27 |
raise ForcedError() |
| 28 |
self.notified.append(id_) |
| 29 |
|
| 30 |
def do(self, num): |
| 31 |
"""Create a Thing instance and notify about it.""" |
| 32 |
Thing.objects.create(num=num) |
| 33 |
transaction.on_commit(lambda: self.notify(num)) |
| 34 |
|
| 35 |
def assertDone(self, nums): |
| 36 |
self.assertNotified(nums) |
| 37 |
self.assertEqual(sorted(t.num for t in Thing.objects.all()), sorted(nums)) |
| 38 |
|
| 39 |
def assertNotified(self, nums): |
| 40 |
self.assertEqual(self.notified, nums) |
| 41 |
|
| 42 |
def test_executes_immediately_if_no_transaction(self): |
| 43 |
self.do(1) |
| 44 |
self.assertDone([1]) |
| 45 |
|
| 46 |
def test_robust_if_no_transaction(self): |
| 47 |
def robust_callback(): |
| 48 |
raise ForcedError("robust callback") |
| 49 |
|
| 50 |
with self.assertLogs("django.db.backends.base", "ERROR") as cm: |
| 51 |
transaction.on_commit(robust_callback, robust=True) |
| 52 |
self.do(1) |
| 53 |
|
| 54 |
self.assertDone([1]) |
| 55 |
log_record = cm.records[0] |
| 56 |
self.assertEqual( |
| 57 |
log_record.getMessage(), |
| 58 |
"Error calling TestConnectionOnCommit.test_robust_if_no_transaction." |
| 59 |
"<locals>.robust_callback in on_commit() (robust callback).", |
| 60 |
) |
| 61 |
self.assertIsNotNone(log_record.exc_info) |
| 62 |
raised_exception = log_record.exc_info[1] |
| 63 |
self.assertIsInstance(raised_exception, ForcedError) |
| 64 |
self.assertEqual(str(raised_exception), "robust callback") |
| 65 |
|
| 66 |
def test_robust_transaction(self): |
| 67 |
def robust_callback(): |
| 68 |
raise ForcedError("robust callback") |
| 69 |
|
| 70 |
with self.assertLogs("django.db.backends", "ERROR") as cm: |
| 71 |
with transaction.atomic(): |
| 72 |
transaction.on_commit(robust_callback, robust=True) |
| 73 |
self.do(1) |
| 74 |
|
| 75 |
self.assertDone([1]) |
| 76 |
log_record = cm.records[0] |
| 77 |
self.assertEqual( |
| 78 |
log_record.getMessage(), |
| 79 |
"Error calling TestConnectionOnCommit.test_robust_transaction.<locals>." |
| 80 |
"robust_callback in on_commit() during transaction (robust callback).", |
| 81 |
) |
| 82 |
self.assertIsNotNone(log_record.exc_info) |
| 83 |
raised_exception = log_record.exc_info[1] |
| 84 |
self.assertIsInstance(raised_exception, ForcedError) |
| 85 |
self.assertEqual(str(raised_exception), "robust callback") |
| 86 |
|
| 87 |
def test_delays_execution_until_after_transaction_commit(self): |
| 88 |
with transaction.atomic(): |
| 89 |
self.do(1) |
| 90 |
self.assertNotified([]) |
| 91 |
self.assertDone([1]) |
| 92 |
|
| 93 |
def test_does_not_execute_if_transaction_rolled_back(self): |
| 94 |
try: |
| 95 |
with transaction.atomic(): |
| 96 |
self.do(1) |
| 97 |
raise ForcedError() |
| 98 |
except ForcedError: |
| 99 |
pass |
| 100 |
|
| 101 |
self.assertDone([]) |
| 102 |
|
| 103 |
def test_executes_only_after_final_transaction_committed(self): |
| 104 |
with transaction.atomic(): |
| 105 |
with transaction.atomic(): |
| 106 |
self.do(1) |
| 107 |
self.assertNotified([]) |
| 108 |
self.assertNotified([]) |
| 109 |
self.assertDone([1]) |
| 110 |
|
| 111 |
def test_discards_hooks_from_rolled_back_savepoint(self): |
| 112 |
with transaction.atomic(): |
| 113 |
# one successful savepoint |
| 114 |
with transaction.atomic(): |
| 115 |
self.do(1) |
| 116 |
# one failed savepoint |
| 117 |
try: |
| 118 |
with transaction.atomic(): |
| 119 |
self.do(2) |
| 120 |
raise ForcedError() |
| 121 |
except ForcedError: |
| 122 |
pass |
| 123 |
# another successful savepoint |
| 124 |
with transaction.atomic(): |
| 125 |
self.do(3) |
| 126 |
|
| 127 |
# only hooks registered during successful savepoints execute |
| 128 |
self.assertDone([1, 3]) |
| 129 |
|
| 130 |
def test_no_hooks_run_from_failed_transaction(self): |
| 131 |
"""If outer transaction fails, no hooks from within it run.""" |
| 132 |
try: |
| 133 |
with transaction.atomic(): |
| 134 |
with transaction.atomic(): |
| 135 |
self.do(1) |
| 136 |
raise ForcedError() |
| 137 |
except ForcedError: |
| 138 |
pass |
| 139 |
|
| 140 |
self.assertDone([]) |
| 141 |
|
| 142 |
def test_inner_savepoint_rolled_back_with_outer(self): |
| 143 |
with transaction.atomic(): |
| 144 |
try: |
| 145 |
with transaction.atomic(): |
| 146 |
with transaction.atomic(): |
| 147 |
self.do(1) |
| 148 |
raise ForcedError() |
| 149 |
except ForcedError: |
| 150 |
pass |
| 151 |
self.do(2) |
| 152 |
|
| 153 |
self.assertDone([2]) |
| 154 |
|
| 155 |
def test_no_savepoints_atomic_merged_with_outer(self): |
| 156 |
with transaction.atomic(): |
| 157 |
with transaction.atomic(): |
| 158 |
self.do(1) |
| 159 |
try: |
| 160 |
with transaction.atomic(savepoint=False): |
| 161 |
raise ForcedError() |
| 162 |
except ForcedError: |
| 163 |
pass |
| 164 |
|
| 165 |
self.assertDone([]) |
| 166 |
|
| 167 |
def test_inner_savepoint_does_not_affect_outer(self): |
| 168 |
with transaction.atomic(): |
| 169 |
with transaction.atomic(): |
| 170 |
self.do(1) |
| 171 |
try: |
| 172 |
with transaction.atomic(): |
| 173 |
raise ForcedError() |
| 174 |
except ForcedError: |
| 175 |
pass |
| 176 |
|
| 177 |
self.assertDone([1]) |
| 178 |
|
| 179 |
def test_runs_hooks_in_order_registered(self): |
| 180 |
with transaction.atomic(): |
| 181 |
self.do(1) |
| 182 |
with transaction.atomic(): |
| 183 |
self.do(2) |
| 184 |
self.do(3) |
| 185 |
|
| 186 |
self.assertDone([1, 2, 3]) |
| 187 |
|
| 188 |
def test_hooks_cleared_after_successful_commit(self): |
| 189 |
with transaction.atomic(): |
| 190 |
self.do(1) |
| 191 |
with transaction.atomic(): |
| 192 |
self.do(2) |
| 193 |
|
| 194 |
self.assertDone([1, 2]) # not [1, 1, 2] |
| 195 |
|
| 196 |
def test_hooks_cleared_after_rollback(self): |
| 197 |
try: |
| 198 |
with transaction.atomic(): |
| 199 |
self.do(1) |
| 200 |
raise ForcedError() |
| 201 |
except ForcedError: |
| 202 |
pass |
| 203 |
|
| 204 |
with transaction.atomic(): |
| 205 |
self.do(2) |
| 206 |
|
| 207 |
self.assertDone([2]) |
| 208 |
|
| 209 |
@skipUnlessDBFeature("test_db_allows_multiple_connections") |
| 210 |
def test_hooks_cleared_on_reconnect(self): |
| 211 |
with transaction.atomic(): |
| 212 |
self.do(1) |
| 213 |
connection.close() |
| 214 |
|
| 215 |
connection.connect() |
| 216 |
|
| 217 |
with transaction.atomic(): |
| 218 |
self.do(2) |
| 219 |
|
| 220 |
self.assertDone([2]) |
| 221 |
|
| 222 |
def test_error_in_hook_doesnt_prevent_clearing_hooks(self): |
| 223 |
try: |
| 224 |
with transaction.atomic(): |
| 225 |
transaction.on_commit(lambda: self.notify("error")) |
| 226 |
except ForcedError: |
| 227 |
pass |
| 228 |
|
| 229 |
with transaction.atomic(): |
| 230 |
self.do(1) |
| 231 |
|
| 232 |
self.assertDone([1]) |
| 233 |
|
| 234 |
def test_db_query_in_hook(self): |
| 235 |
with transaction.atomic(): |
| 236 |
Thing.objects.create(num=1) |
| 237 |
transaction.on_commit( |
| 238 |
lambda: [self.notify(t.num) for t in Thing.objects.all()] |
| 239 |
) |
| 240 |
|
| 241 |
self.assertDone([1]) |
| 242 |
|
| 243 |
def test_transaction_in_hook(self): |
| 244 |
def on_commit(): |
| 245 |
with transaction.atomic(): |
| 246 |
t = Thing.objects.create(num=1) |
| 247 |
self.notify(t.num) |
| 248 |
|
| 249 |
with transaction.atomic(): |
| 250 |
transaction.on_commit(on_commit) |
| 251 |
|
| 252 |
self.assertDone([1]) |
| 253 |
|
| 254 |
def test_hook_in_hook(self): |
| 255 |
def on_commit(i, add_hook): |
| 256 |
with transaction.atomic(): |
| 257 |
if add_hook: |
| 258 |
transaction.on_commit(lambda: on_commit(i + 10, False)) |
| 259 |
t = Thing.objects.create(num=i) |
| 260 |
self.notify(t.num) |
| 261 |
|
| 262 |
with transaction.atomic(): |
| 263 |
transaction.on_commit(lambda: on_commit(1, True)) |
| 264 |
transaction.on_commit(lambda: on_commit(2, True)) |
| 265 |
|
| 266 |
self.assertDone([1, 11, 2, 12]) |
| 267 |
|
| 268 |
def test_raises_exception_non_autocommit_mode(self): |
| 269 |
def should_never_be_called(): |
| 270 |
raise AssertionError("this function should never be called") |
| 271 |
|
| 272 |
try: |
| 273 |
connection.set_autocommit(False) |
| 274 |
msg = "on_commit() cannot be used in manual transaction management" |
| 275 |
with self.assertRaisesMessage(transaction.TransactionManagementError, msg): |
| 276 |
transaction.on_commit(should_never_be_called) |
| 277 |
finally: |
| 278 |
connection.set_autocommit(True) |
| 279 |
|
| 280 |
def test_raises_exception_non_callable(self): |
| 281 |
msg = "on_commit()'s callback must be a callable." |
| 282 |
with self.assertRaisesMessage(TypeError, msg): |
| 283 |
transaction.on_commit(None) |