2013-12-27 19:55:58 +00:00
|
|
|
from django.apps import apps
|
2014-01-20 02:45:21 +00:00
|
|
|
from django.core import management
|
2013-05-17 22:18:35 +00:00
|
|
|
from django.db.models import signals
|
2014-12-09 13:43:40 +00:00
|
|
|
from django.test import override_settings, TestCase
|
2013-05-17 22:18:35 +00:00
|
|
|
from django.utils import six
|
|
|
|
|
|
|
|
|
2013-12-27 19:55:58 +00:00
|
|
|
APP_CONFIG = apps.get_app_config('migrate_signals')
|
2014-01-12 21:20:53 +00:00
|
|
|
PRE_MIGRATE_ARGS = ['app_config', 'verbosity', 'interactive', 'using']
|
2013-09-03 15:51:34 +00:00
|
|
|
MIGRATE_DATABASE = 'default'
|
|
|
|
MIGRATE_VERBOSITY = 1
|
|
|
|
MIGRATE_INTERACTIVE = False
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
|
2013-09-03 15:51:34 +00:00
|
|
|
class PreMigrateReceiver(object):
|
2013-05-17 22:18:35 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.call_counter = 0
|
|
|
|
self.call_args = None
|
|
|
|
|
|
|
|
def __call__(self, signal, sender, **kwargs):
|
|
|
|
self.call_counter = self.call_counter + 1
|
|
|
|
self.call_args = kwargs
|
|
|
|
|
|
|
|
|
|
|
|
class OneTimeReceiver(object):
|
|
|
|
"""
|
2013-09-03 15:51:34 +00:00
|
|
|
Special receiver for handle the fact that test runner calls migrate for
|
2013-05-17 22:18:35 +00:00
|
|
|
several databases and several times for some of them.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.call_counter = 0
|
|
|
|
self.call_args = None
|
|
|
|
|
|
|
|
def __call__(self, signal, sender, **kwargs):
|
2013-09-03 15:51:34 +00:00
|
|
|
# Although test runner calls migrate for several databases,
|
2013-05-17 22:18:35 +00:00
|
|
|
# testing for only one of them is quite sufficient.
|
2014-01-12 21:20:53 +00:00
|
|
|
if kwargs['using'] == MIGRATE_DATABASE:
|
2013-05-17 22:18:35 +00:00
|
|
|
self.call_counter = self.call_counter + 1
|
|
|
|
self.call_args = kwargs
|
2013-09-03 15:51:34 +00:00
|
|
|
# we need to test only one call of migrate
|
2013-12-27 19:55:58 +00:00
|
|
|
signals.pre_migrate.disconnect(pre_migrate_receiver, sender=APP_CONFIG)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
# We connect receiver here and not in unit test code because we need to
|
|
|
|
# connect receiver before test runner creates database. That is, sequence of
|
|
|
|
# actions would be:
|
|
|
|
#
|
|
|
|
# 1. Test runner imports this module.
|
|
|
|
# 2. We connect receiver.
|
2013-09-03 15:51:34 +00:00
|
|
|
# 3. Test runner calls migrate for create default database.
|
2013-05-17 22:18:35 +00:00
|
|
|
# 4. Test runner execute our unit test code.
|
2013-09-03 15:51:34 +00:00
|
|
|
pre_migrate_receiver = OneTimeReceiver()
|
2013-12-27 19:55:58 +00:00
|
|
|
signals.pre_migrate.connect(pre_migrate_receiver, sender=APP_CONFIG)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
|
2013-09-03 15:51:34 +00:00
|
|
|
class MigrateSignalTests(TestCase):
|
2013-06-14 21:11:51 +00:00
|
|
|
|
2013-12-27 19:55:58 +00:00
|
|
|
available_apps = ['migrate_signals']
|
2013-06-14 21:11:51 +00:00
|
|
|
|
2013-09-03 15:51:34 +00:00
|
|
|
def test_pre_migrate_call_time(self):
|
|
|
|
self.assertEqual(pre_migrate_receiver.call_counter, 1)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
2013-09-03 15:51:34 +00:00
|
|
|
def test_pre_migrate_args(self):
|
|
|
|
r = PreMigrateReceiver()
|
2013-12-27 19:55:58 +00:00
|
|
|
signals.pre_migrate.connect(r, sender=APP_CONFIG)
|
2013-09-03 15:51:34 +00:00
|
|
|
management.call_command('migrate', database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY, interactive=MIGRATE_INTERACTIVE,
|
2014-12-26 18:23:38 +00:00
|
|
|
stdout=six.StringIO())
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
args = r.call_args
|
|
|
|
self.assertEqual(r.call_counter, 1)
|
2013-09-03 15:51:34 +00:00
|
|
|
self.assertEqual(set(args), set(PRE_MIGRATE_ARGS))
|
2013-12-27 19:55:58 +00:00
|
|
|
self.assertEqual(args['app_config'], APP_CONFIG)
|
2013-09-03 15:51:34 +00:00
|
|
|
self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
|
|
|
|
self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
|
2014-01-12 21:20:53 +00:00
|
|
|
self.assertEqual(args['using'], 'default')
|
2014-12-09 13:43:40 +00:00
|
|
|
|
|
|
|
@override_settings(MIGRATION_MODULES={'migrate_signals': 'migrate_signals.custom_migrations'})
|
|
|
|
def test_pre_migrate_migrations_only(self):
|
|
|
|
"""
|
|
|
|
If all apps have migrations, pre_migrate should be sent.
|
|
|
|
"""
|
|
|
|
r = PreMigrateReceiver()
|
|
|
|
signals.pre_migrate.connect(r, sender=APP_CONFIG)
|
|
|
|
stdout = six.StringIO()
|
|
|
|
management.call_command('migrate', database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY, interactive=MIGRATE_INTERACTIVE,
|
2014-12-26 18:23:38 +00:00
|
|
|
stdout=stdout)
|
2014-12-09 13:43:40 +00:00
|
|
|
args = r.call_args
|
|
|
|
self.assertEqual(r.call_counter, 1)
|
|
|
|
self.assertEqual(set(args), set(PRE_MIGRATE_ARGS))
|
|
|
|
self.assertEqual(args['app_config'], APP_CONFIG)
|
|
|
|
self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
|
|
|
|
self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
|
|
|
|
self.assertEqual(args['using'], 'default')
|