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.
This commit is contained in:
Aymeric Augustin 2013-03-08 13:31:14 +01:00
parent d04964e70d
commit f32100939e
1 changed files with 9 additions and 7 deletions

View File

@ -63,7 +63,6 @@ def to_list(value):
value = [value] value = [value]
return value return value
real_set_autocommit = transaction.set_autocommit
real_commit = transaction.commit real_commit = transaction.commit
real_rollback = transaction.rollback real_rollback = transaction.rollback
real_enter_transaction_management = transaction.enter_transaction_management real_enter_transaction_management = transaction.enter_transaction_management
@ -74,7 +73,6 @@ def nop(*args, **kwargs):
return return
def disable_transaction_methods(): def disable_transaction_methods():
transaction.set_autocommit = nop
transaction.commit = nop transaction.commit = nop
transaction.rollback = nop transaction.rollback = nop
transaction.enter_transaction_management = nop transaction.enter_transaction_management = nop
@ -82,7 +80,6 @@ def disable_transaction_methods():
transaction.abort = nop transaction.abort = nop
def restore_transaction_methods(): def restore_transaction_methods():
transaction.set_autocommit = real_set_autocommit
transaction.commit = real_commit transaction.commit = real_commit
transaction.rollback = real_rollback transaction.rollback = real_rollback
transaction.enter_transaction_management = real_enter_transaction_management transaction.enter_transaction_management = real_enter_transaction_management
@ -814,8 +811,11 @@ class TestCase(TransactionTestCase):
assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances' assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'
self.atomics = {}
for db_name in self._databases_names(): 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() disable_transaction_methods()
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
@ -835,10 +835,12 @@ class TestCase(TransactionTestCase):
if not connections_support_transactions(): if not connections_support_transactions():
return super(TestCase, self)._fixture_teardown() return super(TestCase, self)._fixture_teardown()
# Remove this when the legacy transaction management goes away.
restore_transaction_methods() restore_transaction_methods()
for db in self._databases_names(): for db_name in reversed(self._databases_names()):
transaction.rollback(using=db) # Hack to force a rollback
transaction.leave_transaction_management(using=db) connections[db_name].needs_rollback = True
self.atomics[db_name].__exit__(None, None, None)
def _deferredSkip(condition, reason): def _deferredSkip(condition, reason):