Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14750 -- ILPostalCodeField didn't handle all EMPTY_VALUES corr…

…ectly. 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
  • Loading branch information...
commit 6a362d368411052ca8d3f0058d82b657eae846f5 1 parent b5ac695
Alex Gaynor authored December 18, 2010
3  django/contrib/localflavor/il/forms.py
@@ -4,6 +4,7 @@
4 4
 import re
5 5
 
6 6
 from django.core.exceptions import ValidationError
  7
+from django.core.validators import EMPTY_VALUES
7 8
 from django.forms.fields import RegexField, Field, EMPTY_VALUES
8 9
 from django.utils.checksums import luhn
9 10
 from django.utils.translation import ugettext_lazy as _
@@ -34,7 +35,7 @@ def __init__(self, *args, **kwargs):
34 35
         super(ILPostalCodeField, self).__init__(r'^\d{5}$', *args, **kwargs)
35 36
 
36 37
     def clean(self, value):
37  
-        if value is not None:
  38
+        if value not in EMPTY_VALUES:
38 39
             value = value.replace(" ", "")
39 40
         return super(ILPostalCodeField, self).clean(value)
40 41
 
86  tests/regressiontests/forms/localflavor/il.py
... ...
@@ -1,57 +1,39 @@
1 1
 from django.contrib.localflavor.il.forms import (ILPostalCodeField,
2 2
     ILIDNumberField)
3  
-from django.core.exceptions import ValidationError
4  
-from django.utils.unittest import TestCase
5 3
 
  4
+from utils import LocalFlavorTestCase
6 5
 
7  
-class ILLocalFlavorTests(TestCase):
8  
-    def test_postal_code_field(self):
9  
-        f = ILPostalCodeField()
10  
-        self.assertRaisesRegexp(ValidationError,
11  
-            "Enter a postal code in the format XXXXX",
12  
-            f.clean, "84545x"
13  
-        )
14  
-        self.assertEqual(f.clean("69973"), "69973")
15  
-        self.assertEqual(f.clean("699 73"), "69973")
16  
-        self.assertEqual(f.clean("12345"), "12345")
17  
-        self.assertRaisesRegexp(ValidationError,
18  
-            "Enter a postal code in the format XXXXX",
19  
-            f.clean, "123456"
20  
-        )
21  
-        self.assertRaisesRegexp(ValidationError,
22  
-            "Enter a postal code in the format XXXXX",
23  
-            f.clean, "1234"
24  
-        )
25  
-        self.assertRaisesRegexp(ValidationError,
26  
-            "Enter a postal code in the format XXXXX",
27  
-            f.clean, "123 4"
28  
-        )
29  
-        self.assertRaises(ValidationError, f.clean, None)
30 6
 
31  
-    def test_id_number_field(self):
32  
-        f = ILIDNumberField()
33  
-        self.assertEqual(f.clean("3933742-3"), "39337423")
34  
-        self.assertEqual(f.clean("39337423"), "39337423")
35  
-        self.assertEqual(f.clean("039337423"), "039337423")
36  
-        self.assertEqual(f.clean("03933742-3"), "039337423")
37  
-        self.assertEqual(f.clean("0091"), "0091")
38  
-        self.assertRaisesRegexp(ValidationError,
39  
-            "Enter a valid ID number.",
40  
-            f.clean, "123456789"
41  
-        )
42  
-        self.assertRaisesRegexp(ValidationError,
43  
-            "Enter a valid ID number.",
44  
-            f.clean, "12345678-9"
45  
-        )
46  
-        self.assertRaisesRegexp(ValidationError,
47  
-            "Enter a valid ID number.",
48  
-            f.clean, "012346578"
49  
-        )
50  
-        self.assertRaisesRegexp(ValidationError,
51  
-            "Enter a valid ID number.",
52  
-            f.clean, "012346578-"
53  
-        )
54  
-        self.assertRaisesRegexp(ValidationError,
55  
-            "Enter a valid ID number.",
56  
-            f.clean, "0001"
57  
-        )
  7
+class ILLocalFlavorTests(LocalFlavorTestCase):
  8
+    def test_ILPostalCodeField(self):
  9
+        error_format = [u'Enter a postal code in the format XXXXX']
  10
+        valid = {
  11
+            '69973': '69973',
  12
+            '699 73': '69973',
  13
+            '12345': '12345',
  14
+        }
  15
+        invalid = {
  16
+            '84545x': error_format,
  17
+            '123456': error_format,
  18
+            '1234': error_format,
  19
+            '123 4': error_format,
  20
+        }
  21
+        self.assertFieldOutput(ILPostalCodeField, valid, invalid)
  22
+
  23
+    def test_ILIDNumberField(self):
  24
+        error_invalid = [u'Enter a valid ID number.']
  25
+        valid = {
  26
+            '3933742-3': '39337423',
  27
+            '39337423': '39337423',
  28
+            '039337423': '039337423',
  29
+            '03933742-3': '039337423',
  30
+            '0091': '0091',
  31
+        }
  32
+        invalid = {
  33
+            '123456789': error_invalid,
  34
+            '12345678-9': error_invalid,
  35
+            '012346578': error_invalid,
  36
+            '012346578-': error_invalid,
  37
+            '0001': error_invalid,
  38
+        }
  39
+        self.assertFieldOutput(ILIDNumberField, valid, invalid)

0 notes on commit 6a362d3

Please sign in to comment.
Something went wrong with that request. Please try again.