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