Ticket #11595: fixture_validation.diff
File fixture_validation.diff, 7.4 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/__init__.py
449 449 450 450 empty_strings_allowed = False 451 451 default_error_messages = { 452 'invalid': _( u'Thisvalue must be an integer.'),452 'invalid': _('"%s" value must be an integer.'), 453 453 } 454 454 def __init__(self, *args, **kwargs): 455 455 assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ … … 465 465 try: 466 466 return int(value) 467 467 except (TypeError, ValueError): 468 raise exceptions.ValidationError(self.error_messages['invalid']) 468 msg = self.error_messages['invalid'] % str(value) 469 raise exceptions.ValidationError(msg) 469 470 470 471 def validate(self, value, model_instance): 471 472 pass … … 487 488 class BooleanField(Field): 488 489 empty_strings_allowed = False 489 490 default_error_messages = { 490 'invalid': _( u'Thisvalue must be either True or False.'),491 'invalid': _('"%s" value must be either True or False.'), 491 492 } 492 493 description = _("Boolean (Either True or False)") 493 494 def __init__(self, *args, **kwargs): … … 508 509 return True 509 510 if value in ('f', 'False', '0'): 510 511 return False 511 raise exceptions.ValidationError(self.error_messages['invalid']) 512 msg = self.error_messages['invalid'] % str(value) 513 raise exceptions.ValidationError(msg) 512 514 513 515 def get_prep_lookup(self, lookup_type, value): 514 516 # Special-case handling for filters coming from a Web request (e.g. the … … 740 742 class DecimalField(Field): 741 743 empty_strings_allowed = False 742 744 default_error_messages = { 743 'invalid': _( u'Thisvalue must be a decimal number.'),745 'invalid': _('"%s" value must be a decimal number.'), 744 746 } 745 747 description = _("Decimal number") 746 748 … … 757 759 try: 758 760 return decimal.Decimal(value) 759 761 except decimal.InvalidOperation: 760 raise exceptions.ValidationError(self.error_messages['invalid']) 762 msg = self.error_messages['invalid'] % str(value) 763 raise exceptions.ValidationError(msg) 761 764 762 765 def _format(self, value): 763 766 if isinstance(value, basestring) or value is None: … … 835 838 class FloatField(Field): 836 839 empty_strings_allowed = False 837 840 default_error_messages = { 838 'invalid': _( "This value must be a float."),841 'invalid': _('"%s" value must be a float.'), 839 842 } 840 843 description = _("Floating point number") 841 844 … … 853 856 try: 854 857 return float(value) 855 858 except (TypeError, ValueError): 856 raise exceptions.ValidationError(self.error_messages['invalid']) 859 msg = self.error_messages['invalid'] % str(value) 860 raise exceptions.ValidationError(msg) 857 861 858 862 def formfield(self, **kwargs): 859 863 defaults = {'form_class': forms.FloatField} … … 863 867 class IntegerField(Field): 864 868 empty_strings_allowed = False 865 869 default_error_messages = { 866 'invalid': _( "This value must be an integer."),870 'invalid': _('"%s" value must be an integer.'), 867 871 } 868 872 description = _("Integer") 869 873 … … 887 891 try: 888 892 return int(value) 889 893 except (TypeError, ValueError): 890 raise exceptions.ValidationError(self.error_messages['invalid']) 894 msg = self.error_messages['invalid'] % str(value) 895 raise exceptions.ValidationError(msg) 891 896 892 897 def formfield(self, **kwargs): 893 898 defaults = {'form_class': forms.IntegerField} … … 925 930 class NullBooleanField(Field): 926 931 empty_strings_allowed = False 927 932 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.'), 929 934 } 930 935 description = _("Boolean (Either True, False or None)") 931 936 … … 948 953 return True 949 954 if value in ('f', 'False', '0'): 950 955 return False 951 raise exceptions.ValidationError(self.error_messages['invalid']) 956 msg = self.error_messages['invalid'] % str(value) 957 raise exceptions.ValidationError(msg) 952 958 953 959 def get_prep_lookup(self, lookup_type, value): 954 960 # Special-case handling for filters coming from a Web request (e.g. the -
tests/modeltests/validation/test_error_messages.py
1 from django.core.exceptions import ValidationError 2 from django.utils.unittest import TestCase 3 from django.db import models 4 5 6 class 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
9 9 from modeltests.validation.test_unique import (GetUniqueCheckTests, 10 10 PerformUniqueChecksTest) 11 11 from modeltests.validation.test_custom_messages import CustomMessagesTest 12 from modeltests.validation.test_error_messages import ValidationMessagesTest 12 13 13 14 14 15 class BaseModelValidationTests(ValidationTestCase):