tests.py 1.84 KB
Newer Older
1
from django.core import management
2
from django.core.checks import run_checks, Error
3
from django.db.models.signals import post_init
4
from django.test import TestCase
5
from django.utils import six
6 7


8 9 10 11 12 13 14
class OnPostInit(object):
    def __call__(self, **kwargs):
        pass


def on_post_init(**kwargs):
    pass
15

16 17

class ModelValidationTest(TestCase):
18 19 20 21 22
    def test_models_validate(self):
        # All our models should validate properly
        # Validation Tests:
        #   * choices= Iterable of Iterables
        #       See: https://code.djangoproject.com/ticket/20430
23 24
        #   * related_name='+' doesn't clash with another '+'
        #       See: https://code.djangoproject.com/ticket/21375
25
        management.call_command("check", stdout=six.StringIO())
26 27 28 29 30

    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')
31 32 33 34

        errors = run_checks()
        expected = [
            Error(
35
                "The 'on_post_init' function was connected to the 'post_init' "
36
                "signal with a lazy reference to the 'missing-app.Model' "
37 38 39
                "sender, which has not been installed.",
                hint=None,
                obj='model_validation.tests',
40
                id='signals.E001',
41 42
            ),
            Error(
43 44
                "An instance of the 'OnPostInit' class was connected to "
                "the 'post_init' signal with a lazy reference to the "
45 46 47
                "'missing-app.Model' sender, which has not been installed.",
                hint=None,
                obj='model_validation.tests',
48
                id='signals.E001',
49 50 51 52
            )
        ]
        self.assertEqual(errors, expected)

53
        post_init.unresolved_references = unresolved_references