1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Fixed #28263 -- Fixed TestCase setup for databases that don't support savepoints.

This commit is contained in:
Tim Graham 2024-02-14 01:57:38 -05:00 committed by GitHub
parent cf107fe255
commit bf692b2fdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1251,6 +1251,18 @@ def connections_support_transactions(aliases=None):
return all(conn.features.supports_transactions for conn in conns)
def connections_support_savepoints(aliases=None):
"""
Return whether or not all (or specified) connections support savepoints.
"""
conns = (
connections.all()
if aliases is None
else (connections[alias] for alias in aliases)
)
return all(conn.features.uses_savepoints for conn in conns)
class TestData:
"""
Descriptor to provide TestCase instance isolation for attributes assigned
@ -1325,10 +1337,17 @@ class TestCase(TransactionTestCase):
def _databases_support_transactions(cls):
return connections_support_transactions(cls.databases)
@classmethod
def _databases_support_savepoints(cls):
return connections_support_savepoints(cls.databases)
@classmethod
def setUpClass(cls):
super().setUpClass()
if not cls._databases_support_transactions():
if not (
cls._databases_support_transactions()
and cls._databases_support_savepoints()
):
return
cls.cls_atomics = cls._enter_atomics()
@ -1356,7 +1375,10 @@ class TestCase(TransactionTestCase):
@classmethod
def tearDownClass(cls):
if cls._databases_support_transactions():
if (
cls._databases_support_transactions()
and cls._databases_support_savepoints()
):
cls._rollback_atomics(cls.cls_atomics)
for conn in connections.all(initialized_only=True):
conn.close()
@ -1382,6 +1404,15 @@ class TestCase(TransactionTestCase):
if self.reset_sequences:
raise TypeError("reset_sequences cannot be used on TestCase instances")
self.atomics = self._enter_atomics()
if not self._databases_support_savepoints():
if self.fixtures:
for db_name in self._databases_names(include_mirrors=False):
call_command(
"loaddata",
*self.fixtures,
**{"verbosity": 0, "database": db_name},
)
self.setUpTestData()
def _fixture_teardown(self):
if not self._databases_support_transactions():