Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14871, #14872 -- ZAIDField didn't handle alll EMPTY_VA…

…LUES correctly and ZAPostCodeField didn't respect *args or **kwargs (such as required=False). Also converted South African localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit. Backport of [14956].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14980 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 160533410fcb94c41f6481880d80500dae57b34e 1 parent 989f592
Alex Gaynor authored December 18, 2010
8  django/contrib/localflavor/za/forms.py
@@ -22,14 +22,14 @@ class ZAIDField(Field):
22 22
     }
23 23
 
24 24
     def clean(self, value):
25  
-        # strip spaces and dashes
26  
-        value = value.strip().replace(' ', '').replace('-', '')
27  
-
28 25
         super(ZAIDField, self).clean(value)
29 26
 
30 27
         if value in EMPTY_VALUES:
31 28
             return u''
32 29
 
  30
+        # strip spaces and dashes
  31
+        value = value.strip().replace(' ', '').replace('-', '')
  32
+
33 33
         match = re.match(id_re, value)
34 34
 
35 35
         if not match:
@@ -57,4 +57,4 @@ class ZAPostCodeField(RegexField):
57 57
 
58 58
     def __init__(self, *args, **kwargs):
59 59
         super(ZAPostCodeField, self).__init__(r'^\d{4}$',
60  
-            max_length=None, min_length=None)
  60
+            max_length=None, min_length=None, *args, **kwargs)
1  tests/regressiontests/forms/localflavor/__init__.py
... ...
@@ -1 +0,0 @@
1  
-# -*- coding: utf-8 -*-
63  tests/regressiontests/forms/localflavor/za.py
... ...
@@ -1,40 +1,29 @@
1  
-tests = r"""
2  
-# ZAIDField #################################################################
  1
+from django.contrib.localflavor.za.forms import ZAIDField, ZAPostCodeField
3 2
 
4  
-ZAIDField validates that the date is a valid birthdate and that the value
5  
-has a valid checksum. It allows spaces and dashes, and returns a plain 
6  
-string of digits.
7  
->>> from django.contrib.localflavor.za.forms import ZAIDField
8  
->>> f = ZAIDField()
9  
->>> f.clean('0002290001003')
10  
-'0002290001003'
11  
->>> f.clean('000229 0001 003')
12  
-'0002290001003'
13  
->>> f.clean('0102290001001')
14  
-Traceback (most recent call last):
15  
-...
16  
-ValidationError: [u'Enter a valid South African ID number']
17  
->>> f.clean('811208')
18  
-Traceback (most recent call last):
19  
-...
20  
-ValidationError: [u'Enter a valid South African ID number']
21  
->>> f.clean('0002290001004')
22  
-Traceback (most recent call last):
23  
-...
24  
-ValidationError: [u'Enter a valid South African ID number']
  3
+from utils import LocalFlavorTestCase
25 4
 
26  
-# ZAPostCodeField ###########################################################
27  
->>> from django.contrib.localflavor.za.forms import ZAPostCodeField
28  
->>> f = ZAPostCodeField()
29  
->>> f.clean('abcd')
30  
-Traceback (most recent call last):
31  
-...
32  
-ValidationError: [u'Enter a valid South African postal code']
33  
->>> f.clean('0000')
34  
-u'0000'
35  
->>> f.clean(' 7530')
36  
-Traceback (most recent call last):
37  
-...
38  
-ValidationError: [u'Enter a valid South African postal code']
39 5
 
40  
-"""
  6
+class ZALocalFlavorTests(LocalFlavorTestCase):
  7
+    def test_ZAIDField(self):
  8
+        error_invalid = [u'Enter a valid South African ID number']
  9
+        valid = {
  10
+            '0002290001003': '0002290001003',
  11
+            '000229 0001 003': '0002290001003',
  12
+        }
  13
+        invalid = {
  14
+            '0102290001001': error_invalid,
  15
+            '811208': error_invalid,
  16
+            '0002290001004': error_invalid,
  17
+        }
  18
+        self.assertFieldOutput(ZAIDField, valid, invalid)
  19
+
  20
+    def test_ZAPostCodeField(self):
  21
+        error_invalid = [u'Enter a valid South African postal code']
  22
+        valid = {
  23
+            '0000': '0000',
  24
+        }
  25
+        invalid = {
  26
+            'abcd': error_invalid,
  27
+            ' 7530': error_invalid,
  28
+        }
  29
+        self.assertFieldOutput(ZAPostCodeField, valid, invalid)
4  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,7 +1,5 @@
1  
-# -*- coding: utf-8 -*-
2 1
 from localflavor.cz import tests as localflavor_cz_tests
3 2
 from localflavor.se import tests as localflavor_se_tests
4  
-from localflavor.za import tests as localflavor_za_tests
5 3
 
6 4
 from localflavor.ar import ARLocalFlavorTests
7 5
 from localflavor.at import ATLocalFlavorTests
@@ -30,10 +28,10 @@
30 28
 from localflavor.uk import UKLocalFlavorTests
31 29
 from localflavor.us import USLocalFlavorTests
32 30
 from localflavor.uy import UYLocalFlavorTests
  31
+from localflavor.za import ZALocalFlavorTests
33 32
 
34 33
 
35 34
 __test__ = {
36 35
     'localflavor_cz_tests': localflavor_cz_tests,
37 36
     'localflavor_se_tests': localflavor_se_tests,
38  
-    'localflavor_za_tests': localflavor_za_tests,
39 37
 }
1  tests/regressiontests/forms/tests/__init__.py
@@ -39,4 +39,5 @@
39 39
     UKLocalFlavorTests,
40 40
     USLocalFlavorTests,
41 41
     UYLocalFlavorTests,
  42
+    ZALocalFlavorTests,
42 43
 )

0 notes on commit 1605334

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