Cleaned up several test cases, to better isolate state.

This commit is contained in:
Alex Gaynor 2012-09-07 13:43:51 -04:00
parent 72b8f8d73b
commit f47af9df1d
3 changed files with 59 additions and 64 deletions

View File

@ -21,18 +21,15 @@ from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
ExternalDependency, Thingy) 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): 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): def test_duplicate_pk(self):
""" """
This is a regression test for ticket #3790. This is a regression test for ticket #3790.
@ -111,7 +108,6 @@ class TestFixtures(TestCase):
) )
self.assertEqual(Absolute.load_count, 1) self.assertEqual(Absolute.load_count, 1)
def test_unknown_format(self): def test_unknown_format(self):
""" """
Test for ticket #4371 -- Loading data of an unknown format should fail 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 Test for tickets #8298, #9942 - Field values should be coerced into the
correct type by the deserializer, not as part of the database write. correct type by the deserializer, not as part of the database write.
""" """
global pre_save_checks self.pre_save_checks = []
pre_save_checks = [] signals.pre_save.connect(self.animal_pre_save_check)
signals.pre_save.connect(animal_pre_save_check)
try: try:
management.call_command( management.call_command(
'loaddata', 'loaddata',
@ -257,14 +252,14 @@ class TestFixtures(TestCase):
commit=False, commit=False,
) )
self.assertEqual( self.assertEqual(
pre_save_checks, self.pre_save_checks,
[ [
("Count = 42 (<%s 'int'>)" % ('class' if PY3 else 'type'), ("Count = 42 (<%s 'int'>)" % ('class' if PY3 else 'type'),
"Weight = 1.2 (<%s 'float'>)" % ('class' if PY3 else 'type')) "Weight = 1.2 (<%s 'float'>)" % ('class' if PY3 else 'type'))
] ]
) )
finally: 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): def test_dumpdata_uses_default_manager(self):
""" """

View File

@ -71,16 +71,18 @@ class SettingGetter(object):
def __init__(self): def __init__(self):
self.test = getattr(settings, 'TEST', 'undefined') 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): 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): def test_override(self):
settings.TEST = 'test' settings.TEST = 'test'
@ -128,12 +130,12 @@ class SettingsTests(TestCase):
def test_signal_callback_context_manager(self): def test_signal_callback_context_manager(self):
self.assertRaises(AttributeError, getattr, settings, 'TEST') self.assertRaises(AttributeError, getattr, settings, 'TEST')
with self.settings(TEST='override'): with self.settings(TEST='override'):
self.assertEqual(testvalue, 'override') self.assertEqual(self.testvalue, 'override')
self.assertEqual(testvalue, None) self.assertEqual(self.testvalue, None)
@override_settings(TEST='override') @override_settings(TEST='override')
def test_signal_callback_decorator(self): def test_signal_callback_decorator(self):
self.assertEqual(testvalue, 'override') self.assertEqual(self.testvalue, 'override')
# #
# Regression tests for #10130: deleting settings. # Regression tests for #10130: deleting settings.

View File

@ -6,31 +6,6 @@ from django.test import TestCase
from .models import Author, Book 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): class SignalsRegressTests(TestCase):
""" """
Testing signals before/after saving and deleting. Testing signals before/after saving and deleting.
@ -38,12 +13,35 @@ class SignalsRegressTests(TestCase):
def get_signal_output(self, fn, *args, **kwargs): def get_signal_output(self, fn, *args, **kwargs):
# Flush any existing signal output # Flush any existing signal output
global signal_output self.signal_output = []
signal_output = []
fn(*args, **kwargs) 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): def setUp(self):
self.signal_output = []
# Save up the number of connected signals so that we can check at the end # 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) # that all the signals we register get properly unregistered (#9989)
self.pre_signals = (len(models.signals.pre_save.receivers), 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.pre_delete.receivers),
len(models.signals.post_delete.receivers)) len(models.signals.post_delete.receivers))
models.signals.pre_save.connect(pre_save_test) models.signals.pre_save.connect(self.pre_save_test)
models.signals.post_save.connect(post_save_test) models.signals.post_save.connect(self.post_save_test)
models.signals.pre_delete.connect(pre_delete_test) models.signals.pre_delete.connect(self.pre_delete_test)
models.signals.post_delete.connect(post_delete_test) models.signals.post_delete.connect(self.post_delete_test)
def tearDown(self): def tearDown(self):
models.signals.post_delete.disconnect(post_delete_test) models.signals.post_delete.disconnect(self.post_delete_test)
models.signals.pre_delete.disconnect(pre_delete_test) models.signals.pre_delete.disconnect(self.pre_delete_test)
models.signals.post_save.disconnect(post_save_test) models.signals.post_save.disconnect(self.post_save_test)
models.signals.pre_save.disconnect(pre_save_test) models.signals.pre_save.disconnect(self.pre_save_test)
# Check that all our signals got disconnected properly. # Check that all our signals got disconnected properly.
post_signals = (len(models.signals.pre_save.receivers), post_signals = (len(models.signals.pre_save.receivers),