diff --git a/django/contrib/localflavor/il/forms.py b/django/contrib/localflavor/il/forms.py index bbf95d2eed..a14358737e 100644 --- a/django/contrib/localflavor/il/forms.py +++ b/django/contrib/localflavor/il/forms.py @@ -4,6 +4,7 @@ Israeli-specific form helpers import re from django.core.exceptions import ValidationError +from django.core.validators import EMPTY_VALUES from django.forms.fields import RegexField, Field, EMPTY_VALUES from django.utils.checksums import luhn from django.utils.translation import ugettext_lazy as _ @@ -34,7 +35,7 @@ class ILPostalCodeField(RegexField): super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs) def clean(self, value): - if value is not None: + if value not in EMPTY_VALUES: value = value.replace(" ", "") return super(ILPostalCodeField, self).clean(value) diff --git a/tests/regressiontests/forms/localflavor/il.py b/tests/regressiontests/forms/localflavor/il.py index 8a327079ac..c7323e943c 100644 --- a/tests/regressiontests/forms/localflavor/il.py +++ b/tests/regressiontests/forms/localflavor/il.py @@ -1,57 +1,39 @@ from django.contrib.localflavor.il.forms import (ILPostalCodeField, ILIDNumberField) -from django.core.exceptions import ValidationError -from django.utils.unittest import TestCase + +from utils import LocalFlavorTestCase -class ILLocalFlavorTests(TestCase): - def test_postal_code_field(self): - f = ILPostalCodeField() - self.assertRaisesRegexp(ValidationError, - "Enter a postal code in the format XXXXX", - f.clean, "84545x" - ) - self.assertEqual(f.clean("69973"), "69973") - self.assertEqual(f.clean("699 73"), "69973") - self.assertEqual(f.clean("12345"), "12345") - self.assertRaisesRegexp(ValidationError, - "Enter a postal code in the format XXXXX", - f.clean, "123456" - ) - self.assertRaisesRegexp(ValidationError, - "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) +class ILLocalFlavorTests(LocalFlavorTestCase): + def test_ILPostalCodeField(self): + error_format = [u'Enter a postal code in the format XXXXX'] + valid = { + '69973': '69973', + '699 73': '69973', + '12345': '12345', + } + invalid = { + '84545x': error_format, + '123456': error_format, + '1234': error_format, + '123 4': error_format, + } + self.assertFieldOutput(ILPostalCodeField, valid, invalid) - def test_id_number_field(self): - f = ILIDNumberField() - self.assertEqual(f.clean("3933742-3"), "39337423") - self.assertEqual(f.clean("39337423"), "39337423") - self.assertEqual(f.clean("039337423"), "039337423") - self.assertEqual(f.clean("03933742-3"), "039337423") - self.assertEqual(f.clean("0091"), "0091") - self.assertRaisesRegexp(ValidationError, - "Enter a valid ID number.", - f.clean, "123456789" - ) - self.assertRaisesRegexp(ValidationError, - "Enter a valid ID number.", - f.clean, "12345678-9" - ) - self.assertRaisesRegexp(ValidationError, - "Enter a valid ID number.", - 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" - ) + def test_ILIDNumberField(self): + error_invalid = [u'Enter a valid ID number.'] + valid = { + '3933742-3': '39337423', + '39337423': '39337423', + '039337423': '039337423', + '03933742-3': '039337423', + '0091': '0091', + } + invalid = { + '123456789': error_invalid, + '12345678-9': error_invalid, + '012346578': error_invalid, + '012346578-': error_invalid, + '0001': error_invalid, + } + self.assertFieldOutput(ILIDNumberField, valid, invalid)