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:
parent
b5ac6956a6
commit
6a362d3684
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue