Refs #32355 -- Used addClassCleanup() in SimpleTestCase and SerializeMixin.

This commit is contained in:
Mariusz Felisiak 2021-06-08 11:35:05 +02:00 committed by GitHub
parent c0e29cec83
commit faba5b702a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 18 deletions

View File

@ -179,10 +179,13 @@ class SimpleTestCase(unittest.TestCase):
if cls._overridden_settings: if cls._overridden_settings:
cls._cls_overridden_context = override_settings(**cls._overridden_settings) cls._cls_overridden_context = override_settings(**cls._overridden_settings)
cls._cls_overridden_context.enable() cls._cls_overridden_context.enable()
cls.addClassCleanup(cls._cls_overridden_context.disable)
if cls._modified_settings: if cls._modified_settings:
cls._cls_modified_context = modify_settings(cls._modified_settings) cls._cls_modified_context = modify_settings(cls._modified_settings)
cls._cls_modified_context.enable() cls._cls_modified_context.enable()
cls.addClassCleanup(cls._cls_modified_context.disable)
cls._add_databases_failures() cls._add_databases_failures()
cls.addClassCleanup(cls._remove_databases_failures)
@classmethod @classmethod
def _validate_databases(cls): def _validate_databases(cls):
@ -227,17 +230,6 @@ class SimpleTestCase(unittest.TestCase):
method = getattr(connection, name) method = getattr(connection, name)
setattr(connection, name, method.wrapped) setattr(connection, name, method.wrapped)
@classmethod
def tearDownClass(cls):
cls._remove_databases_failures()
if hasattr(cls, '_cls_modified_context'):
cls._cls_modified_context.disable()
delattr(cls, '_cls_modified_context')
if hasattr(cls, '_cls_overridden_context'):
cls._cls_overridden_context.disable()
delattr(cls, '_cls_overridden_context')
super().tearDownClass()
def __call__(self, result=None): def __call__(self, result=None):
""" """
Wrapper around default __call__ method to perform common Django test Wrapper around default __call__ method to perform common Django test
@ -1221,14 +1213,12 @@ class TestCase(TransactionTestCase):
call_command('loaddata', *cls.fixtures, **{'verbosity': 0, 'database': db_name}) call_command('loaddata', *cls.fixtures, **{'verbosity': 0, 'database': db_name})
except Exception: except Exception:
cls._rollback_atomics(cls.cls_atomics) cls._rollback_atomics(cls.cls_atomics)
cls._remove_databases_failures()
raise raise
pre_attrs = cls.__dict__.copy() pre_attrs = cls.__dict__.copy()
try: try:
cls.setUpTestData() cls.setUpTestData()
except Exception: except Exception:
cls._rollback_atomics(cls.cls_atomics) cls._rollback_atomics(cls.cls_atomics)
cls._remove_databases_failures()
raise raise
for name, value in cls.__dict__.items(): for name, value in cls.__dict__.items():
if value is not pre_attrs.get(name): if value is not pre_attrs.get(name):
@ -1634,10 +1624,6 @@ class SerializeMixin:
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls._lockfile = open(cls.lockfile) cls._lockfile = open(cls.lockfile)
cls.addClassCleanup(cls._lockfile.close)
locks.lock(cls._lockfile, locks.LOCK_EX) locks.lock(cls._lockfile, locks.LOCK_EX)
super().setUpClass() super().setUpClass()
@classmethod
def tearDownClass(cls):
super().tearDownClass()
cls._lockfile.close()