Ticket #11595: fixture_validationv2.diff

File fixture_validationv2.diff, 7.7 KB (added by Anand Kumria, 13 years ago)

updated version of this patch

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 9037265..98098ac 100644
    a b class AutoField(Field):  
    459459
    460460    empty_strings_allowed = False
    461461    default_error_messages = {
    462         'invalid': _(u'This value must be an integer.'),
     462        'invalid': _(u"'%s' value must be an integer."),
    463463    }
    464464    def __init__(self, *args, **kwargs):
    465465        assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__
    class AutoField(Field):  
    475475        try:
    476476            return int(value)
    477477        except (TypeError, ValueError):
    478             raise exceptions.ValidationError(self.error_messages['invalid'])
     478            msg = self.error_messages['invalid'] % str(value)
     479            raise exceptions.ValidationError(msg)
    479480
    480481    def validate(self, value, model_instance):
    481482        pass
    class AutoField(Field):  
    497498class BooleanField(Field):
    498499    empty_strings_allowed = False
    499500    default_error_messages = {
    500         'invalid': _(u'This value must be either True or False.'),
     501        'invalid': _(u"'%s' value must be either True or False."),
    501502    }
    502503    description = _("Boolean (Either True or False)")
    503504    def __init__(self, *args, **kwargs):
    class BooleanField(Field):  
    518519            return True
    519520        if value in ('f', 'False', '0'):
    520521            return False
    521         raise exceptions.ValidationError(self.error_messages['invalid'])
     522        msg = self.error_messages['invalid'] % str(value)
     523        raise exceptions.ValidationError(msg)
    522524
    523525    def get_prep_lookup(self, lookup_type, value):
    524526        # Special-case handling for filters coming from a Web request (e.g. the
    class DateTimeField(DateField):  
    750752class DecimalField(Field):
    751753    empty_strings_allowed = False
    752754    default_error_messages = {
    753         'invalid': _(u'This value must be a decimal number.'),
     755        'invalid': _(u"'%s' value must be a decimal number."),
    754756    }
    755757    description = _("Decimal number")
    756758
    class DecimalField(Field):  
    767769        try:
    768770            return decimal.Decimal(value)
    769771        except decimal.InvalidOperation:
    770             raise exceptions.ValidationError(self.error_messages['invalid'])
     772            msg = self.error_messages['invalid'] % str(value)
     773            raise exceptions.ValidationError(msg)
    771774
    772775    def _format(self, value):
    773776        if isinstance(value, basestring) or value is None:
    class FilePathField(Field):  
    845848class FloatField(Field):
    846849    empty_strings_allowed = False
    847850    default_error_messages = {
    848         'invalid': _("This value must be a float."),
     851        'invalid': _("'%s' value must be a float."),
    849852    }
    850853    description = _("Floating point number")
    851854
    class FloatField(Field):  
    863866        try:
    864867            return float(value)
    865868        except (TypeError, ValueError):
    866             raise exceptions.ValidationError(self.error_messages['invalid'])
     869            msg = self.error_messages['invalid'] % str(value)
     870            raise exceptions.ValidationError(msg)
    867871
    868872    def formfield(self, **kwargs):
    869873        defaults = {'form_class': forms.FloatField}
    class FloatField(Field):  
    873877class IntegerField(Field):
    874878    empty_strings_allowed = False
    875879    default_error_messages = {
    876         'invalid': _("This value must be an integer."),
     880        'invalid': _("'%s' value must be an integer."),
    877881    }
    878882    description = _("Integer")
    879883
    class IntegerField(Field):  
    897901        try:
    898902            return int(value)
    899903        except (TypeError, ValueError):
    900             raise exceptions.ValidationError(self.error_messages['invalid'])
     904            msg = self.error_messages['invalid'] % str(value)
     905            raise exceptions.ValidationError(msg)
    901906
    902907    def formfield(self, **kwargs):
    903908        defaults = {'form_class': forms.IntegerField}
    class IPAddressField(Field):  
    935940class NullBooleanField(Field):
    936941    empty_strings_allowed = False
    937942    default_error_messages = {
    938         'invalid': _("This value must be either None, True or False."),
     943        'invalid': _("'%s' value must be either None, True or False."),
    939944    }
    940945    description = _("Boolean (Either True, False or None)")
    941946
    class NullBooleanField(Field):  
    958963            return True
    959964        if value in ('f', 'False', '0'):
    960965            return False
    961         raise exceptions.ValidationError(self.error_messages['invalid'])
     966        msg = self.error_messages['invalid'] % str(value)
     967        raise exceptions.ValidationError(msg)
    962968
    963969    def get_prep_lookup(self, lookup_type, value):
    964970        # Special-case handling for filters coming from a Web request (e.g. the
  • new file tests/modeltests/validation/test_error_messages.py

    diff --git a/tests/modeltests/validation/test_error_messages.py b/tests/modeltests/validation/test_error_messages.py
    new file mode 100644
    index 0000000..45d0327
    - +  
     1from django.core.exceptions import ValidationError
     2from django.utils.unittest import TestCase
     3from django.db import models
     4
     5
     6class ValidationMessagesTest(TestCase):
     7
     8    def test_autofield_field_raises_error_message(self):
     9        f = models.AutoField(primary_key=True)
     10        self.assertRaises(ValidationError, f.clean, 'foo', None)
     11        try:
     12            f.clean('foo', None)
     13        except ValidationError, e:
     14            self.assertEqual(e.messages, [u"'foo' value must be an integer."])
     15
     16    def test_integer_field_raises_error_message(self):
     17        f = models.IntegerField()
     18        self.assertRaises(ValidationError, f.clean, 'foo', None)
     19        try:
     20            f.clean('foo', None)
     21        except ValidationError, e:
     22            self.assertEqual(e.messages, [u"'foo' value must be an integer."])
     23
     24    def test_boolean_field_raises_error_message(self):
     25        f = models.BooleanField()
     26        self.assertRaises(ValidationError, f.clean, 'foo', None)
     27        try:
     28            f.clean('foo', None)
     29        except ValidationError, e:
     30            self.assertEqual(e.messages,
     31                        [u"'foo' value must be either True or False."])
     32
     33    def test_float_field_raises_error_message(self):
     34        f = models.FloatField()
     35        self.assertRaises(ValidationError, f.clean, 'foo', None)
     36        try:
     37            f.clean('foo', None)
     38        except ValidationError, e:
     39            self.assertEqual(e.messages, [u"'foo' value must be a float."])
     40
     41    def test_decimal_field_raises_error_message(self):
     42        f = models.DecimalField()
     43        self.assertRaises(ValidationError, f.clean, 'foo', None)
     44        try:
     45            f.clean('foo', None)
     46        except ValidationError, e:
     47            self.assertEqual(e.messages,
     48                        [u"'foo' value must be a decimal number."])
     49
     50    def test_null_boolean_field_raises_error_message(self):
     51        f = models.NullBooleanField()
     52        self.assertRaises(ValidationError, f.clean, 'foo', None)
     53        try:
     54            f.clean('foo', None)
     55        except ValidationError, e:
     56            self.assertEqual(e.messages,
     57                        [u"'foo' value must be either None, True or False."])
  • tests/modeltests/validation/tests.py

    diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
    index 4236d8e..d507aac 100644
    a b from modeltests.validation.validators import TestModelsWithValidators  
    99from modeltests.validation.test_unique import (GetUniqueCheckTests,
    1010    PerformUniqueChecksTest)
    1111from modeltests.validation.test_custom_messages import CustomMessagesTest
     12from modeltests.validation.test_error_messages import ValidationMessagesTest
    1213
    1314
    1415class BaseModelValidationTests(ValidationTestCase):
Back to Top