Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14859 -- ITSocialSecurityNumberField and ITVatNumberField didn…

…'t handle all EMPTY_VALUES gracefully. Also converted the Italias localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14945 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f8caeefff193e4ace0db13c7213194be933584f7 1 parent 58bc881
Alex Gaynor authored December 18, 2010
8  django/contrib/localflavor/it/forms.py
@@ -50,8 +50,8 @@ def __init__(self, *args, **kwargs):
50 50
 
51 51
     def clean(self, value):
52 52
         value = super(ITSocialSecurityNumberField, self).clean(value)
53  
-        if value == u'':
54  
-            return value
  53
+        if value in EMPTY_VALUES:
  54
+            return u''
55 55
         value = re.sub('\s', u'', value).upper()
56 56
         try:
57 57
             check_digit = ssn_check_digit(value)
@@ -71,8 +71,8 @@ class ITVatNumberField(Field):
71 71
 
72 72
     def clean(self, value):
73 73
         value = super(ITVatNumberField, self).clean(value)
74  
-        if value == u'':
75  
-            return value
  74
+        if value in EMPTY_VALUES:
  75
+            return u''
76 76
         try:
77 77
             vat_number = int(value)
78 78
         except ValueError:
122  tests/regressiontests/forms/localflavor/it.py
... ...
@@ -1,62 +1,70 @@
1  
-# -*- coding: utf-8 -*-
2  
-# Tests for the contrib/localflavor/ IT form fields.
  1
+from django.contrib.localflavor.it.forms import (ITZipCodeField, ITRegionSelect,
  2
+    ITSocialSecurityNumberField, ITVatNumberField)
3 3
 
