2013-05-17 19:31:41 +00:00
|
|
|
from django.core import management
|
2016-03-30 08:34:44 +00:00
|
|
|
from django.core.checks import Error
|
2016-05-18 15:18:40 +00:00
|
|
|
from django.core.checks.model_checks import _check_lazy_references
|
|
|
|
from django.db import models
|
2013-11-05 04:11:51 +00:00
|
|
|
from django.db.models.signals import post_init
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase
|
2016-05-18 15:18:40 +00:00
|
|
|
from django.test.utils import isolate_apps, override_settings
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils import six
|
2013-05-17 19:31:41 +00:00
|
|
|
|
|
|
|
|
2014-09-08 17:38:07 +00:00
|
|
|
@override_settings(
|
|
|
|
INSTALLED_APPS=['django.contrib.auth', 'django.contrib.contenttypes'],
|
|
|
|
SILENCED_SYSTEM_CHECKS=['fields.W342'], # ForeignKey(unique=True)
|
|
|
|
)
|
2015-04-17 21:38:20 +00:00
|
|
|
class ModelValidationTest(SimpleTestCase):
|
2013-05-17 19:31:41 +00: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 19:16:33 +00:00
|
|
|
# * related_name='+' doesn't clash with another '+'
|
|
|
|
# See: https://code.djangoproject.com/ticket/21375
|
2014-01-20 02:45:21 +00:00
|
|
|
management.call_command("check", stdout=six.StringIO())
|
2013-11-05 04:11:51 +00:00
|
|
|
|
2016-05-18 15:18:40 +00:00
|
|
|
@isolate_apps('django.contrib.auth', kwarg_name='apps')
|
|
|
|
def test_lazy_reference_checks(self, apps):
|
|
|
|
|
|
|
|
class DummyModel(models.Model):
|
|
|
|
author = models.ForeignKey('Author', models.CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = "model_validation"
|
|
|
|
|
2016-03-30 08:34:44 +00:00
|
|
|
class DummyClass(object):
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def dummy_method(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def dummy_function(*args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
2016-05-18 15:18:40 +00:00
|
|
|
apps.lazy_model_operation(dummy_function, ('auth', 'imaginarymodel'))
|
|
|
|
apps.lazy_model_operation(dummy_function, ('fanciful_app', 'imaginarymodel'))
|
|
|
|
|
2016-03-30 08:34:44 +00:00
|
|
|
post_init.connect(dummy_function, sender='missing-app.Model', apps=apps)
|
|
|
|
post_init.connect(DummyClass(), sender='missing-app.Model', apps=apps)
|
|
|
|
post_init.connect(DummyClass().dummy_method, sender='missing-app.Model', apps=apps)
|
2016-05-18 15:18:40 +00:00
|
|
|
|
2016-03-30 08:34:44 +00:00
|
|
|
errors = _check_lazy_references(apps)
|
2016-05-18 15:18:40 +00:00
|
|
|
expected = [
|
|
|
|
Error(
|
|
|
|
"%r contains a lazy reference to auth.imaginarymodel, "
|
|
|
|
"but app 'auth' doesn't provide model 'imaginarymodel'." % dummy_function,
|
|
|
|
obj=dummy_function,
|
|
|
|
id='models.E022',
|
|
|
|
),
|
|
|
|
Error(
|
|
|
|
"%r contains a lazy reference to fanciful_app.imaginarymodel, "
|
|
|
|
"but app 'fanciful_app' isn't installed." % dummy_function,
|
|
|
|
obj=dummy_function,
|
|
|
|
id='models.E022',
|
|
|
|
),
|
2016-03-30 08:34:44 +00:00
|
|
|
Error(
|
|
|
|
"An instance of class 'DummyClass' was connected to "
|
|
|
|
"the 'post_init' signal with a lazy reference to the sender "
|
|
|
|
"'missing-app.model', but app 'missing-app' isn't installed.",
|
|
|
|
hint=None,
|
|
|
|
obj='model_validation.tests',
|
|
|
|
id='signals.E001',
|
|
|
|
),
|
|
|
|
Error(
|
|
|
|
"Bound method 'DummyClass.dummy_method' was connected to the "
|
|
|
|
"'post_init' signal with a lazy reference to the sender "
|
|
|
|
"'missing-app.model', but app 'missing-app' isn't installed.",
|
|
|
|
hint=None,
|
|
|
|
obj='model_validation.tests',
|
|
|
|
id='signals.E001',
|
|
|
|
),
|
2016-05-18 15:18:40 +00:00
|
|
|
Error(
|
|
|
|
"The field model_validation.DummyModel.author was declared "
|
|
|
|
"with a lazy reference to 'model_validation.author', but app "
|
|
|
|
"'model_validation' isn't installed.",
|
|
|
|
hint=None,
|
|
|
|
obj=DummyModel.author.field,
|
|
|
|
id='fields.E307',
|
|
|
|
),
|
2016-03-30 08:34:44 +00:00
|
|
|
Error(
|
|
|
|
"The function 'dummy_function' was connected to the 'post_init' "
|
|
|
|
"signal with a lazy reference to the sender "
|
|
|
|
"'missing-app.model', but app 'missing-app' isn't installed.",
|
|
|
|
hint=None,
|
|
|
|
obj='model_validation.tests',
|
|
|
|
id='signals.E001',
|
|
|
|
),
|
2016-05-18 15:18:40 +00:00
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|