2013-05-18 03:31:41 +08:00
|
|
|
from django.core import management
|
2013-11-05 12:11:51 +08:00
|
|
|
from django.core.management.validation import (
|
|
|
|
ModelErrorCollection, validate_model_signals
|
|
|
|
)
|
|
|
|
from django.db.models.signals import post_init
|
2013-05-18 03:31:41 +08:00
|
|
|
from django.test import TestCase
|
2013-05-19 00:06:31 +08:00
|
|
|
from django.utils import six
|
2013-05-18 03:31:41 +08:00
|
|
|
|
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
class OnPostInit(object):
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def on_post_init(**kwargs):
|
|
|
|
pass
|
2013-05-18 03:31:41 +08:00
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
|
|
|
|
class ModelValidationTest(TestCase):
|
2013-05-18 03:31:41 +08: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-03 03:16:33 +08:00
|
|
|
# * related_name='+' doesn't clash with another '+'
|
|
|
|
# See: https://code.djangoproject.com/ticket/21375
|
2013-05-19 00:06:31 +08:00
|
|
|
management.call_command("validate", stdout=six.StringIO())
|
2013-11-05 12:11:51 +08: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')
|
|
|
|
e = ModelErrorCollection(six.StringIO())
|
|
|
|
validate_model_signals(e)
|
|
|
|
self.assertSetEqual(set(e.errors), {
|
|
|
|
('model_validation.tests',
|
|
|
|
"The `on_post_init` function was connected to the `post_init` "
|
|
|
|
"signal with a lazy reference to the 'missing-app.Model' "
|
|
|
|
"sender, which has not been installed."
|
|
|
|
),
|
|
|
|
('model_validation.tests',
|
|
|
|
"An instance of the `OnPostInit` class was connected to "
|
|
|
|
"the `post_init` signal with a lazy reference to the "
|
|
|
|
"'missing-app.Model' sender, which has not been installed."
|
|
|
|
)
|
|
|
|
})
|
|
|
|
post_init.unresolved_references = unresolved_references
|