2017-01-19 18:16:04 +01:00
|
|
|
from unittest import mock
|
|
|
|
|
2014-01-20 03:45:21 +01:00
|
|
|
from django.core.checks import Error
|
2015-08-01 16:46:25 +02:00
|
|
|
from django.db import connections, models
|
2017-01-19 18:16:04 +01:00
|
|
|
from django.test import SimpleTestCase
|
2015-11-17 06:39:28 +01:00
|
|
|
from django.test.utils import isolate_apps
|
2014-01-20 03:45:21 +01:00
|
|
|
|
|
|
|
|
2015-08-01 16:46:25 +02: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 06:39:28 +01:00
|
|
|
@isolate_apps("invalid_models_tests")
|
|
|
|
class BackendSpecificChecksTests(SimpleTestCase):
|
2015-08-01 16:46:25 +02:00
|
|
|
@mock.patch("django.db.models.fields.router.allow_migrate", new=dummy_allow_migrate)
|
2014-01-20 03:45:21 +01:00
|
|
|
def test_check_field(self):
|
|
|
|
"""Test if backend specific checks are performed."""
|
2016-02-12 17:36:46 +01:00
|
|
|
error = Error("an error")
|
2014-01-20 03:45:21 +01:00
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.IntegerField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field("field")
|
2015-08-01 16:46:25 +02:00
|
|
|
with mock.patch.object(
|
|
|
|
connections["default"].validation, "check_field", return_value=[error]
|
|
|
|
):
|
2020-02-19 12:14:54 +01:00
|
|
|
self.assertEqual(field.check(databases={"default"}), [error])
|