Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Abused atomic for transaction handling in TestCase.

It isn't necessary to disable set_autocommit since its use is prohibited
inside an atomic block. It's still necessary to disable the legacy
transaction management methods for backwards compatibility, until
they're removed.
  • Loading branch information...
commit f32100939e8ea8a2714e45e22467af5df55c8f33 1 parent d04964e
@aaugustin aaugustin authored
Showing with 9 additions and 7 deletions.
  1. +9 −7 django/test/testcases.py
View
16 django/test/testcases.py
@@ -63,7 +63,6 @@ def to_list(value):
value = [value]
return value
-real_set_autocommit = transaction.set_autocommit
real_commit = transaction.commit
real_rollback = transaction.rollback
real_enter_transaction_management = transaction.enter_transaction_management
@@ -74,7 +73,6 @@ def nop(*args, **kwargs):
return
def disable_transaction_methods():
- transaction.set_autocommit = nop
transaction.commit = nop
transaction.rollback = nop
transaction.enter_transaction_management = nop
@@ -82,7 +80,6 @@ def disable_transaction_methods():
transaction.abort = nop
def restore_transaction_methods():
- transaction.set_autocommit = real_set_autocommit
transaction.commit = real_commit
transaction.rollback = real_rollback
transaction.enter_transaction_management = real_enter_transaction_management
@@ -814,8 +811,11 @@ def _fixture_setup(self):
assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'
+ self.atomics = {}
for db_name in self._databases_names():
- transaction.enter_transaction_management(using=db_name)
+ self.atomics[db_name] = transaction.atomic(using=db_name)
+ self.atomics[db_name].__enter__()
+ # Remove this when the legacy transaction management goes away.
disable_transaction_methods()
from django.contrib.sites.models import Site
@@ -835,10 +835,12 @@ def _fixture_teardown(self):
if not connections_support_transactions():
return super(TestCase, self)._fixture_teardown()
+ # Remove this when the legacy transaction management goes away.
restore_transaction_methods()
- for db in self._databases_names():
- transaction.rollback(using=db)
- transaction.leave_transaction_management(using=db)
+ for db_name in reversed(self._databases_names()):
+ # Hack to force a rollback
+ connections[db_name].needs_rollback = True
+ self.atomics[db_name].__exit__(None, None, None)
def _deferredSkip(condition, reason):
Please sign in to comment.
Something went wrong with that request. Please try again.