diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py index 3e5a55372a..d675372c7a 100644 --- a/tests/regressiontests/fixtures_regress/tests.py +++ b/tests/regressiontests/fixtures_regress/tests.py @@ -21,18 +21,15 @@ from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, ExternalDependency, Thingy) -pre_save_checks = [] -def animal_pre_save_check(signal, sender, instance, **kwargs): - "A signal that is used to check the type of data loaded from fixtures" - pre_save_checks.append( - ( - 'Count = %s (%s)' % (instance.count, type(instance.count)), - 'Weight = %s (%s)' % (instance.weight, type(instance.weight)), - ) - ) - - class TestFixtures(TestCase): + def animal_pre_save_check(self, signal, sender, instance, **kwargs): + self.pre_save_checks.append( + ( + 'Count = %s (%s)' % (instance.count, type(instance.count)), + 'Weight = %s (%s)' % (instance.weight, type(instance.weight)), + ) + ) + def test_duplicate_pk(self): """ This is a regression test for ticket #3790. @@ -111,7 +108,6 @@ class TestFixtures(TestCase): ) self.assertEqual(Absolute.load_count, 1) - def test_unknown_format(self): """ Test for ticket #4371 -- Loading data of an unknown format should fail @@ -246,9 +242,8 @@ class TestFixtures(TestCase): Test for tickets #8298, #9942 - Field values should be coerced into the correct type by the deserializer, not as part of the database write. """ - global pre_save_checks - pre_save_checks = [] - signals.pre_save.connect(animal_pre_save_check) + self.pre_save_checks = [] + signals.pre_save.connect(self.animal_pre_save_check) try: management.call_command( 'loaddata', @@ -257,14 +252,14 @@ class TestFixtures(TestCase): commit=False, ) self.assertEqual( - pre_save_checks, + self.pre_save_checks, [ ("Count = 42 (<%s 'int'>)" % ('class' if PY3 else 'type'), "Weight = 1.2 (<%s 'float'>)" % ('class' if PY3 else 'type')) ] ) finally: - signals.pre_save.disconnect(animal_pre_save_check) + signals.pre_save.disconnect(self.animal_pre_save_check) def test_dumpdata_uses_default_manager(self): """ diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py index 7225fb03ef..aaf8bcffcf 100644 --- a/tests/regressiontests/settings_tests/tests.py +++ b/tests/regressiontests/settings_tests/tests.py @@ -71,16 +71,18 @@ class SettingGetter(object): def __init__(self): self.test = getattr(settings, 'TEST', 'undefined') -testvalue = None - -def signal_callback(sender, setting, value, **kwargs): - if setting == 'TEST': - global testvalue - testvalue = value - -signals.setting_changed.connect(signal_callback) class SettingsTests(TestCase): + def setUp(self): + self.testvalue = None + signals.setting_changed.connect(self.signal_callback) + + def tearDown(self): + signals.setting_changed.disconnect(self.signal_callback) + + def signal_callback(self, sender, setting, value, **kwargs): + if setting == 'TEST': + self.testvalue = value def test_override(self): settings.TEST = 'test' @@ -128,12 +130,12 @@ class SettingsTests(TestCase): def test_signal_callback_context_manager(self): self.assertRaises(AttributeError, getattr, settings, 'TEST') with self.settings(TEST='override'): - self.assertEqual(testvalue, 'override') - self.assertEqual(testvalue, None) + self.assertEqual(self.testvalue, 'override') + self.assertEqual(self.testvalue, None) @override_settings(TEST='override') def test_signal_callback_decorator(self): - self.assertEqual(testvalue, 'override') + self.assertEqual(self.testvalue, 'override') # # Regression tests for #10130: deleting settings. diff --git a/tests/regressiontests/signals_regress/tests.py b/tests/regressiontests/signals_regress/tests.py index 4809a1e2a5..8fb3ad5a48 100644 --- a/tests/regressiontests/signals_regress/tests.py +++ b/tests/regressiontests/signals_regress/tests.py @@ -6,31 +6,6 @@ from django.test import TestCase from .models import Author, Book -signal_output = [] - -def pre_save_test(signal, sender, instance, **kwargs): - signal_output.append('pre_save signal, %s' % instance) - if kwargs.get('raw'): - signal_output.append('Is raw') - -def post_save_test(signal, sender, instance, **kwargs): - signal_output.append('post_save signal, %s' % instance) - if 'created' in kwargs: - if kwargs['created']: - signal_output.append('Is created') - else: - signal_output.append('Is updated') - if kwargs.get('raw'): - signal_output.append('Is raw') - -def pre_delete_test(signal, sender, instance, **kwargs): - signal_output.append('pre_save signal, %s' % instance) - signal_output.append('instance.id is not None: %s' % (instance.id != None)) - -def post_delete_test(signal, sender, instance, **kwargs): - signal_output.append('post_delete signal, %s' % instance) - signal_output.append('instance.id is not None: %s' % (instance.id != None)) - class SignalsRegressTests(TestCase): """ Testing signals before/after saving and deleting. @@ -38,12 +13,35 @@ class SignalsRegressTests(TestCase): def get_signal_output(self, fn, *args, **kwargs): # Flush any existing signal output - global signal_output - signal_output = [] + self.signal_output = [] fn(*args, **kwargs) - return signal_output + return self.signal_output + + def pre_save_test(self, signal, sender, instance, **kwargs): + self.signal_output.append('pre_save signal, %s' % instance) + if kwargs.get('raw'): + self.signal_output.append('Is raw') + + def post_save_test(self, signal, sender, instance, **kwargs): + self.signal_output.append('post_save signal, %s' % instance) + if 'created' in kwargs: + if kwargs['created']: + self.signal_output.append('Is created') + else: + self.signal_output.append('Is updated') + if kwargs.get('raw'): + self.signal_output.append('Is raw') + + def pre_delete_test(self, signal, sender, instance, **kwargs): + self.signal_output.append('pre_save signal, %s' % instance) + self.signal_output.append('instance.id is not None: %s' % (instance.id != None)) + + def post_delete_test(self, signal, sender, instance, **kwargs): + self.signal_output.append('post_delete signal, %s' % instance) + self.signal_output.append('instance.id is not None: %s' % (instance.id != None)) def setUp(self): + self.signal_output = [] # Save up the number of connected signals so that we can check at the end # that all the signals we register get properly unregistered (#9989) self.pre_signals = (len(models.signals.pre_save.receivers), @@ -51,16 +49,16 @@ class SignalsRegressTests(TestCase): len(models.signals.pre_delete.receivers), len(models.signals.post_delete.receivers)) - models.signals.pre_save.connect(pre_save_test) - models.signals.post_save.connect(post_save_test) - models.signals.pre_delete.connect(pre_delete_test) - models.signals.post_delete.connect(post_delete_test) + models.signals.pre_save.connect(self.pre_save_test) + models.signals.post_save.connect(self.post_save_test) + models.signals.pre_delete.connect(self.pre_delete_test) + models.signals.post_delete.connect(self.post_delete_test) def tearDown(self): - models.signals.post_delete.disconnect(post_delete_test) - models.signals.pre_delete.disconnect(pre_delete_test) - models.signals.post_save.disconnect(post_save_test) - models.signals.pre_save.disconnect(pre_save_test) + models.signals.post_delete.disconnect(self.post_delete_test) + models.signals.pre_delete.disconnect(self.pre_delete_test) + models.signals.post_save.disconnect(self.post_save_test) + models.signals.pre_save.disconnect(self.pre_save_test) # Check that all our signals got disconnected properly. post_signals = (len(models.signals.pre_save.receivers),