1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #25172 -- Fixed check framework to work with multiple databases.

This commit is contained in:
Ion Scerbatiuc
2015-08-01 07:46:25 -07:00
committed by Tim Graham
parent d0bd533043
commit 0cc059cd10
4 changed files with 70 additions and 24 deletions

View File

@@ -1,37 +1,31 @@
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
from types import MethodType
from django.core.checks import Error
from django.db import connection, models
from django.db import connections, models
from django.test import mock
from .base import IsolatedModelsTestCase
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'
class BackendSpecificChecksTests(IsolatedModelsTestCase):
@mock.patch('django.db.models.fields.router.allow_migrate', new=dummy_allow_migrate)
def test_check_field(self):
""" Test if backend specific checks are performed. """
error = Error('an error', hint=None)
def mock(self, field, **kwargs):
return [error]
class Model(models.Model):
field = models.IntegerField()
field = Model._meta.get_field('field')
# Mock connection.validation.check_field method.
v = connection.validation
old_check_field = v.check_field
v.check_field = MethodType(mock, v)
try:
with mock.patch.object(connections['default'].validation, 'check_field', return_value=[error]):
errors = field.check()
finally:
# Unmock connection.validation.check_field method.
v.check_field = old_check_field
self.assertEqual(errors, [error])