Debian Codegraph @

tests/transactions/tests.py

raw
1 import sys
2 import threading
3 import time
4 from unittest import skipIf, skipUnless
5
6 from django.db import (
7 DatabaseError,
8 Error,
9 IntegrityError,
10 OperationalError,
11 connection,
12 transaction,
13 )
14 from django.test import (
15 TestCase,
16 TransactionTestCase,
17 skipIfDBFeature,
18 skipUnlessDBFeature,
19 )
20
21 from .models import Reporter
22
23
24 @skipUnlessDBFeature("uses_savepoints")
25 class AtomicTests(TransactionTestCase):
26 """
27 Tests for the atomic decorator and context manager.
28
29 The tests make assertions on internal attributes because there isn't a
30 robust way to ask the database for its current transaction state.
31
32 Since the decorator syntax is converted into a context manager (see the
33 implementation), there are only a few basic tests with the decorator
34 syntax and the bulk of the tests use the context manager syntax.
35 """
36
37 available_apps = ["transactions"]
38
39 def test_decorator_syntax_commit(self):
40 @transaction.atomic
41 def make_reporter():
42 return Reporter.objects.create(first_name="Tintin")
43
44 reporter = make_reporter()
45 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
46
47 def test_decorator_syntax_rollback(self):
48 @transaction.atomic
49 def make_reporter():
50 Reporter.objects.create(first_name="Haddock")
51 raise Exception("Oops, that's his last name")
52
53 with self.assertRaisesMessage(Exception, "Oops"):
54 make_reporter()
55 self.assertSequenceEqual(Reporter.objects.all(), [])
56
57 def test_alternate_decorator_syntax_commit(self):
58 @transaction.atomic()
59 def make_reporter():
60 return Reporter.objects.create(first_name="Tintin")
61
62 reporter = make_reporter()
63 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
64
65 def test_alternate_decorator_syntax_rollback(self):
66 @transaction.atomic()
67 def make_reporter():
68 Reporter.objects.create(first_name="Haddock")
69 raise Exception("Oops, that's his last name")
70
71 with self.assertRaisesMessage(Exception, "Oops"):
72 make_reporter()
73 self.assertSequenceEqual(Reporter.objects.all(), [])
74
75 def test_commit(self):
76 with transaction.atomic():
77 reporter = Reporter.objects.create(first_name="Tintin")
78 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
79
80 def test_rollback(self):
81 with self.assertRaisesMessage(Exception, "Oops"):
82 with transaction.atomic():
83 Reporter.objects.create(first_name="Haddock")
84 raise Exception("Oops, that's his last name")
85 self.assertSequenceEqual(Reporter.objects.all(), [])
86
87 def test_nested_commit_commit(self):
88 with transaction.atomic():
89 reporter1 = Reporter.objects.create(first_name="Tintin")
90 with transaction.atomic():
91 reporter2 = Reporter.objects.create(
92 first_name="Archibald", last_name="Haddock"
93 )
94 self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
95
96 def test_nested_commit_rollback(self):
97 with transaction.atomic():
98 reporter = Reporter.objects.create(first_name="Tintin")
99 with self.assertRaisesMessage(Exception, "Oops"):
100 with transaction.atomic():
101 Reporter.objects.create(first_name="Haddock")
102 raise Exception("Oops, that's his last name")
103 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
104
105 def test_nested_rollback_commit(self):
106 with self.assertRaisesMessage(Exception, "Oops"):
107 with transaction.atomic():
108 Reporter.objects.create(last_name="Tintin")
109 with transaction.atomic():
110 Reporter.objects.create(last_name="Haddock")
111 raise Exception("Oops, that's his first name")
112 self.assertSequenceEqual(Reporter.objects.all(), [])
113
114 def test_nested_rollback_rollback(self):
115 with self.assertRaisesMessage(Exception, "Oops"):
116 with transaction.atomic():
117 Reporter.objects.create(last_name="Tintin")
118 with self.assertRaisesMessage(Exception, "Oops"):
119 with transaction.atomic():
120 Reporter.objects.create(first_name="Haddock")
121 raise Exception("Oops, that's his last name")
122 raise Exception("Oops, that's his first name")
123 self.assertSequenceEqual(Reporter.objects.all(), [])
124
125 def test_merged_commit_commit(self):
126 with transaction.atomic():
127 reporter1 = Reporter.objects.create(first_name="Tintin")
128 with transaction.atomic(savepoint=False):
129 reporter2 = Reporter.objects.create(
130 first_name="Archibald", last_name="Haddock"
131 )
132 self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
133
134 def test_merged_commit_rollback(self):
135 with transaction.atomic():
136 Reporter.objects.create(first_name="Tintin")
137 with self.assertRaisesMessage(Exception, "Oops"):
138 with transaction.atomic(savepoint=False):
139 Reporter.objects.create(first_name="Haddock")
140 raise Exception("Oops, that's his last name")
141 # Writes in the outer block are rolled back too.
142 self.assertSequenceEqual(Reporter.objects.all(), [])
143
144 def test_merged_rollback_commit(self):
145 with self.assertRaisesMessage(Exception, "Oops"):
146 with transaction.atomic():
147 Reporter.objects.create(last_name="Tintin")
148 with transaction.atomic(savepoint=False):
149 Reporter.objects.create(last_name="Haddock")
150 raise Exception("Oops, that's his first name")
151 self.assertSequenceEqual(Reporter.objects.all(), [])
152
153 def test_merged_rollback_rollback(self):
154 with self.assertRaisesMessage(Exception, "Oops"):
155 with transaction.atomic():
156 Reporter.objects.create(last_name="Tintin")
157 with self.assertRaisesMessage(Exception, "Oops"):
158 with transaction.atomic(savepoint=False):
159 Reporter.objects.create(first_name="Haddock")
160 raise Exception("Oops, that's his last name")
161 raise Exception("Oops, that's his first name")
162 self.assertSequenceEqual(Reporter.objects.all(), [])
163
164 def test_reuse_commit_commit(self):
165 atomic = transaction.atomic()
166 with atomic:
167 reporter1 = Reporter.objects.create(first_name="Tintin")
168 with atomic:
169 reporter2 = Reporter.objects.create(
170 first_name="Archibald", last_name="Haddock"
171 )
172 self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
173
174 def test_reuse_commit_rollback(self):
175 atomic = transaction.atomic()
176 with atomic:
177 reporter = Reporter.objects.create(first_name="Tintin")
178 with self.assertRaisesMessage(Exception, "Oops"):
179 with atomic:
180 Reporter.objects.create(first_name="Haddock")
181 raise Exception("Oops, that's his last name")
182 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
183
184 def test_reuse_rollback_commit(self):
185 atomic = transaction.atomic()
186 with self.assertRaisesMessage(Exception, "Oops"):
187 with atomic:
188 Reporter.objects.create(last_name="Tintin")
189 with atomic:
190 Reporter.objects.create(last_name="Haddock")
191 raise Exception("Oops, that's his first name")
192 self.assertSequenceEqual(Reporter.objects.all(), [])
193
194 def test_reuse_rollback_rollback(self):
195 atomic = transaction.atomic()
196 with self.assertRaisesMessage(Exception, "Oops"):
197 with atomic:
198 Reporter.objects.create(last_name="Tintin")
199 with self.assertRaisesMessage(Exception, "Oops"):
200 with atomic:
201 Reporter.objects.create(first_name="Haddock")
202 raise Exception("Oops, that's his last name")
203 raise Exception("Oops, that's his first name")
204 self.assertSequenceEqual(Reporter.objects.all(), [])
205
206 def test_force_rollback(self):
207 with transaction.atomic():
208 Reporter.objects.create(first_name="Tintin")
209 # atomic block shouldn't rollback, but force it.
210 self.assertFalse(transaction.get_rollback())
211 transaction.set_rollback(True)
212 self.assertSequenceEqual(Reporter.objects.all(), [])
213
214 def test_prevent_rollback(self):
215 with transaction.atomic():
216 reporter = Reporter.objects.create(first_name="Tintin")
217 sid = transaction.savepoint()
218 # trigger a database error inside an inner atomic without savepoint
219 with self.assertRaises(DatabaseError):
220 with transaction.atomic(savepoint=False):
221 with connection.cursor() as cursor:
222 cursor.execute("SELECT no_such_col FROM transactions_reporter")
223 # prevent atomic from rolling back since we're recovering manually
224 self.assertTrue(transaction.get_rollback())
225 transaction.set_rollback(False)
226 transaction.savepoint_rollback(sid)
227 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
228
229 @skipUnlessDBFeature("can_release_savepoints")
230 def test_failure_on_exit_transaction(self):
231 with transaction.atomic():
232 with self.assertRaises(DatabaseError):
233 with transaction.atomic():
234 Reporter.objects.create(last_name="Tintin")
235 self.assertEqual(len(Reporter.objects.all()), 1)
236 # Incorrect savepoint id to provoke a database error.
237 connection.savepoint_ids.append("12")
238 with self.assertRaises(transaction.TransactionManagementError):
239 len(Reporter.objects.all())
240 self.assertIs(connection.needs_rollback, True)
241 if connection.savepoint_ids:
242 connection.savepoint_ids.pop()
243 self.assertSequenceEqual(Reporter.objects.all(), [])
244
245
246 class AtomicInsideTransactionTests(AtomicTests):
247 """All basic tests for atomic should also pass within an existing transaction."""
248
249 def setUp(self):
250 self.atomic = transaction.atomic()
251 self.atomic.__enter__()
252
253 def tearDown(self):
254 self.atomic.__exit__(*sys.exc_info())
255
256
257 class AtomicWithoutAutocommitTests(AtomicTests):
258 """All basic tests for atomic should also pass when autocommit is turned off."""
259
260 def setUp(self):
261 transaction.set_autocommit(False)
262 self.addCleanup(transaction.set_autocommit, True)
263 # The tests access the database after exercising 'atomic', initiating
264 # a transaction ; a rollback is required before restoring autocommit.
265 self.addCleanup(transaction.rollback)
266
267
268 @skipUnlessDBFeature("uses_savepoints")
269 class AtomicMergeTests(TransactionTestCase):
270 """Test merging transactions with savepoint=False."""
271
272 available_apps = ["transactions"]
273
274 def test_merged_outer_rollback(self):
275 with transaction.atomic():
276 Reporter.objects.create(first_name="Tintin")
277 with transaction.atomic(savepoint=False):
278 Reporter.objects.create(first_name="Archibald", last_name="Haddock")
279 with self.assertRaisesMessage(Exception, "Oops"):
280 with transaction.atomic(savepoint=False):
281 Reporter.objects.create(first_name="Calculus")
282 raise Exception("Oops, that's his last name")
283 # The third insert couldn't be roll back. Temporarily mark the
284 # connection as not needing rollback to check it.
285 self.assertTrue(transaction.get_rollback())
286 transaction.set_rollback(False)
287 self.assertEqual(Reporter.objects.count(), 3)
288 transaction.set_rollback(True)
289 # The second insert couldn't be roll back. Temporarily mark the
290 # connection as not needing rollback to check it.
291 self.assertTrue(transaction.get_rollback())
292 transaction.set_rollback(False)
293 self.assertEqual(Reporter.objects.count(), 3)
294 transaction.set_rollback(True)
295 # The first block has a savepoint and must roll back.
296 self.assertSequenceEqual(Reporter.objects.all(), [])
297
298 def test_merged_inner_savepoint_rollback(self):
299 with transaction.atomic():
300 reporter = Reporter.objects.create(first_name="Tintin")
301 with transaction.atomic():
302 Reporter.objects.create(first_name="Archibald", last_name="Haddock")
303 with self.assertRaisesMessage(Exception, "Oops"):
304 with transaction.atomic(savepoint=False):
305 Reporter.objects.create(first_name="Calculus")
306 raise Exception("Oops, that's his last name")
307 # The third insert couldn't be roll back. Temporarily mark the
308 # connection as not needing rollback to check it.
309 self.assertTrue(transaction.get_rollback())
310 transaction.set_rollback(False)
311 self.assertEqual(Reporter.objects.count(), 3)
312 transaction.set_rollback(True)
313 # The second block has a savepoint and must roll back.
314 self.assertEqual(Reporter.objects.count(), 1)
315 self.assertSequenceEqual(Reporter.objects.all(), [reporter])
316
317
318 @skipUnlessDBFeature("uses_savepoints")
319 class AtomicErrorsTests(TransactionTestCase):
320 available_apps = ["transactions"]
321 forbidden_atomic_msg = "This is forbidden when an 'atomic' block is active."
322
323 def test_atomic_prevents_setting_autocommit(self):
324 autocommit = transaction.get_autocommit()
325 with transaction.atomic():
326 with self.assertRaisesMessage(
327 transaction.TransactionManagementError, self.forbidden_atomic_msg
328 ):
329 transaction.set_autocommit(not autocommit)
330 # Make sure autocommit wasn't changed.
331 self.assertEqual(connection.autocommit, autocommit)
332
333 def test_atomic_prevents_calling_transaction_methods(self):
334 with transaction.atomic():
335 with self.assertRaisesMessage(
336 transaction.TransactionManagementError, self.forbidden_atomic_msg
337 ):
338 transaction.commit()
339 with self.assertRaisesMessage(
340 transaction.TransactionManagementError, self.forbidden_atomic_msg
341 ):
342 transaction.rollback()
343
344 def test_atomic_prevents_queries_in_broken_transaction(self):
345 r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
346 with transaction.atomic():
347 r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
348 with self.assertRaises(IntegrityError):
349 r2.save(force_insert=True)
350 # The transaction is marked as needing rollback.
351 msg = (
352 "An error occurred in the current transaction. You can't "
353 "execute queries until the end of the 'atomic' block."
354 )
355 with self.assertRaisesMessage(
356 transaction.TransactionManagementError, msg
357 ) as cm:
358 r2.save(force_update=True)
359 self.assertIsInstance(cm.exception.__cause__, IntegrityError)
360 self.assertEqual(Reporter.objects.get(pk=r1.pk).last_name, "Haddock")
361
362 @skipIfDBFeature("atomic_transactions")
363 def test_atomic_allows_queries_after_fixing_transaction(self):
364 r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
365 with transaction.atomic():
366 r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
367 with self.assertRaises(IntegrityError):
368 r2.save(force_insert=True)
369 # Mark the transaction as no longer needing rollback.
370 transaction.set_rollback(False)
371 r2.save(force_update=True)
372 self.assertEqual(Reporter.objects.get(pk=r1.pk).last_name, "Calculus")
373
374 @skipUnlessDBFeature("test_db_allows_multiple_connections")
375 def test_atomic_prevents_queries_in_broken_transaction_after_client_close(self):
376 with transaction.atomic():
377 Reporter.objects.create(first_name="Archibald", last_name="Haddock")
378 connection.close()
379 # The connection is closed and the transaction is marked as
380 # needing rollback. This will raise an InterfaceError on databases
381 # that refuse to create cursors on closed connections (PostgreSQL)
382 # and a TransactionManagementError on other databases.
383 with self.assertRaises(Error):
384 Reporter.objects.create(first_name="Cuthbert", last_name="Calculus")
385 # The connection is usable again .
386 self.assertEqual(Reporter.objects.count(), 0)
387
388
389 @skipUnlessDBFeature("uses_savepoints")
390 @skipUnless(connection.vendor == "mysql", "MySQL-specific behaviors")
391 class AtomicMySQLTests(TransactionTestCase):
392 available_apps = ["transactions"]
393
394 @skipIf(threading is None, "Test requires threading")
395 def test_implicit_savepoint_rollback(self):
396 """MySQL implicitly rolls back savepoints when it deadlocks (#22291)."""
397 Reporter.objects.create(id=1)
398 Reporter.objects.create(id=2)
399
400 main_thread_ready = threading.Event()
401
402 def other_thread():
403 try:
404 with transaction.atomic():
405 Reporter.objects.select_for_update().get(id=1)
406 main_thread_ready.wait()
407 # 1) This line locks... (see below for 2)
408 Reporter.objects.exclude(id=1).update(id=2)
409 finally:
410 # This is the thread-local connection, not the main connection.
411 connection.close()
412
413 other_thread = threading.Thread(target=other_thread)
414 other_thread.start()
415
416 with self.assertRaisesMessage(OperationalError, "Deadlock found"):
417 # Double atomic to enter a transaction and create a savepoint.
418 with transaction.atomic():
419 with transaction.atomic():
420 Reporter.objects.select_for_update().get(id=2)
421 main_thread_ready.set()
422 # The two threads can't be synchronized with an event here
423 # because the other thread locks. Sleep for a little while.
424 time.sleep(1)
425 # 2) ... and this line deadlocks. (see above for 1)
426 Reporter.objects.exclude(id=2).update(id=1)
427
428 other_thread.join()
429
430
431 class AtomicMiscTests(TransactionTestCase):
432 available_apps = ["transactions"]
433
434 def test_wrap_callable_instance(self):
435 """#20028 -- Atomic must support wrapping callable instances."""
436
437 class Callable:
438 def __call__(self):
439 pass
440
441 # Must not raise an exception
442 transaction.atomic(Callable())
443
444 @skipUnlessDBFeature("can_release_savepoints")
445 def test_atomic_does_not_leak_savepoints_on_failure(self):
446 """#23074 -- Savepoints must be released after rollback."""
447
448 # Expect an error when rolling back a savepoint that doesn't exist.
449 # Done outside of the transaction block to ensure proper recovery.
450 with self.assertRaises(Error):
451 # Start a plain transaction.
452 with transaction.atomic():
453 # Swallow the intentional error raised in the sub-transaction.
454 with self.assertRaisesMessage(Exception, "Oops"):
455 # Start a sub-transaction with a savepoint.
456 with transaction.atomic():
457 sid = connection.savepoint_ids[-1]
458 raise Exception("Oops")
459
460 # This is expected to fail because the savepoint no longer exists.
461 connection.savepoint_rollback(sid)
462
463 def test_mark_for_rollback_on_error_in_transaction(self):
464 with transaction.atomic(savepoint=False):
465 # Swallow the intentional error raised.
466 with self.assertRaisesMessage(Exception, "Oops"):
467 # Wrap in `mark_for_rollback_on_error` to check if the
468 # transaction is marked broken.
469 with transaction.mark_for_rollback_on_error():
470 # Ensure that we are still in a good state.
471 self.assertFalse(transaction.get_rollback())
472
473 raise Exception("Oops")
474
475 # mark_for_rollback_on_error marked the transaction as broken …
476 self.assertTrue(transaction.get_rollback())
477
478 # … and further queries fail.
479 msg = "You can't execute queries until the end of the 'atomic' block."
480 with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
481 Reporter.objects.create()
482
483 # Transaction errors are reset at the end of an transaction, so this
484 # should just work.
485 Reporter.objects.create()
486
487 def test_mark_for_rollback_on_error_in_autocommit(self):
488 self.assertTrue(transaction.get_autocommit())
489
490 # Swallow the intentional error raised.
491 with self.assertRaisesMessage(Exception, "Oops"):
492 # Wrap in `mark_for_rollback_on_error` to check if the transaction
493 # is marked broken.
494 with transaction.mark_for_rollback_on_error():
495 # Ensure that we are still in a good state.
496 self.assertFalse(transaction.get_connection().needs_rollback)
497
498 raise Exception("Oops")
499
500 # Ensure that `mark_for_rollback_on_error` did not mark the transaction
501 # as broken, since we are in autocommit mode …
502 self.assertFalse(transaction.get_connection().needs_rollback)
503
504 # … and further queries work nicely.
505 Reporter.objects.create()
506
507
508 class NonAutocommitTests(TransactionTestCase):
509 available_apps = []
510
511 def setUp(self):
512 transaction.set_autocommit(False)
513 self.addCleanup(transaction.set_autocommit, True)
514 self.addCleanup(transaction.rollback)
515
516 def test_orm_query_after_error_and_rollback(self):
517 """
518 ORM queries are allowed after an error and a rollback in non-autocommit
519 mode (#27504).
520 """
521 r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock")
522 r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
523 with self.assertRaises(IntegrityError):
524 r2.save(force_insert=True)
525 transaction.rollback()
526 Reporter.objects.last()
527
528 def test_orm_query_without_autocommit(self):
529 """#24921 -- ORM queries must be possible after set_autocommit(False)."""
530 Reporter.objects.create(first_name="Tintin")
531
532
533 class DurableTestsBase:
534 available_apps = ["transactions"]
535
536 def test_commit(self):
537 with transaction.atomic(durable=True):
538 reporter = Reporter.objects.create(first_name="Tintin")
539 self.assertEqual(Reporter.objects.get(), reporter)
540
541 def test_nested_outer_durable(self):
542 with transaction.atomic(durable=True):
543 reporter1 = Reporter.objects.create(first_name="Tintin")
544 with transaction.atomic():
545 reporter2 = Reporter.objects.create(
546 first_name="Archibald",
547 last_name="Haddock",
548 )
549 self.assertSequenceEqual(Reporter.objects.all(), [reporter2, reporter1])
550
551 def test_nested_both_durable(self):
552 msg = "A durable atomic block cannot be nested within another atomic block."
553 with transaction.atomic(durable=True):
554 with self.assertRaisesMessage(RuntimeError, msg):
555 with transaction.atomic(durable=True):
556 pass
557
558 def test_nested_inner_durable(self):
559 msg = "A durable atomic block cannot be nested within another atomic block."
560 with transaction.atomic():
561 with self.assertRaisesMessage(RuntimeError, msg):
562 with transaction.atomic(durable=True):
563 pass
564
565 def test_sequence_of_durables(self):
566 with transaction.atomic(durable=True):
567 reporter = Reporter.objects.create(first_name="Tintin 1")
568 self.assertEqual(Reporter.objects.get(first_name="Tintin 1"), reporter)
569 with transaction.atomic(durable=True):
570 reporter = Reporter.objects.create(first_name="Tintin 2")
571 self.assertEqual(Reporter.objects.get(first_name="Tintin 2"), reporter)
572
573
574 class DurableTransactionTests(DurableTestsBase, TransactionTestCase):
575 pass
576
577
578 class DurableTests(DurableTestsBase, TestCase):
579 pass