test_backend_specific.py 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. # -*- encoding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.core.checks import Error
  4. from django.db import connections, models
  5. from django.test import SimpleTestCase, mock
  6. from django.test.utils import isolate_apps
  7. def dummy_allow_migrate(db, app_label, **hints):
  8. # Prevent checks from being run on the 'other' database, which doesn't have
  9. # its check_field() method mocked in the test.
  10. return db == 'default'
  11. @isolate_apps('invalid_models_tests')
  12. class BackendSpecificChecksTests(SimpleTestCase):
  13. @mock.patch('django.db.models.fields.router.allow_migrate', new=dummy_allow_migrate)
  14. def test_check_field(self):
  15. """ Test if backend specific checks are performed. """
  16. error = Error('an error')
  17. class Model(models.Model):
  18. field = models.IntegerField()
  19. field = Model._meta.get_field('field')
  20. with mock.patch.object(connections['default'].validation, 'check_field', return_value=[error]):
  21. errors = field.check()
  22. self.assertEqual(errors, [error])