tests.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from django.core import management
  2. from django.core.management.validation import (
  3. ModelErrorCollection, validate_model_signals
  4. )
  5. from django.db.models.signals import post_init
  6. from django.test import TestCase
  7. from django.utils import six
  8. class OnPostInit(object):
  9. def __call__(self, **kwargs):
  10. pass
  11. def on_post_init(**kwargs):
  12. pass
  13. class ModelValidationTest(TestCase):
  14. def test_models_validate(self):
  15. # All our models should validate properly
  16. # Validation Tests:
  17. # * choices= Iterable of Iterables
  18. # See: https://code.djangoproject.com/ticket/20430
  19. # * related_name='+' doesn't clash with another '+'
  20. # See: https://code.djangoproject.com/ticket/21375
  21. management.call_command("validate", stdout=six.StringIO())
  22. def test_model_signal(self):
  23. unresolved_references = post_init.unresolved_references.copy()
  24. post_init.connect(on_post_init, sender='missing-app.Model')
  25. post_init.connect(OnPostInit(), sender='missing-app.Model')
  26. e = ModelErrorCollection(six.StringIO())
  27. validate_model_signals(e)
  28. self.assertSetEqual(set(e.errors), {
  29. ('model_validation.tests',
  30. "The `on_post_init` function was connected to the `post_init` "
  31. "signal with a lazy reference to the 'missing-app.Model' "
  32. "sender, which has not been installed."
  33. ),
  34. ('model_validation.tests',
  35. "An instance of the `OnPostInit` class was connected to "
  36. "the `post_init` signal with a lazy reference to the "
  37. "'missing-app.Model' sender, which has not been installed."
  38. )
  39. })
  40. post_init.unresolved_references = unresolved_references