2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
from django.core.checks import Error
|
2015-08-01 22:46:25 +08:00
|
|
|
from django.db import connections, models
|
2017-01-20 01:16:04 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import isolate_apps
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-08-01 22:46:25 +08:00
|
|
|
def dummy_allow_migrate(db, app_label, **hints):
|
|
|
|
# Prevent checks from being run on the 'other' database, which doesn't have
|
|
|
|
# its check_field() method mocked in the test.
|
|
|
|
return db == "default"
|
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
class BackendSpecificChecksTests(SimpleTestCase):
|
2015-08-01 22:46:25 +08:00
|
|
|
@mock.patch("django.db.models.fields.router.allow_migrate", new=dummy_allow_migrate)
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_check_field(self):
|
|
|
|
"""Test if backend specific checks are performed."""
|
2016-02-13 00:36:46 +08:00
|
|
|
error = Error("an error")
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.IntegerField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field("field")
|
2015-08-01 22:46:25 +08:00
|
|
|
with mock.patch.object(
|
|
|
|
connections["default"].validation, "check_field", return_value=[error]
|
|
|
|
):
|
2020-02-19 19:14:54 +08:00
|
|
|
self.assertEqual(field.check(databases={"default"}), [error])
|