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