Ticket #11595: fixture_validationv2.diff
File fixture_validationv2.diff, 7.7 KB (added by , 13 years ago) |
---|
-
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): 459 459 460 460 empty_strings_allowed = False 461 461 default_error_messages = { 462 'invalid': _(u 'This value must be an integer.'),462 'invalid': _(u"'%s' value must be an integer."), 463 463 } 464 464 def __init__(self, *args, **kwargs): 465 465 assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ … … class AutoField(Field): 475 475 try: 476 476 return int(value) 477 477 except (TypeError, ValueError): 478 raise exceptions.ValidationError(self.error_messages['invalid']) 478 msg = self.error_messages['invalid'] % str(value) 479 raise exceptions.ValidationError(msg) 479 480 480 481 def validate(self, value, model_instance): 481 482 pass … … class AutoField(Field): 497 498 class BooleanField(Field): 498 499 empty_strings_allowed = False 499 500 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."), 501 502 } 502 503 description = _("Boolean (Either True or False)") 503 504 def __init__(self, *args, **kwargs): … … class BooleanField(Field): 518 519 return True 519 520 if value in ('f', 'False', '0'): 520 521 return False 521 raise exceptions.ValidationError(self.error_messages['invalid']) 522 msg = self.error_messages['invalid'] % str(value) 523 raise exceptions.ValidationError(msg) 522 524 523 525 def get_prep_lookup(self, lookup_type, value): 524 526 # Special-case handling for filters coming from a Web request (e.g. the … … class DateTimeField(DateField): 750 752 class DecimalField(Field): 751 753 empty_strings_allowed = False 752 754 default_error_messages = { 753 'invalid': _(u 'This value must be a decimal number.'),755 'invalid': _(u"'%s' value must be a decimal number."), 754 756 } 755 757 description = _("Decimal number") 756 758 … … class DecimalField(Field): 767 769 try: 768 770 return decimal.Decimal(value) 769 771 except decimal.InvalidOperation: 770 raise exceptions.ValidationError(self.error_messages['invalid']) 772 msg = self.error_messages['invalid'] % str(value) 773 raise exceptions.ValidationError(msg) 771 774 772 775 def _format(self, value): 773 776 if isinstance(value, basestring) or value is None: … … class FilePathField(Field): 845 848 class FloatField(Field): 846 849 empty_strings_allowed = False 847 850 default_error_messages = { 848 'invalid': _(" Thisvalue must be a float."),851 'invalid': _("'%s' value must be a float."), 849 852 } 850 853 description = _("Floating point number") 851 854 … … class FloatField(Field): 863 866 try: 864 867 return float(value) 865 868 except (TypeError, ValueError): 866 raise exceptions.ValidationError(self.error_messages['invalid']) 869 msg = self.error_messages['invalid'] % str(value) 870 raise exceptions.ValidationError(msg) 867 871 868 872 def formfield(self, **kwargs): 869 873 defaults = {'form_class': forms.FloatField} … … class FloatField(Field): 873 877 class IntegerField(Field): 874 878 empty_strings_allowed = False 875 879 default_error_messages = { 876 'invalid': _(" Thisvalue must be an integer."),880 'invalid': _("'%s' value must be an integer."), 877 881 } 878 882 description = _("Integer") 879 883 … … class IntegerField(Field): 897 901 try: 898 902 return int(value) 899 903 except (TypeError, ValueError): 900 raise exceptions.ValidationError(self.error_messages['invalid']) 904 msg = self.error_messages['invalid'] % str(value) 905 raise exceptions.ValidationError(msg) 901 906 902 907 def formfield(self, **kwargs): 903 908 defaults = {'form_class': forms.IntegerField} … … class IPAddressField(Field): 935 940 class NullBooleanField(Field): 936 941 empty_strings_allowed = False 937 942 default_error_messages = { 938 'invalid': _(" Thisvalue must be either None, True or False."),943 'invalid': _("'%s' value must be either None, True or False."), 939 944 } 940 945 description = _("Boolean (Either True, False or None)") 941 946 … … class NullBooleanField(Field): 958 963 return True 959 964 if value in ('f', 'False', '0'): 960 965 return False 961 raise exceptions.ValidationError(self.error_messages['invalid']) 966 msg = self.error_messages['invalid'] % str(value) 967 raise exceptions.ValidationError(msg) 962 968 963 969 def get_prep_lookup(self, lookup_type, value): 964 970 # 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
- + 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
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 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):