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):
|
1071 | 1071 | return all(conn.features.supports_transactions for conn in conns) |
1072 | 1072 | |
1073 | 1073 | |
| 1074 | def 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 | |
1074 | 1082 | class TestCase(TransactionTestCase): |
1075 | 1083 | """ |
1076 | 1084 | Similar to TransactionTestCase, but use `transaction.atomic()` to achieve |
… |
… |
class TestCase(TransactionTestCase):
|
1104 | 1112 | def _databases_support_transactions(cls): |
1105 | 1113 | return connections_support_transactions(cls.databases) |
1106 | 1114 | |
| 1115 | @classmethod |
| 1116 | def _databases_support_savepoints(cls): |
| 1117 | return connections_support_savepoints(cls.databases) |
| 1118 | |
1107 | 1119 | @classmethod |
1108 | 1120 | def setUpClass(cls): |
1109 | 1121 | super().setUpClass() |
1110 | | if not cls._databases_support_transactions(): |
| 1122 | if not (cls._databases_support_transactions() and cls._databases_support_savepoints()): |
1111 | 1123 | return |
1112 | 1124 | cls.cls_atomics = cls._enter_atomics() |
1113 | 1125 | |
… |
… |
class TestCase(TransactionTestCase):
|
1128 | 1140 | |
1129 | 1141 | @classmethod |
1130 | 1142 | def tearDownClass(cls): |
1131 | | if cls._databases_support_transactions(): |
| 1143 | if cls._databases_support_transactions() and cls._databases_support_savepoints(): |
1132 | 1144 | cls._rollback_atomics(cls.cls_atomics) |
1133 | 1145 | for conn in connections.all(): |
1134 | 1146 | conn.close() |
… |
… |
class TestCase(TransactionTestCase):
|
1146 | 1158 | |
1147 | 1159 | def _fixture_setup(self): |
1148 | 1160 | 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. |
1151 | 1163 | self.setUpTestData() |
1152 | 1164 | return super()._fixture_setup() |
1153 | 1165 | |
1154 | 1166 | assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances' |
1155 | 1167 | 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() |
1156 | 1174 | |
1157 | 1175 | def _fixture_teardown(self): |
1158 | 1176 | if not self._databases_support_transactions(): |