Added tests for the post_migrate signal.

This commit is contained in:
Simon Charette 2016-05-11 20:48:25 -04:00
parent 99d9d4e695
commit 18900e55c5
1 changed files with 34 additions and 28 deletions

View File

@ -11,10 +11,11 @@ MIGRATE_VERBOSITY = 1
MIGRATE_INTERACTIVE = False MIGRATE_INTERACTIVE = False
class PreMigrateReceiver(object): class Receiver(object):
def __init__(self): def __init__(self, signal):
self.call_counter = 0 self.call_counter = 0
self.call_args = None self.call_args = None
signal.connect(self, sender=APP_CONFIG)
def __call__(self, signal, sender, **kwargs): def __call__(self, signal, sender, **kwargs):
self.call_counter = self.call_counter + 1 self.call_counter = self.call_counter + 1
@ -27,9 +28,11 @@ class OneTimeReceiver(object):
several databases and several times for some of them. several databases and several times for some of them.
""" """
def __init__(self): def __init__(self, signal):
self.signal = signal
self.call_counter = 0 self.call_counter = 0
self.call_args = None self.call_args = None
self.signal.connect(self, sender=APP_CONFIG)
def __call__(self, signal, sender, **kwargs): def __call__(self, signal, sender, **kwargs):
# Although test runner calls migrate for several databases, # Although test runner calls migrate for several databases,
@ -38,7 +41,7 @@ class OneTimeReceiver(object):
self.call_counter = self.call_counter + 1 self.call_counter = self.call_counter + 1
self.call_args = kwargs self.call_args = kwargs
# we need to test only one call of migrate # we need to test only one call of migrate
signals.pre_migrate.disconnect(pre_migrate_receiver, sender=APP_CONFIG) self.signal.disconnect(self, sender=APP_CONFIG)
# We connect receiver here and not in unit test code because we need to # We connect receiver here and not in unit test code because we need to
@ -49,49 +52,52 @@ class OneTimeReceiver(object):
# 2. We connect receiver. # 2. We connect receiver.
# 3. Test runner calls migrate for create default database. # 3. Test runner calls migrate for create default database.
# 4. Test runner execute our unit test code. # 4. Test runner execute our unit test code.
pre_migrate_receiver = OneTimeReceiver() pre_migrate_receiver = OneTimeReceiver(signals.pre_migrate)
signals.pre_migrate.connect(pre_migrate_receiver, sender=APP_CONFIG) post_migrate_receiver = OneTimeReceiver(signals.post_migrate)
class MigrateSignalTests(TestCase): class MigrateSignalTests(TestCase):
available_apps = ['migrate_signals'] available_apps = ['migrate_signals']
def test_pre_migrate_call_time(self): def test_call_time(self):
self.assertEqual(pre_migrate_receiver.call_counter, 1) self.assertEqual(pre_migrate_receiver.call_counter, 1)
self.assertEqual(post_migrate_receiver.call_counter, 1)
def test_pre_migrate_args(self): def test_args(self):
r = PreMigrateReceiver() pre_migrate_receiver = Receiver(signals.pre_migrate)
signals.pre_migrate.connect(r, sender=APP_CONFIG) post_migrate_receiver = Receiver(signals.post_migrate)
management.call_command( management.call_command(
'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY, 'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY,
interactive=MIGRATE_INTERACTIVE, stdout=six.StringIO(), interactive=MIGRATE_INTERACTIVE, stdout=six.StringIO(),
) )
args = r.call_args for receiver in [pre_migrate_receiver, post_migrate_receiver]:
self.assertEqual(r.call_counter, 1) args = receiver.call_args
self.assertEqual(set(args), set(PRE_MIGRATE_ARGS)) self.assertEqual(receiver.call_counter, 1)
self.assertEqual(args['app_config'], APP_CONFIG) self.assertEqual(set(args), set(SIGNAL_ARGS))
self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY) self.assertEqual(args['app_config'], APP_CONFIG)
self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE) self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
self.assertEqual(args['using'], 'default') self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
self.assertEqual(args['using'], 'default')
@override_settings(MIGRATION_MODULES={'migrate_signals': 'migrate_signals.custom_migrations'}) @override_settings(MIGRATION_MODULES={'migrate_signals': 'migrate_signals.custom_migrations'})
def test_pre_migrate_migrations_only(self): def test_migrations_only(self):
""" """
If all apps have migrations, pre_migrate should be sent. If all apps have migrations, migration signals should be sent.
""" """
r = PreMigrateReceiver() pre_migrate_receiver = Receiver(signals.pre_migrate)
signals.pre_migrate.connect(r, sender=APP_CONFIG) post_migrate_receiver = Receiver(signals.post_migrate)
stdout = six.StringIO() stdout = six.StringIO()
management.call_command( management.call_command(
'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY, 'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY,
interactive=MIGRATE_INTERACTIVE, stdout=stdout, interactive=MIGRATE_INTERACTIVE, stdout=stdout,
) )
args = r.call_args for receiver in [pre_migrate_receiver, post_migrate_receiver]:
self.assertEqual(r.call_counter, 1) args = receiver.call_args
self.assertEqual(set(args), set(PRE_MIGRATE_ARGS)) self.assertEqual(receiver.call_counter, 1)
self.assertEqual(args['app_config'], APP_CONFIG) self.assertEqual(set(args), set(SIGNAL_ARGS))
self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY) self.assertEqual(args['app_config'], APP_CONFIG)
self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE) self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
self.assertEqual(args['using'], 'default') self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
self.assertEqual(args['using'], 'default')