4  
-tests = r"""
5  
-# ITZipCodeField #############################################################
  4
+from utils import LocalFlavorTestCase
6 5
 
7  
->>> from django.contrib.localflavor.it.forms import ITZipCodeField
8  
->>> f = ITZipCodeField()
9  
->>> f.clean('00100')
10  
-u'00100'
11  
->>> f.clean(' 00100')
12  
-Traceback (most recent call last):
13  
-...
14  
-ValidationError: [u'Enter a valid zip code.']
15 6
 
16  
-# ITRegionSelect #############################################################
  7
+class ITLocalFlavorTests(LocalFlavorTestCase):
  8
+    def test_ITRegionSelect(self):
  9
+        f = ITRegionSelect()
  10
+        out = u'''<select name="regions">
  11
+<option value="ABR">Abruzzo</option>
  12
+<option value="BAS">Basilicata</option>
  13
+<option value="CAL">Calabria</option>
  14
+<option value="CAM">Campania</option>
  15
+<option value="EMR">Emilia-Romagna</option>
  16
+<option value="FVG">Friuli-Venezia Giulia</option>
  17
+<option value="LAZ">Lazio</option>
  18
+<option value="LIG">Liguria</option>
  19
+<option value="LOM">Lombardia</option>
  20
+<option value="MAR">Marche</option>
  21
+<option value="MOL">Molise</option>
  22
+<option value="PMN" selected="selected">Piemonte</option>
  23
+<option value="PUG">Puglia</option>
  24
+<option value="SAR">Sardegna</option>
  25
+<option value="SIC">Sicilia</option>
  26
+<option value="TOS">Toscana</option>
  27
+<option value="TAA">Trentino-Alto Adige</option>
  28
+<option value="UMB">Umbria</option>
  29
+<option value="VAO">Valle d\u2019Aosta</option>
  30
+<option value="VEN">Veneto</option>
  31
+</select>'''
  32
+        self.assertEqual(f.render('regions', 'PMN'), out)
17 33
 
18  
->>> from django.contrib.localflavor.it.forms import ITRegionSelect
19  
->>> w = ITRegionSelect()
20  
->>> w.render('regions', 'PMN')
21  
-u'<select name="regions">\n<option value="ABR">Abruzzo</option>\n<option value="BAS">Basilicata</option>\n<option value="CAL">Calabria</option>\n<option value="CAM">Campania</option>\n<option value="EMR">Emilia-Romagna</option>\n<option value="FVG">Friuli-Venezia Giulia</option>\n<option value="LAZ">Lazio</option>\n<option value="LIG">Liguria</option>\n<option value="LOM">Lombardia</option>\n<option value="MAR">Marche</option>\n<option value="MOL">Molise</option>\n<option value="PMN" selected="selected">Piemonte</option>\n<option value="PUG">Puglia</option>\n<option value="SAR">Sardegna</option>\n<option value="SIC">Sicilia</option>\n<option value="TOS">Toscana</option>\n<option value="TAA">Trentino-Alto Adige</option>\n<option value="UMB">Umbria</option>\n<option value="VAO">Valle d\u2019Aosta</option>\n<option value="VEN">Veneto</option>\n</select>'
  34
+    def test_ITZipCodeField(self):
  35
+        error_invalid = [u'Enter a valid zip code.']
  36
+        valid = {
  37
+            '00100': '00100',
  38
+        }
  39
+        invalid = {
  40
+            ' 00100': error_invalid,
  41
+        }
  42
+        self.assertFieldOutput(ITZipCodeField, valid, invalid)
  43
+    
  44
+    def test_ITSocialSecurityNumberField(self):
  45
+        error_invalid = [u'Enter a valid Social Security number.']
  46
+        valid = {
  47
+            'LVSGDU99T71H501L': 'LVSGDU99T71H501L',
  48
+            'LBRRME11A01L736W': 'LBRRME11A01L736W',
  49
+            'lbrrme11a01l736w': 'LBRRME11A01L736W',
  50
+            'LBR RME 11A01 L736W': 'LBRRME11A01L736W',
  51
+        }
  52
+        invalid = {
  53
+            'LBRRME11A01L736A': error_invalid,
  54
+            '%BRRME11A01L736W': error_invalid,
  55
+        }
  56
+        self.assertFieldOutput(ITSocialSecurityNumberField, valid, invalid)
  57
+    
  58
+    def test_ITVatNumberField(self):
  59
+        error_invalid = [u'Enter a valid VAT number.']
  60
+        valid = {
  61
+            '07973780013': '07973780013',
  62
+            '7973780013': '07973780013',
  63
+            7973780013: '07973780013',
  64
+        }
  65
+        invalid = {
  66
+            '07973780014': error_invalid,
  67
+            'A7973780013': error_invalid,
  68
+        }
  69
+        self.assertFieldOutput(ITVatNumberField, valid, invalid)
22 70
 
23  
-# ITSocialSecurityNumberField #################################################
24  
-
25  
->>> from django.contrib.localflavor.it.forms import ITSocialSecurityNumberField
26  
->>> f = ITSocialSecurityNumberField()
27  
->>> f.clean('LVSGDU99T71H501L')
28  
-u'LVSGDU99T71H501L'
29  
->>> f.clean('LBRRME11A01L736W')
30  
-u'LBRRME11A01L736W'
31  
->>> f.clean('lbrrme11a01l736w')
32  
-u'LBRRME11A01L736W'
33  
->>> f.clean('LBR RME 11A01 L736W')
34  
-u'LBRRME11A01L736W'
35  
->>> f.clean('LBRRME11A01L736A')
36  
-Traceback (most recent call last):
37  
-...
38  
-ValidationError: [u'Enter a valid Social Security number.']
39  
->>> f.clean('%BRRME11A01L736W')
40  
-Traceback (most recent call last):
41  
-...
42  
-ValidationError: [u'Enter a valid Social Security number.']
43  
-
44  
-# ITVatNumberField ###########################################################
45  
-
46  
->>> from django.contrib.localflavor.it.forms import ITVatNumberField
47  
->>> f = ITVatNumberField()
48  
->>> f.clean('07973780013')
49  
-u'07973780013'
50  
->>> f.clean('7973780013')
51  
-u'07973780013'
52  
->>> f.clean(7973780013)
53  
-u'07973780013'
54  
->>> f.clean('07973780014')
55  
-Traceback (most recent call last):
56  
-...
57  
-ValidationError: [u'Enter a valid VAT number.']
58  
->>> f.clean('A7973780013')
59  
-Traceback (most recent call last):
60  
-...
61  
-ValidationError: [u'Enter a valid VAT number.']
62  
-"""
3  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,6 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from localflavor.cz import tests as localflavor_cz_tests
3  
-from localflavor.it import tests as localflavor_it_tests
4 3
 from localflavor.jp import tests as localflavor_jp_tests
5 4
 from localflavor.kw import tests as localflavor_kw_tests
6 5
 from localflavor.nl import tests as localflavor_nl_tests
@@ -31,12 +30,12 @@
31 30
 from localflavor.ie import IELocalFlavorTests
32 31
 from localflavor.il import ILLocalFlavorTests
33 32
 from localflavor.is_ import ISLocalFlavorTests
  33
+from localflavor.it import ITLocalFlavorTests
34 34
 from localflavor.tr import TRLocalFlavorTests
35 35
 
36 36
 
37 37
 __test__ = {
38 38
     'localflavor_cz_tests': localflavor_cz_tests,
39  
-    'localflavor_it_tests': localflavor_it_tests,
40 39
     'localflavor_jp_tests': localflavor_jp_tests,
41 40
     'localflavor_kw_tests': localflavor_kw_tests,
42 41
     'localflavor_nl_tests': localflavor_nl_tests,
1  tests/regressiontests/forms/tests/__init__.py
@@ -30,5 +30,6 @@
30 30
     IELocalFlavorTests,
31 31
     ILLocalFlavorTests,
32 32
     ISLocalFlavorTests,
  33
+    ITLocalFlavorTests,
33 34
     TRLocalFlavorTests,
34 35
 )

0 notes on commit f8caeef

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