Fixed #14750 -- ILPostalCodeField didn't handle all EMPTY_VALUES correctly. Also converted the Israeli localflavor testcases to use the new, less verbose format. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14943 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 20:31:22 +00:00
parent b5ac6956a6
commit 6a362d3684
2 changed files with 36 additions and 53 deletions

View File

@ -4,6 +4,7 @@ Israeli-specific form helpers
import re import re
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES
from django.forms.fields import RegexField, Field, EMPTY_VALUES from django.forms.fields import RegexField, Field, EMPTY_VALUES
from django.utils.checksums import luhn from django.utils.checksums import luhn
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -34,7 +35,7 @@ class ILPostalCodeField(RegexField):
super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs) super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs)
def clean(self, value): def clean(self, value):
if value is not None: if value not in EMPTY_VALUES:
value = value.replace(" ", "") value = value.replace(" ", "")
return super(ILPostalCodeField, self).clean(value) return super(ILPostalCodeField, self).clean(value)

View File

@ -1,57 +1,39 @@
from django.contrib.localflavor.il.forms import (ILPostalCodeField, from django.contrib.localflavor.il.forms import (ILPostalCodeField,
ILIDNumberField) ILIDNumberField)
from django.core.exceptions import ValidationError
from django.utils.unittest import TestCase from utils import LocalFlavorTestCase
class ILLocalFlavorTests(TestCase): class ILLocalFlavorTests(LocalFlavorTestCase):
def test_postal_code_field(self): def test_ILPostalCodeField(self):
f = ILPostalCodeField() error_format = [u'Enter a postal code in the format XXXXX']
self.assertRaisesRegexp(ValidationError, valid = {
"Enter a postal code in the format XXXXX", '69973': '69973',
f.clean, "84545x" '699 73': '69973',
) '12345': '12345',
self.assertEqual(f.clean("69973"), "69973") }
self.assertEqual(f.clean("699 73"), "69973") invalid = {
self.assertEqual(f.clean("12345"), "12345") '84545x': error_format,
self.assertRaisesRegexp(ValidationError, '123456': error_format,
"Enter a postal code in the format XXXXX", '1234': error_format,
f.clean, "123456" '123 4': error_format,
) }
self.assertRaisesRegexp(ValidationError, self.assertFieldOutput(ILPostalCodeField, valid, invalid)
"Enter a postal code in the format XXXXX",
f.clean, "1234"
)
self.assertRaisesRegexp(ValidationError,
"Enter a postal code in the format XXXXX",
f.clean, "123 4"
)
self.assertRaises(ValidationError, f.clean, None)
def test_id_number_field(self): def test_ILIDNumberField(self):
f = ILIDNumberField() error_invalid = [u'Enter a valid ID number.']
self.assertEqual(f.clean("3933742-3"), "39337423") valid = {
self.assertEqual(f.clean("39337423"), "39337423") '3933742-3': '39337423',
self.assertEqual(f.clean("039337423"), "039337423") '39337423': '39337423',
self.assertEqual(f.clean("03933742-3"), "039337423") '039337423': '039337423',
self.assertEqual(f.clean("0091"), "0091") '03933742-3': '039337423',
self.assertRaisesRegexp(ValidationError, '0091': '0091',
"Enter a valid ID number.", }
f.clean, "123456789" invalid = {
) '123456789': error_invalid,
self.assertRaisesRegexp(ValidationError, '12345678-9': error_invalid,
"Enter a valid ID number.", '012346578': error_invalid,
f.clean, "12345678-9" '012346578-': error_invalid,
) '0001': error_invalid,
self.assertRaisesRegexp(ValidationError, }
"Enter a valid ID number.", self.assertFieldOutput(ILIDNumberField, valid, invalid)
f.clean, "012346578"
)
self.assertRaisesRegexp(ValidationError,
"Enter a valid ID number.",
f.clean, "012346578-"
)
self.assertRaisesRegexp(ValidationError,
"Enter a valid ID number.",
f.clean, "0001"
)