2014-01-20 10:45:21 +08:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.core.checks import Error
|
2015-08-01 22:46:25 +08:00
|
|
|
from django.db import connections, models
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test import SimpleTestCase, mock
|
|
|
|
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):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
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. """
|
|
|
|
error = Error('an error', hint=None)
|
|
|
|
|
|
|
|
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]):
|
2014-01-20 10:45:21 +08:00
|
|
|
errors = field.check()
|
|
|
|
|
|
|
|
self.assertEqual(errors, [error])
|