Ticket #11595: fixture_validation.diff

File fixture_validation.diff, 7.4 KB (added by Raúl Cumplido, 14 years ago)

Added their data to fixture validation errors and tests

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

     
    449449
    450450    empty_strings_allowed = False
    451451    default_error_messages = {
    452         'invalid': _(u'This value must be an integer.'),
     452        'invalid': _('"%s" value must be an integer.'),
    453453    }
    454454    def __init__(self, *args, **kwargs):
    455455        assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__
     
    465465        try:
    466466            return int(value)
    467467        except (TypeError, ValueError):
    468             raise exceptions.ValidationError(self.error_messages['invalid'])
     468            msg = self.error_messages['invalid'] % str(value)
     469            raise exceptions.ValidationError(msg)
    469470
    470471    def validate(self, value, model_instance):
    471472        pass
     
    487488class BooleanField(Field):
    488489    empty_strings_allowed = False
    489490    default_error_messages = {
    490         'invalid': _(u'This value must be either True or False.'),
     491        'invalid': _('"%s" value must be either True or False.'),
    491492    }
    492493    description = _("Boolean (Either True or False)")
    493494    def __init__(self, *args, **kwargs):
     
    508509            return True
    509510        if value in ('f', 'False', '0'):
    510511            return False
    511         raise exceptions.ValidationError(self.error_messages['invalid'])
     512        msg = self.error_messages['invalid'] % str(value)
     513        raise exceptions.ValidationError(msg)
    512514
    513515    def get_prep_lookup(self, lookup_type, value):
    514516        # Special-case handling for filters coming from a Web request (e.g. the
     
    740742class DecimalField(Field):
    741743    empty_strings_allowed = False
    742744    default_error_messages = {
    743         'invalid': _(u'This value must be a decimal number.'),
     745        'invalid': _('"%s" value must be a decimal number.'),
    744746    }
    745747    description = _("Decimal number")
    746748
     
    757759        try:
    758760            return decimal.Decimal(value)
    759761        except decimal.InvalidOperation:
    760             raise exceptions.ValidationError(self.error_messages['invalid'])
     762            msg = self.error_messages['invalid'] % str(value)
     763            raise exceptions.ValidationError(msg)
    761764
    762765    def _format(self, value):
    763766        if isinstance(value, basestring) or value is None:
     
    835838class FloatField(Field):
    836839    empty_strings_allowed = False
    837840    default_error_messages = {
    838         'invalid': _("This value must be a float."),
     841        'invalid': _('"%s" value must be a float.'),
    839842    }
    840843    description = _("Floating point number")
    841844
     
    853856        try:
    854857            return float(value)
    855858        except (TypeError, ValueError):
    856             raise exceptions.ValidationError(self.error_messages['invalid'])
     859            msg = self.error_messages['invalid'] % str(value)
     860            raise exceptions.ValidationError(msg)
    857861
    858862    def formfield(self, **kwargs):
    859863        defaults = {'form_class': forms.FloatField}
     
    863867class IntegerField(Field):
    864868    empty_strings_allowed = False
    865869    default_error_messages = {
    866         'invalid': _("This value must be an integer."),
     870        'invalid': _('"%s" value must be an integer.'),
    867871    }
    868872    description = _("Integer")
    869873
     
    887891        try:
    888892            return int(value)
    889893        except (TypeError, ValueError):
    890             raise exceptions.ValidationError(self.error_messages['invalid'])
     894            msg = self.error_messages['invalid'] % str(value)
     895            raise exceptions.ValidationError(msg)
    891896
    892897    def formfield(self, **kwargs):
    893898        defaults = {'form_class': forms.IntegerField}
     
    925930class NullBooleanField(Field):
    926931    empty_strings_allowed = False
    927932    default_error_messages = {
    928         'invalid': _("This value must be either None, True or False."),
     933        'invalid': _('"%s" value must be either None, True or False.'),
    929934    }
    930935    description = _("Boolean (Either True, False or None)")
    931936
     
    948953            return True
    949954        if value in ('f', 'False', '0'):
    950955            return False
    951         raise exceptions.ValidationError(self.error_messages['invalid'])
     956        msg = self.error_messages['invalid'] % str(value)
     957        raise exceptions.ValidationError(msg)
    952958
    953959    def get_prep_lookup(self, lookup_type, value):
    954960        # Special-case handling for filters coming from a Web request (e.g. the
  • tests/modeltests/validation/test_error_messages.py

     
     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

     
    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