Ticket #19343: 19343.diff

File 19343.diff, 3.8 KB (added by Aymeric Augustin, 12 years ago)
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 0f76e11..a17bbdd 100644
    a b class TransactionTestCase(SimpleTestCase):  
    416416        self._urlconf_setup()
    417417        mail.outbox = []
    418418
     419    def _databases_names(self, include_mirrors=True):
     420        # If the test case has a multi_db=True flag, act on all databases.
     421        # Otherwise, just on the default DB.
     422        if getattr(self, 'multi_db', False):
     423            return [alias for alias in connections
     424                    if include_mirrors or not connections[alias].settings_dict['TEST_MIRROR']]
     425        else:
     426            return [DEFAULT_DB_ALIAS]
     427
    419428    def _reset_sequences(self, db_name):
    420429        conn = connections[db_name]
    421430        if conn.features.supports_sequence_reset:
    class TransactionTestCase(SimpleTestCase):  
    433442                transaction.commit_unless_managed(using=db_name)
    434443
    435444    def _fixture_setup(self):
    436         # If the test case has a multi_db=True flag, act on all databases.
    437         # Otherwise, just on the default DB.
    438         db_names = connections if getattr(self, 'multi_db', False) else [DEFAULT_DB_ALIAS]
    439         for db_name in db_names:
     445        for db_name in self._databases_names(include_mirrors=False):
    440446            # Reset sequences
    441447            if self.reset_sequences:
    442448                self._reset_sequences(db_name)
    class TransactionTestCase(SimpleTestCase):  
    502508            conn.close()
    503509
    504510    def _fixture_teardown(self):
    505         # If the test case has a multi_db=True flag, flush all databases.
    506         # Otherwise, just flush default.
    507         databases = connections if getattr(self, 'multi_db', False) else [DEFAULT_DB_ALIAS]
    508 
    509511        # Roll back any pending transactions in order to avoid a deadlock
    510512        # during flush when TEST_MIRROR is used (#18984).
    511513        for conn in connections.all():
    512514            conn.rollback_unless_managed()
    513515
    514         for db in databases:
     516        for db in self._databases_names(include_mirrors=False):
    515517            call_command('flush', verbosity=0, interactive=False, database=db,
    516518                         skip_validation=True, reset_sequences=False)
    517519
    class TestCase(TransactionTestCase):  
    796798
    797799        assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'
    798800
    799         # If the test case has a multi_db=True flag, setup all databases.
    800         # Otherwise, just use default.
    801         db_names = connections if getattr(self, 'multi_db', False) else [DEFAULT_DB_ALIAS]
    802 
    803         for db_name in db_names:
     801        for db_name in self._databases_names():
    804802            transaction.enter_transaction_management(using=db_name)
    805803            transaction.managed(True, using=db_name)
    806804        disable_transaction_methods()
    class TestCase(TransactionTestCase):  
    808806        from django.contrib.sites.models import Site
    809807        Site.objects.clear_cache()
    810808
    811         for db in db_names:
     809        for db in self._databases_names(include_mirrors=False):
    812810            if hasattr(self, 'fixtures'):
    813811                call_command('loaddata', *self.fixtures,
    814812                             **{
    class TestCase(TransactionTestCase):  
    822820        if not connections_support_transactions():
    823821            return super(TestCase, self)._fixture_teardown()
    824822
    825         # If the test case has a multi_db=True flag, teardown all databases.
    826         # Otherwise, just teardown default.
    827         if getattr(self, 'multi_db', False):
    828             databases = connections
    829         else:
    830             databases = [DEFAULT_DB_ALIAS]
    831 
    832823        restore_transaction_methods()
    833         for db in databases:
     824        for db in self._databases_names():
    834825            transaction.rollback(using=db)
    835826            transaction.leave_transaction_management(using=db)
    836827
Back to Top