Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14499 -- ATSocialSecurityNumberField now responds to all EMPTY…

…_VALUES correctly. Also converted Austrian localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14873 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 575962c213c7e6b7393c9ba43ecbb11389ff7c8b 1 parent aa05224
Alex Gaynor authored December 12, 2010
10  django/contrib/localflavor/at/forms.py
@@ -4,12 +4,14 @@
4 4
 
5 5
 import re
6 6
 
7  
-from django.utils.translation import ugettext_lazy as _
8  
-from django.forms.fields import Field, RegexField, Select
  7
+from django.core.validators import EMPTY_VALUES
9 8
 from django.forms import ValidationError
  9
+from django.forms.fields import Field, RegexField, Select
  10
+from django.utils.translation import ugettext_lazy as _
10 11
 
11 12
 re_ssn = re.compile(r'^\d{4} \d{6}')
12 13
 
  14
+
13 15
 class ATZipCodeField(RegexField):
14 16
     """
15 17
     A form field that validates its input is an Austrian postcode.
@@ -49,6 +51,9 @@ class ATSocialSecurityNumberField(Field):
49 51
     }
50 52
 
51 53
     def clean(self, value):
  54
+        value = super(ATSocialSecurityNumberField, self).clean(value)
  55
+        if value in EMPTY_VALUES:
  56
+            return u""
52 57
         if not re_ssn.search(value):
53 58
             raise ValidationError(self.error_messages['invalid'])
54 59
         sqnr, date = value.split(" ")
@@ -62,4 +67,3 @@ def clean(self, value):
62 67
         if res != int(check):
63 68
            raise ValidationError(self.error_messages['invalid'])
64 69
         return u'%s%s %s'%(sqnr, check, date,)
65  
-
125  tests/regressiontests/forms/localflavor/at.py
... ...
@@ -1,80 +1,45 @@
1  
-# -*- coding: utf-8 -*-
2  
-# Tests for the contrib/localflavor/ AT form fields.
3  
-
4  
-tests = r"""
5  
-# ATZipCodeField ###########################################################
6  
-
7  
->>> from django.contrib.localflavor.at.forms import ATZipCodeField 
8  
->>> f = ATZipCodeField()
9  
->>> f.clean('1150')
10  
-u'1150'
11  
->>> f.clean('4020')
12  
-u'4020'
13  
->>> f.clean('8020')
14  
-u'8020'
15  
->>> f.clean('111222')
16  
-Traceback (most recent call last):
17  
-...
18  
-ValidationError: [u'Enter a zip code in the format XXXX.']
19  
->>> f.clean('eeffee')
20  
-Traceback (most recent call last):
21  
-...
22  
-ValidationError: [u'Enter a zip code in the format XXXX.']
23  
->>> f.clean(u'')
24  
-Traceback (most recent call last):
25  
-...
26  
-ValidationError: [u'This field is required.']
27  
->>> f.clean(None)
28  
-Traceback (most recent call last):
29  
-...
30  
-ValidationError: [u'This field is required.']
31  
->>> f.clean('')
32  
-Traceback (most recent call last):
33  
-...
34  
-ValidationError: [u'This field is required.']
35  
-
36  
-
37  
->>> f = ATZipCodeField(required=False)
38  
->>> f.clean('1150')
39  
-u'1150'
40  
->>> f.clean('4020')
41  
-u'4020'
42  
->>> f.clean('8020')
43  
-u'8020'
44  
->>> f.clean('111222')
45  
-Traceback (most recent call last):
46  
-...
47  
-ValidationError: [u'Enter a zip code in the format XXXX.']
48  
->>> f.clean('eeffee')
49  
-Traceback (most recent call last):
50  
-...
51  
-ValidationError: [u'Enter a zip code in the format XXXX.']
52  
->>> f.clean(None)
53  
-u''
54  
->>> f.clean('')
55  
-u''
56  
->>> f.clean(u'')
57  
-u''
58  
-
59  
-# ATStateSelect ##################################################################
60  
-
61  
->>> from django.contrib.localflavor.at.forms import ATStateSelect
62  
->>> f = ATStateSelect()
63  
->>> f.render('bundesland', 'WI')
64  
-u'<select name="bundesland">\n<option value="BL">Burgenland</option>\n<option value="KA">Carinthia</option>\n<option value="NO">Lower Austria</option>\n<option value="OO">Upper Austria</option>\n<option value="SA">Salzburg</option>\n<option value="ST">Styria</option>\n<option value="TI">Tyrol</option>\n<option value="VO">Vorarlberg</option>\n<option value="WI" selected="selected">Vienna</option>\n</select>'
65  
-
66  
-# ATSocialSecurityNumberField ################################################
67  
-
68  
->>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField
69  
->>> f = ATSocialSecurityNumberField()
70  
->>> f.clean('1237 010180')
71  
-u'1237 010180'
72  
->>> f.clean('1237 010181')
73  
-Traceback (most recent call last):
74  
-...
75  
-ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
76  
->>> f.clean('12370 010180')
77  
-Traceback (most recent call last):
78  
-...
79  
-ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
80  
-"""
  1
+from django.contrib.localflavor.at.forms import (ATZipCodeField, ATStateSelect,
  2
+    ATSocialSecurityNumberField)
  3
+
  4
+from utils import LocalFlavorTestCase
  5
+
  6
+
  7
+class ATLocalFlavorTests(LocalFlavorTestCase):
  8
+    def test_ATStateSelect(self):
  9
+        f = ATStateSelect()
  10
+        out = u'''<select name="bundesland">
  11
+<option value="BL">Burgenland</option>
  12
+<option value="KA">Carinthia</option>
  13
+<option value="NO">Lower Austria</option>
  14
+<option value="OO">Upper Austria</option>
  15
+<option value="SA">Salzburg</option>
  16
+<option value="ST">Styria</option>
  17
+<option value="TI">Tyrol</option>
  18
+<option value="VO">Vorarlberg</option>
  19
+<option value="WI" selected="selected">Vienna</option>
  20
+</select>'''
  21
+        self.assertEqual(f.render('bundesland', 'WI'), out)
  22
+
  23
+    def test_ATZipCodeField(self):
  24
+        error_format = [u'Enter a zip code in the format XXXX.']
  25
+        valid = {
  26
+            '1150': '1150',
  27
+            '4020': '4020',
  28
+            '8020': '8020',
  29
+        }
  30
+        invalid = {
  31
+            '111222': error_format,
  32
+            'eeffee': error_format,
  33
+        }
  34
+        self.assertFieldOutput(ATZipCodeField, valid, invalid)
  35
+
  36
+    def test_ATSocialSecurityNumberField(self):
  37
+        error_format = [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
  38
+        valid = {
  39
+            '1237 010180': '1237 010180',
  40
+        }
  41
+        invalid = {
  42
+            '1237 010181': error_format,
  43
+            '12370 010180': error_format,
  44
+        }
  45
+        self.assertFieldOutput(ATSocialSecurityNumberField, valid, invalid)
3  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,5 +1,4 @@
1 1
 # -*- coding: utf-8 -*-
2  
-from localflavor.at import tests as localflavor_at_tests
3 2
 from localflavor.au import tests as localflavor_au_tests
4 3
 from localflavor.br import tests as localflavor_br_tests
5 4
 from localflavor.ca import tests as localflavor_ca_tests
@@ -28,6 +27,7 @@
28 27
 from localflavor.za import tests as localflavor_za_tests
29 28
 
30 29
 from localflavor.ar import ARLocalFlavorTests
  30
+from localflavor.at import ATLocalFlavorTests
31 31
 from localflavor.de import DELocalFlavorTests
32 32
 from localflavor.be import BELocalFlavorTests
33 33
 from localflavor.il import ILLocalFlavorTests
@@ -35,7 +35,6 @@
35 35
 
36 36
 
37 37
 __test__ = {
38  
-    'localflavor_at_tests': localflavor_at_tests,
39 38
     'localflavor_au_tests': localflavor_au_tests,
40 39
     'localflavor_br_tests': localflavor_br_tests,
41 40
     'localflavor_ca_tests': localflavor_ca_tests,
4  tests/regressiontests/forms/tests/__init__.py
@@ -13,8 +13,10 @@
13 13
 
14 14
 from regressiontests.forms.localflavortests import (
15 15
     __test__,
  16
+    ARLocalFlavorTests,
  17
+    ATLocalFlavorTests,
16 18
     BELocalFlavorTests,
17 19
     DELocalFlavorTests,
18 20
     ILLocalFlavorTests,
19  
-    TRLocalFlavorTests
  21
+    TRLocalFlavorTests,
20 22
 )

0 notes on commit 575962c

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