Ticket #28263: 28263.diff

File 28263.diff, 2.9 KB (added by Tim Graham, 5 years ago)
  • django/test/testcases.py

    commit e9cfcc0cdaefd20a5b347064b7a9953b949bed37
    Author: Tim Graham <timograham@gmail.com>
    Date:   Mon Oct 21 09:14:07 2019 -0400
    
        Fixed #28263 -- Corrected TestCase set up for databases that don't support savepoints.
    
    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 7ebddf80e5..beabb7990c 100644
    a b def connections_support_transactions(aliases=None):  
    10711071    return all(conn.features.supports_transactions for conn in conns)
    10721072
    10731073
     1074def connections_support_savepoints(aliases=None):
     1075    """
     1076    Return whether or not all (or specified) connections support savepoints.
     1077    """
     1078    conns = connections.all() if aliases is None else (connections[alias] for alias in aliases)
     1079    return all(conn.features.uses_savepoints for conn in conns)
     1080
     1081
    10741082class TestCase(TransactionTestCase):
    10751083    """
    10761084    Similar to TransactionTestCase, but use `transaction.atomic()` to achieve
    class TestCase(TransactionTestCase):  
    11041112    def _databases_support_transactions(cls):
    11051113        return connections_support_transactions(cls.databases)
    11061114
     1115    @classmethod
     1116    def _databases_support_savepoints(cls):
     1117        return connections_support_savepoints(cls.databases)
     1118
    11071119    @classmethod
    11081120    def setUpClass(cls):
    11091121        super().setUpClass()
    1110         if not cls._databases_support_transactions():
     1122        if not (cls._databases_support_transactions() and cls._databases_support_savepoints()):
    11111123            return
    11121124        cls.cls_atomics = cls._enter_atomics()
    11131125
    class TestCase(TransactionTestCase):  
    11281140
    11291141    @classmethod
    11301142    def tearDownClass(cls):
    1131         if cls._databases_support_transactions():
     1143        if cls._databases_support_transactions() and cls._databases_support_savepoints():
    11321144            cls._rollback_atomics(cls.cls_atomics)
    11331145            for conn in connections.all():
    11341146                conn.close()
    class TestCase(TransactionTestCase):  
    11461158
    11471159    def _fixture_setup(self):
    11481160        if not self._databases_support_transactions():
    1149             # If the backend does not support transactions, we should reload
    1150             # class data before each test
     1161            # If the backend does not support transactions,
     1162            # reload class data before each test.
    11511163            self.setUpTestData()
    11521164            return super()._fixture_setup()
    11531165
    11541166        assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'
    11551167        self.atomics = self._enter_atomics()
     1168        if not self._databases_support_savepoints():
     1169            # TODO: exception wrapping as above
     1170            if self.fixtures:
     1171                for db_name in self._databases_names(include_mirrors=False):
     1172                    call_command('loaddata', *self.fixtures, **{'verbosity': 0, 'database': db_name})
     1173            self.setUpTestData()
    11561174
    11571175    def _fixture_teardown(self):
    11581176        if not self._databases_support_transactions():
Back to Top