forked from django/django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #25172 -- Fixed check framework to work with multiple databases.
- Loading branch information
1 parent
d0bd533
commit 0cc059c
Showing
4 changed files
with
70 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.db import connections, models | ||
from django.test import TestCase, mock | ||
from django.test.utils import override_settings | ||
|
||
from .tests import IsolateModelsMixin | ||
|
||
|
||
class TestRouter(object): | ||
""" | ||
Routes to the 'other' database if the model name starts with 'Other'. | ||
""" | ||
def allow_migrate(self, db, app_label, model=None, **hints): | ||
return db == ('other' if model._meta.verbose_name.startswith('other') else 'default') | ||
|
||
|
||
@override_settings(DATABASE_ROUTERS=[TestRouter()]) | ||
class TestMultiDBChecks(IsolateModelsMixin, TestCase): | ||
multi_db = True | ||
|
||
def _patch_check_field_on(self, db): | ||
return mock.patch.object(connections[db].validation, 'check_field') | ||
|
||
def test_checks_called_on_the_default_database(self): | ||
class Model(models.Model): | ||
field = models.CharField(max_length=100) | ||
|
||
model = Model() | ||
with self._patch_check_field_on('default') as mock_check_field_default: | ||
with self._patch_check_field_on('other') as mock_check_field_other: | ||
model.check() | ||
self.assertTrue(mock_check_field_default.called) | ||
self.assertFalse(mock_check_field_other.called) | ||
|
||
def test_checks_called_on_the_other_database(self): | ||
class OtherModel(models.Model): | ||
field = models.CharField(max_length=100) | ||
|
||
model = OtherModel() | ||
with self._patch_check_field_on('other') as mock_check_field_other: | ||
with self._patch_check_field_on('default') as mock_check_field_default: | ||
model.check() | ||
self.assertTrue(mock_check_field_other.called) | ||
self.assertFalse(mock_check_field_default.called) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters