2021-01-29 15:19:06 +00:00
|
|
|
from io import StringIO
|
|
|
|
|
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
|
2016-05-13 15:58:54 +00:00
|
|
|
from django.db import migrations
|
2013-05-17 22:18:35 +00:00
|
|
|
from django.db.models import signals
|
2016-05-19 13:10:47 +00:00
|
|
|
from django.test import TransactionTestCase, override_settings
|
2013-05-17 22:18:35 +00:00
|
|
|
|
2013-12-27 19:55:58 +00:00
|
|
|
APP_CONFIG = apps.get_app_config("migrate_signals")
|
2021-01-29 15:19:06 +00:00
|
|
|
SIGNAL_ARGS = [
|
|
|
|
"app_config",
|
|
|
|
"verbosity",
|
|
|
|
"interactive",
|
|
|
|
"using",
|
|
|
|
"stdout",
|
|
|
|
"plan",
|
|
|
|
"apps",
|
|
|
|
]
|
2013-09-03 15:51:34 +00:00
|
|
|
MIGRATE_DATABASE = "default"
|
2020-05-13 07:12:43 +00:00
|
|
|
MIGRATE_VERBOSITY = 0
|
2013-09-03 15:51:34 +00:00
|
|
|
MIGRATE_INTERACTIVE = False
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class Receiver:
|
2016-05-12 00:48:25 +00:00
|
|
|
def __init__(self, signal):
|
2013-05-17 22:18:35 +00:00
|
|
|
self.call_counter = 0
|
|
|
|
self.call_args = None
|
2016-05-12 00:48:25 +00:00
|
|
|
signal.connect(self, sender=APP_CONFIG)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
def __call__(self, signal, sender, **kwargs):
|
2016-07-21 08:28:40 +00:00
|
|
|
self.call_counter += 1
|
2013-05-17 22:18:35 +00:00
|
|
|
self.call_args = kwargs
|
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class OneTimeReceiver:
|
2013-05-17 22:18:35 +00:00
|
|
|
"""
|
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.
|
|
|
|
"""
|
|
|
|
|
2016-05-12 00:48:25 +00:00
|
|
|
def __init__(self, signal):
|
|
|
|
self.signal = signal
|
2013-05-17 22:18:35 +00:00
|
|
|
self.call_counter = 0
|
|
|
|
self.call_args = None
|
2016-05-12 00:48:25 +00:00
|
|
|
self.signal.connect(self, sender=APP_CONFIG)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
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:
|
2016-07-21 08:28:40 +00:00
|
|
|
self.call_counter += 1
|
2013-05-17 22:18:35 +00:00
|
|
|
self.call_args = kwargs
|
2013-09-03 15:51:34 +00:00
|
|
|
# we need to test only one call of migrate
|
2016-05-12 00:48:25 +00:00
|
|
|
self.signal.disconnect(self, 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.
|
2016-05-12 00:48:25 +00:00
|
|
|
pre_migrate_receiver = OneTimeReceiver(signals.pre_migrate)
|
|
|
|
post_migrate_receiver = OneTimeReceiver(signals.post_migrate)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
|
|
|
|
2016-05-19 13:10:47 +00:00
|
|
|
class MigrateSignalTests(TransactionTestCase):
|
2013-12-27 19:55:58 +00:00
|
|
|
available_apps = ["migrate_signals"]
|
2013-06-14 21:11:51 +00:00
|
|
|
|
2016-05-12 00:48:25 +00:00
|
|
|
def test_call_time(self):
|
2013-09-03 15:51:34 +00:00
|
|
|
self.assertEqual(pre_migrate_receiver.call_counter, 1)
|
2016-05-12 00:48:25 +00:00
|
|
|
self.assertEqual(post_migrate_receiver.call_counter, 1)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
2016-05-12 00:48:25 +00:00
|
|
|
def test_args(self):
|
|
|
|
pre_migrate_receiver = Receiver(signals.pre_migrate)
|
|
|
|
post_migrate_receiver = Receiver(signals.post_migrate)
|
2016-04-08 02:04:45 +00:00
|
|
|
management.call_command(
|
|
|
|
"migrate",
|
|
|
|
database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY,
|
2021-01-29 15:19:06 +00:00
|
|
|
interactive=MIGRATE_INTERACTIVE,
|
|
|
|
stdout=StringIO("test_args"),
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2013-05-17 22:18:35 +00:00
|
|
|
|
2016-05-12 00:48:25 +00:00
|
|
|
for receiver in [pre_migrate_receiver, post_migrate_receiver]:
|
2021-02-04 05:30:53 +00:00
|
|
|
with self.subTest(receiver=receiver):
|
|
|
|
args = receiver.call_args
|
|
|
|
self.assertEqual(receiver.call_counter, 1)
|
|
|
|
self.assertEqual(set(args), set(SIGNAL_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")
|
2021-01-29 15:19:06 +00:00
|
|
|
self.assertIn("test_args", args["stdout"].getvalue())
|
2021-02-04 05:30:53 +00:00
|
|
|
self.assertEqual(args["plan"], [])
|
|
|
|
self.assertIsInstance(args["apps"], migrations.state.StateApps)
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2014-12-09 13:43:40 +00:00
|
|
|
@override_settings(
|
|
|
|
MIGRATION_MODULES={"migrate_signals": "migrate_signals.custom_migrations"}
|
|
|
|
)
|
2016-05-12 00:48:25 +00:00
|
|
|
def test_migrations_only(self):
|
2014-12-09 13:43:40 +00:00
|
|
|
"""
|
2016-05-12 00:48:25 +00:00
|
|
|
If all apps have migrations, migration signals should be sent.
|
2014-12-09 13:43:40 +00:00
|
|
|
"""
|
2016-05-12 00:48:25 +00:00
|
|
|
pre_migrate_receiver = Receiver(signals.pre_migrate)
|
|
|
|
post_migrate_receiver = Receiver(signals.post_migrate)
|
2016-04-08 02:04:45 +00:00
|
|
|
management.call_command(
|
|
|
|
"migrate",
|
|
|
|
database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY,
|
2020-05-13 07:12:43 +00:00
|
|
|
interactive=MIGRATE_INTERACTIVE,
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2016-05-12 00:48:25 +00:00
|
|
|
for receiver in [pre_migrate_receiver, post_migrate_receiver]:
|
|
|
|
args = receiver.call_args
|
|
|
|
self.assertEqual(receiver.call_counter, 1)
|
|
|
|
self.assertEqual(set(args), set(SIGNAL_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")
|
2016-05-13 15:58:54 +00:00
|
|
|
self.assertIsInstance(args["plan"][0][0], migrations.Migration)
|
|
|
|
# The migration isn't applied backward.
|
|
|
|
self.assertFalse(args["plan"][0][1])
|
|
|
|
self.assertIsInstance(args["apps"], migrations.state.StateApps)
|
|
|
|
self.assertEqual(pre_migrate_receiver.call_args["apps"].get_models(), [])
|
|
|
|
self.assertEqual(
|
|
|
|
[
|
|
|
|
model._meta.label
|
|
|
|
for model in post_migrate_receiver.call_args["apps"].get_models()
|
|
|
|
],
|
|
|
|
["migrate_signals.Signal"],
|
|
|
|
)
|
2016-08-20 20:34:06 +00:00
|
|
|
# Migrating with an empty plan.
|
2016-08-20 22:05:04 +00:00
|
|
|
pre_migrate_receiver = Receiver(signals.pre_migrate)
|
2016-08-20 20:34:06 +00:00
|
|
|
post_migrate_receiver = Receiver(signals.post_migrate)
|
|
|
|
management.call_command(
|
|
|
|
"migrate",
|
|
|
|
database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY,
|
2020-05-13 07:12:43 +00:00
|
|
|
interactive=MIGRATE_INTERACTIVE,
|
2016-08-20 20:34:06 +00:00
|
|
|
)
|
2016-08-20 22:05:04 +00:00
|
|
|
self.assertEqual(
|
|
|
|
[
|
|
|
|
model._meta.label
|
|
|
|
for model in pre_migrate_receiver.call_args["apps"].get_models()
|
|
|
|
],
|
|
|
|
["migrate_signals.Signal"],
|
|
|
|
)
|
2016-08-20 20:34:06 +00:00
|
|
|
self.assertEqual(
|
|
|
|
[
|
|
|
|
model._meta.label
|
|
|
|
for model in post_migrate_receiver.call_args["apps"].get_models()
|
|
|
|
],
|
|
|
|
["migrate_signals.Signal"],
|
|
|
|
)
|
2022-09-27 12:10:18 +00:00
|
|
|
# Migrating with an empty plan and --check doesn't emit signals.
|
|
|
|
pre_migrate_receiver = Receiver(signals.pre_migrate)
|
|
|
|
post_migrate_receiver = Receiver(signals.post_migrate)
|
|
|
|
management.call_command(
|
|
|
|
"migrate",
|
|
|
|
database=MIGRATE_DATABASE,
|
|
|
|
verbosity=MIGRATE_VERBOSITY,
|
|
|
|
interactive=MIGRATE_INTERACTIVE,
|
|
|
|
check_unapplied=True,
|
|
|
|
)
|
|
|
|
self.assertEqual(pre_migrate_receiver.call_counter, 0)
|
|
|
|
self.assertEqual(post_migrate_receiver.call_counter, 0)
|