summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests/test_backend_specific.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/invalid_models_tests/test_backend_specific.py')
-rw-r--r--tests/invalid_models_tests/test_backend_specific.py26
1 files changed, 10 insertions, 16 deletions
diff --git a/tests/invalid_models_tests/test_backend_specific.py b/tests/invalid_models_tests/test_backend_specific.py
index 2d3ea7e6c7..5be1d6f691 100644
--- a/tests/invalid_models_tests/test_backend_specific.py
+++ b/tests/invalid_models_tests/test_backend_specific.py
@@ -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])