1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
# -*- 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 .base import IsolatedModelsTestCase
class BackendSpecificChecksTests(IsolatedModelsTestCase):
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:
errors = field.check()
finally:
# Unmock connection.validation.check_field method.
v.check_field = old_check_field
self.assertEqual(errors, [error])
def test_validate_field(self):
""" Errors raised by deprecated `validate_field` method should be
collected. """
def mock(self, errors, opts, field):
errors.add(opts, "An error!")
class Model(models.Model):
field = models.IntegerField()
field = Model._meta.get_field('field')
expected = [
Error(
"An error!",
hint=None,
obj=field,
)
]
# Mock connection.validation.validate_field method.
v = connection.validation
old_validate_field = v.validate_field
v.validate_field = MethodType(mock, v)
try:
errors = field.check()
finally:
# Unmock connection.validation.validate_field method.
v.validate_field = old_validate_field
self.assertEqual(errors, expected)
|