mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Moved model_validation tests to invalid_models_tests.
This commit is contained in:
@@ -6,7 +6,9 @@ import warnings
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.checks import Error
|
||||
from django.core.checks.model_checks import _check_lazy_references
|
||||
from django.db import connections, models
|
||||
from django.db.models.signals import post_init
|
||||
from django.test import SimpleTestCase
|
||||
from django.test.utils import isolate_apps, override_settings
|
||||
|
||||
@@ -827,3 +829,76 @@ class OtherModelTests(SimpleTestCase):
|
||||
id='fields.E340',
|
||||
)
|
||||
])
|
||||
|
||||
@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 = 'invalid_models_tests'
|
||||
|
||||
class DummyClass(object):
|
||||
def __call__(self, **kwargs):
|
||||
pass
|
||||
|
||||
def dummy_method(self):
|
||||
pass
|
||||
|
||||
def dummy_function(*args, **kwargs):
|
||||
pass
|
||||
|
||||
apps.lazy_model_operation(dummy_function, ('auth', 'imaginarymodel'))
|
||||
apps.lazy_model_operation(dummy_function, ('fanciful_app', 'imaginarymodel'))
|
||||
|
||||
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)
|
||||
|
||||
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',
|
||||
),
|
||||
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='invalid_models_tests.test_models',
|
||||
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='invalid_models_tests.test_models',
|
||||
id='signals.E001',
|
||||
),
|
||||
Error(
|
||||
"The field invalid_models_tests.DummyModel.author was declared "
|
||||
"with a lazy reference to 'invalid_models_tests.author', but app "
|
||||
"'invalid_models_tests' isn't installed.",
|
||||
hint=None,
|
||||
obj=DummyModel.author.field,
|
||||
id='fields.E307',
|
||||
),
|
||||
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='invalid_models_tests.test_models',
|
||||
id='signals.E001',
|
||||
),
|
||||
]
|
||||
self.assertEqual(_check_lazy_references(apps), expected)
|
||||
|
||||
Reference in New Issue
Block a user