summaryrefslogtreecommitdiff
path: root/tests/test_utils
diff options
context:
space:
mode:
authorAdam Chainz <adam@adamj.eu>2015-07-26 17:42:21 +0100
committerTim Graham <timograham@gmail.com>2015-08-01 07:38:20 -0400
commitb46dad1befb1edd680c71f09df952245b3413f20 (patch)
tree2569259468a767336f53954d0f4323ad9ab8b3a9 /tests/test_utils
parent1acdb88136d6ca470e78dfff2998c3cc0795b39b (diff)
[1.8.x] Fixed #25176 -- Prevented TestCase.setUpTestData() exception from leaking transaction.
Backport of 0abb06930fc0686cb35079934e5bb40df66f5691 from master
Diffstat (limited to 'tests/test_utils')
-rw-r--r--tests/test_utils/tests.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index 1c5c12ca5b..ae8d34b691 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -914,3 +914,36 @@ class OverrideSettingsTests(TestCase):
with self.settings(STATICFILES_DIRS=[test_path]):
finder = get_finder('django.contrib.staticfiles.finders.FileSystemFinder')
self.assertIn(expected_location, finder.locations)
+
+
+class TestBadSetUpTestData(TestCase):
+ """
+ An exception in setUpTestData() shouldn't leak a transaction which would
+ cascade across the rest of the test suite.
+ """
+ class MyException(Exception):
+ pass
+
+ @classmethod
+ def setUpClass(cls):
+ try:
+ super(TestBadSetUpTestData, cls).setUpClass()
+ except cls.MyException:
+ cls._in_atomic_block = connection.in_atomic_block
+
+ @classmethod
+ def tearDownClass(Cls):
+ # override to avoid a second cls._rollback_atomics() which would fail.
+ # Normal setUpClass() methods won't have exception handling so this
+ # method wouldn't typically be run.
+ pass
+
+ @classmethod
+ def setUpTestData(cls):
+ # Simulate a broken setUpTestData() method.
+ raise cls.MyException()
+
+ def test_failure_in_setUpTestData_should_rollback_transaction(self):
+ # setUpTestData() should call _rollback_atomics() so that the
+ # transaction doesn't leak.
+ self.assertFalse(self._in_atomic_block)