2013-05-17 21:31:41 +02:00
|
|
|
from django.core import management
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.core.checks import Error, run_checks
|
2013-11-05 05:11:51 +01:00
|
|
|
from django.db.models.signals import post_init
|
2015-04-17 23:38:20 +02:00
|
|
|
from django.test import SimpleTestCase
|
2014-09-08 19:38:07 +02:00
|
|
|
from django.test.utils import override_settings
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.utils import six
|
2013-05-17 21:31:41 +02:00
|
|
|
|
|
|
|
|
2013-11-05 05:11:51 +01:00
|
|
|
class OnPostInit(object):
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def on_post_init(**kwargs):
|
|
|
|
pass
|
2013-05-17 21:31:41 +02:00
|
|
|
|
2013-11-05 05:11:51 +01:00
|
|
|
|
2014-09-08 19:38:07 +02:00
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes'],
|
|
|
|
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
|
|
|
|
)
|
2015-04-17 23:38:20 +02:00
|
|
|
class ModelValidationTest(SimpleTestCase):
|
2013-05-17 21:31:41 +02:00
|
|
|
def test_models_validate(self):
|
|
|
|
# All our models should validate properly
|
|
|
|
# Validation Tests:
|
|
|
|
# * choices= Iterable of Iterables
|
|
|
|
# See: https://code.djangoproject.com/ticket/20430
|
2013-11-02 20:16:33 +01:00
|
|
|
# * related_name='+' doesn't clash with another '+'
|
|
|
|
# See: https://code.djangoproject.com/ticket/21375
|
2014-01-20 03:45:21 +01:00
|
|
|
management.call_command("check", stdout=six.StringIO())
|
2013-11-05 05:11:51 +01:00
|
|
|
|
|
|
|
def test_model_signal(self):
|
|
|
|
unresolved_references = post_init.unresolved_references.copy()
|
|
|
|
post_init.connect(on_post_init, sender='missing-app.Model')
|
|
|
|
post_init.connect(OnPostInit(), sender='missing-app.Model')
|
2014-01-20 03:45:21 +01:00
|
|
|
|
|
|
|
errors = run_checks()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 12:29:50 +01:00
|
|
|
"The 'on_post_init' function was connected to the 'post_init' "
|
2013-11-05 05:11:51 +01:00
|
|
|
"signal with a lazy reference to the 'missing-app.Model' "
|
2014-01-20 03:45:21 +01:00
|
|
|
"sender, which has not been installed.",
|
|
|
|
hint=None,
|
|
|
|
obj='model_validation.tests',
|
2014-03-03 11:18:39 +01:00
|
|
|
id='signals.E001',
|
2014-01-20 03:45:21 +01:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 12:29:50 +01:00
|
|
|
"An instance of the 'OnPostInit' class was connected to "
|
|
|
|
"the 'post_init' signal with a lazy reference to the "
|
2014-01-20 03:45:21 +01:00
|
|
|
"'missing-app.Model' sender, which has not been installed.",
|
|
|
|
hint=None,
|
|
|
|
obj='model_validation.tests',
|
2014-03-03 11:18:39 +01:00
|
|
|
id='signals.E001',
|
2014-01-20 03:45:21 +01:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2013-11-05 05:11:51 +01:00
|
|
|
post_init.unresolved_references = unresolved_references
|