Skip to content

Commit

Permalink
Fixed #28263 -- Fixed TestCase setup for databases that don't support…
Browse files Browse the repository at this point in the history
… savepoints.
  • Loading branch information
timgraham authored Feb 14, 2024
1 parent cf107fe commit bf692b2
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions django/test/testcases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1325,10 +1337,17 @@ def _rollback_atomics(cls, atomics):
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()

Expand Down Expand Up @@ -1356,7 +1375,10 @@ def setUpClass(cls):

@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()
Expand All @@ -1382,6 +1404,15 @@ def _fixture_setup(self):
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():
Expand Down

0 comments on commit bf692b2

Please sign in to comment.