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] Converted the Brazillian localflavor doctests to unittests. W…

…e have always been at war with doctests. Thanks to Idan Gazit. Backport of [14933].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14958 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d2ef2cd9ccf548807d0d36ef7e0767190f7fef97 1 parent f32feaf
Alex Gaynor authored December 18, 2010
348  tests/regressiontests/forms/localflavor/br.py
... ...
@@ -1,220 +1,144 @@
1  
-# -*- coding: utf-8 -*-
2  
-# Tests for the contrib/localflavor/ BR form fields.
  1
+from django.contrib.localflavor.br.forms import (BRZipCodeField,
  2
+    BRCNPJField, BRCPFField, BRPhoneNumberField, BRStateSelect,
  3
+    BRStateChoiceField)
3 4
 
4  
-tests = r"""
5  
-# BRZipCodeField ############################################################
6  
->>> from django.contrib.localflavor.br.forms import BRZipCodeField
7  
->>> f = BRZipCodeField()
8  
->>> f.clean('12345-123')
9  
-u'12345-123'
10  
->>> f.clean('12345_123')
11  
-Traceback (most recent call last):
12  
-...
13  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
14  
->>> f.clean('1234-123')
15  
-Traceback (most recent call last):
16  
-...
17  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
18  
->>> f.clean('abcde-abc')
19  
-Traceback (most recent call last):
20  
-...
21  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
22  
->>> f.clean('12345-')
23  
-Traceback (most recent call last):
24  
-...
25  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
26  
->>> f.clean('-123')
27  
-Traceback (most recent call last):
28  
-...
29  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
30  
->>> f.clean('')
31  
-Traceback (most recent call last):
32  
-...
33  
-ValidationError: [u'This field is required.']
34  
->>> f.clean(None)
35  
-Traceback (most recent call last):
36  
-...
37  
-ValidationError: [u'This field is required.']
  5
+from utils import LocalFlavorTestCase
38 6
 
39  
->>> f = BRZipCodeField(required=False)
40  
->>> f.clean(None)
41  
-u''
42  
->>> f.clean('')
43  
-u''
44  
->>> f.clean('-123')
45  
-Traceback (most recent call last):
46  
-...
47  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
48  
->>> f.clean('12345-')
49  
-Traceback (most recent call last):
50  
-...
51  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
52  
->>> f.clean('abcde-abc')
53  
-Traceback (most recent call last):
54  
-...
55  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
56  
->>> f.clean('1234-123')
57  
-Traceback (most recent call last):
58  
-...
59  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
60  
->>> f.clean('12345_123')
61  
-Traceback (most recent call last):
62  
-...
63  
-ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
64  
->>> f.clean('12345-123')
65  
-u'12345-123'
66 7
 
67  
-# BRCNPJField ############################################################
  8
+class BRLocalFlavorTests(LocalFlavorTestCase):
  9
+    def test_BRZipCodeField(self):
  10
+        error_format = [u'Enter a zip code in the format XXXXX-XXX.']
  11
+        valid = {
  12
+            '12345-123': '12345-123',
  13
+        }
  14
+        invalid = {
  15
+            '12345_123': error_format,
  16
+            '1234-123': error_format,
  17
+            'abcde-abc': error_format,
  18
+            '12345-': error_format,
  19
+            '-123': error_format,
  20
+        }
  21
+        self.assertFieldOutput(BRZipCodeField, valid, invalid)
68 22
 
69  
->>> from django.contrib.localflavor.br.forms import BRCNPJField
70  
->>> f = BRCNPJField(required=True)
71  
->>> f.clean('')
72  
-Traceback (most recent call last):
73  
-...
74  
-ValidationError: [u'This field is required.']
75  
->>> f.clean('12-345-678/9012-10')
76  
-Traceback (most recent call last):
77  
-...
78  
-ValidationError: [u'Invalid CNPJ number.']
79  
->>> f.clean('12.345.678/9012-10')
80  
-Traceback (most recent call last):
81  
-...
82  
-ValidationError: [u'Invalid CNPJ number.']
83  
->>> f.clean('12345678/9012-10')
84  
-Traceback (most recent call last):
85  
-...
86  
-ValidationError: [u'Invalid CNPJ number.']
87  
->>> f.clean('64.132.916/0001-88')
88  
-'64.132.916/0001-88'
89  
->>> f.clean('64-132-916/0001-88')
90  
-'64-132-916/0001-88'
91  
->>> f.clean('64132916/0001-88')
92  
-'64132916/0001-88'
93  
->>> f.clean('64.132.916/0001-XX')
94  
-Traceback (most recent call last):
95  
-...
96  
-ValidationError: [u'This field requires only numbers.']
97  
->>> f = BRCNPJField(required=False)
98  
->>> f.clean('')
99  
-u''
  23
+    def test_BRCNPJField(self):
  24
+        error_format = [u'Invalid CNPJ number.']
  25
+        error_numbersonly = [u'This field requires only numbers.']
  26
+        valid = {
  27
+            '64.132.916/0001-88': '64.132.916/0001-88',
  28
+            '64-132-916/0001-88': '64-132-916/0001-88',
  29
+            '64132916/0001-88': '64132916/0001-88',
  30
+        }
  31
+        invalid = {
  32
+            '12-345-678/9012-10': error_format,
  33
+            '12.345.678/9012-10': error_format,
  34
+            '12345678/9012-10': error_format,
  35
+            '64.132.916/0001-XX': error_numbersonly,
  36
+        }
  37
+        self.assertFieldOutput(BRCNPJField, valid, invalid)
100 38
 
101  
-# BRCPFField #################################################################
  39
+    def test_BRCPFField(self):
  40
+        error_format = [u'Invalid CPF number.']
  41
+        error_numbersonly = [u'This field requires only numbers.']
  42
+        error_atmost_chars = [u'Ensure this value has at most 14 characters (it has 15).']
  43
+        error_atleast_chars = [u'Ensure this value has at least 11 characters (it has 10).']
  44
+        error_atmost = [u'This field requires at most 11 digits or 14 characters.']
  45
+        valid = {
  46
+            '663.256.017-26': '663.256.017-26',
  47
+            '66325601726': '66325601726',
  48
+            '375.788.573-20': '375.788.573-20',
  49
+            '84828509895': '84828509895',
  50
+        }
  51
+        invalid = {
  52
+            '489.294.654-54': error_format,
  53
+            '295.669.575-98': error_format,
  54
+            '539.315.127-22': error_format,
  55
+            '375.788.573-XX': error_numbersonly,
  56
+            '375.788.573-000': error_atmost_chars,
  57
+            '123.456.78': error_atleast_chars,
  58
+            '123456789555': error_atmost,
  59
+        }
  60
+        self.assertFieldOutput(BRCPFField, valid, invalid)
102 61
 
103  
->>> from django.contrib.localflavor.br.forms import BRCPFField
104  
->>> f = BRCPFField()
105  
->>> f.clean('')
106  
-Traceback (most recent call last):
107  
-...
108  
-ValidationError: [u'This field is required.']
109  
->>> f.clean(None)
110  
-Traceback (most recent call last):
111  
-...
112  
-ValidationError: [u'This field is required.']
113  
->>> f.clean('489.294.654-54')
114  
-Traceback (most recent call last):
115  
-...
116  
-ValidationError: [u'Invalid CPF number.']
117  
->>> f.clean('295.669.575-98')
118  
-Traceback (most recent call last):
119  
-...
120  
-ValidationError: [u'Invalid CPF number.']
121  
->>> f.clean('539.315.127-22')
122  
-Traceback (most recent call last):
123  
-...
124  
-ValidationError: [u'Invalid CPF number.']
125  
->>> f.clean('663.256.017-26')
126  
-u'663.256.017-26'
127  
->>> f.clean('66325601726')
128  
-u'66325601726'
129  
->>> f.clean('375.788.573-20')
130  
-u'375.788.573-20'
131  
->>> f.clean('84828509895')
132  
-u'84828509895'
133  
->>> f.clean('375.788.573-XX')
134  
-Traceback (most recent call last):
135  
-...
136  
-ValidationError: [u'This field requires only numbers.']
137  
->>> f.clean('375.788.573-000')
138  
-Traceback (most recent call last):
139  
-...
140  
-ValidationError: [u'Ensure this value has at most 14 characters (it has 15).']
141  
->>> f.clean('123.456.78')
142  
-Traceback (most recent call last):
143  
-...
144  
-ValidationError: [u'Ensure this value has at least 11 characters (it has 10).']
145  
->>> f.clean('123456789555')
146  
-Traceback (most recent call last):
147  
-...
148  
-ValidationError: [u'This field requires at most 11 digits or 14 characters.']
149  
->>> f = BRCPFField(required=False)
150  
->>> f.clean('')
151  
-u''
152  
->>> f.clean(None)
153  
-u''
  62
+    def test_BRPhoneNumberField(self):
  63
+        # TODO: this doesn't test for any invalid inputs.
  64
+        valid = {
  65
+            '41-3562-3464': u'41-3562-3464',
  66
+            '4135623464': u'41-3562-3464',
  67
+            '41 3562-3464': u'41-3562-3464',
  68
+            '41 3562 3464': u'41-3562-3464',
  69
+            '(41) 3562 3464': u'41-3562-3464',
  70
+            '41.3562.3464': u'41-3562-3464',
  71
+            '41.3562-3464': u'41-3562-3464',
  72
+            ' (41) 3562.3464': u'41-3562-3464',
  73
+        }
  74
+        invalid = {}
  75
+        self.assertFieldOutput(BRPhoneNumberField, valid, invalid)
154 76
 
155  
-# BRPhoneNumberField #########################################################
  77
+    def test_BRStateSelect(self):
  78
+        f = BRStateSelect()
  79
+        out = u'''<select name="states">
  80
+<option value="AC">Acre</option>
  81
+<option value="AL">Alagoas</option>
  82
+<option value="AP">Amap\xe1</option>
  83
+<option value="AM">Amazonas</option>
  84
+<option value="BA">Bahia</option>
  85
+<option value="CE">Cear\xe1</option>
  86
+<option value="DF">Distrito Federal</option>
  87
+<option value="ES">Esp\xedrito Santo</option>
  88
+<option value="GO">Goi\xe1s</option>
  89
+<option value="MA">Maranh\xe3o</option>
  90
+<option value="MT">Mato Grosso</option>
  91
+<option value="MS">Mato Grosso do Sul</option>
  92
+<option value="MG">Minas Gerais</option>
  93
+<option value="PA">Par\xe1</option>
  94
+<option value="PB">Para\xedba</option>
  95
+<option value="PR" selected="selected">Paran\xe1</option>
  96
+<option value="PE">Pernambuco</option>
  97
+<option value="PI">Piau\xed</option>
  98
+<option value="RJ">Rio de Janeiro</option>
  99
+<option value="RN">Rio Grande do Norte</option>
  100
+<option value="RS">Rio Grande do Sul</option>
  101
+<option value="RO">Rond\xf4nia</option>
  102
+<option value="RR">Roraima</option>
  103
+<option value="SC">Santa Catarina</option>
  104
+<option value="SP">S\xe3o Paulo</option>
  105
+<option value="SE">Sergipe</option>
  106
+<option value="TO">Tocantins</option>
  107
+</select>'''
  108
+        self.assertEqual(f.render('states', 'PR'), out)
156 109
 
157  
->>> from django.contrib.localflavor.br.forms import BRPhoneNumberField
158  
->>> f = BRPhoneNumberField()
159  
->>> f.clean('41-3562-3464')
160  
-u'41-3562-3464'
161  
->>> f.clean('4135623464')
162  
-u'41-3562-3464'
163  
->>> f.clean('41 3562-3464')
164  
-u'41-3562-3464'
165  
->>> f.clean('41 3562 3464')
166  
-u'41-3562-3464'
167  
->>> f.clean('(41) 3562 3464')
168  
-u'41-3562-3464'
169  
->>> f.clean('41.3562.3464')
170  
-u'41-3562-3464'
171  
->>> f.clean('41.3562-3464')
172  
-u'41-3562-3464'
173  
->>> f.clean(' (41) 3562.3464')
174  
-u'41-3562-3464'
175  
->>> f.clean(None)
176  
-Traceback (most recent call last):
177  
-...
178  
-ValidationError: [u'This field is required.']
179  
->>> f.clean('')
180  
-Traceback (most recent call last):
181  
-...
182  
-ValidationError: [u'This field is required.']
183  
-
184  
->>> f = BRPhoneNumberField(required=False)
185  
->>> f.clean('')
186  
-u''
187  
->>> f.clean(None)
188  
-u''
189  
->>> f.clean(' (41) 3562.3464')
190  
-u'41-3562-3464'
191  
->>> f.clean('41.3562-3464')
192  
-u'41-3562-3464'
193  
->>> f.clean('(41) 3562 3464')
194  
-u'41-3562-3464'
195  
->>> f.clean('4135623464')
196  
-u'41-3562-3464'
197  
->>> f.clean('41 3562-3464')
198  
-u'41-3562-3464'
199  
-
200  
-# BRStateSelect ##############################################################
201  
-
202  
->>> from django.contrib.localflavor.br.forms import BRStateSelect
203  
->>> w = BRStateSelect()
204  
->>> w.render('states', 'PR')
205  
-u'<select name="states">\n<option value="AC">Acre</option>\n<option value="AL">Alagoas</option>\n<option value="AP">Amap\xe1</option>\n<option value="AM">Amazonas</option>\n<option value="BA">Bahia</option>\n<option value="CE">Cear\xe1</option>\n<option value="DF">Distrito Federal</option>\n<option value="ES">Esp\xedrito Santo</option>\n<option value="GO">Goi\xe1s</option>\n<option value="MA">Maranh\xe3o</option>\n<option value="MT">Mato Grosso</option>\n<option value="MS">Mato Grosso do Sul</option>\n<option value="MG">Minas Gerais</option>\n<option value="PA">Par\xe1</option>\n<option value="PB">Para\xedba</option>\n<option value="PR" selected="selected">Paran\xe1</option>\n<option value="PE">Pernambuco</option>\n<option value="PI">Piau\xed</option>\n<option value="RJ">Rio de Janeiro</option>\n<option value="RN">Rio Grande do Norte</option>\n<option value="RS">Rio Grande do Sul</option>\n<option value="RO">Rond\xf4nia</option>\n<option value="RR">Roraima</option>\n<option value="SC">Santa Catarina</option>\n<option value="SP">S\xe3o Paulo</option>\n<option value="SE">Sergipe</option>\n<option value="TO">Tocantins</option>\n</select>'
206  
-
207  
-# BRStateChoiceField #########################################################
208  
->>> from django.contrib.localflavor.br.forms import BRStateChoiceField
209  
->>> f = BRStateChoiceField()
210  
->>> ', '.join([f.clean(s) for s, _ in f.widget.choices])
211  
-u'AC, AL, AP, AM, BA, CE, DF, ES, GO, MA, MT, MS, MG, PA, PB, PR, PE, PI, RJ, RN, RS, RO, RR, SC, SP, SE, TO'
212  
->>> f.clean('')
213  
-Traceback (most recent call last):
214  
-...
215  
-ValidationError: [u'This field is required.']
216  
->>> f.clean('pr')
217  
-Traceback (most recent call last):
218  
-...
219  
-ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.']
220  
-"""
  110
+    def test_BRStateChoiceField(self):
  111
+        error_invalid = [u'Select a valid brazilian state. That state is not one of the available states.']
  112
+        valid = {
  113
+            'AC': 'AC',
  114
+            'AL': 'AL',
  115
+            'AP': 'AP',
  116
+            'AM': 'AM',
  117
+            'BA': 'BA',
  118
+            'CE': 'CE',
  119
+            'DF': 'DF',
  120
+            'ES': 'ES',
  121
+            'GO': 'GO',
  122
+            'MA': 'MA',
  123
+            'MT': 'MT',
  124
+            'MS': 'MS',
  125
+            'MG': 'MG',
  126
+            'PA': 'PA',
  127
+            'PB': 'PB',
  128
+            'PR': 'PR',
  129
+            'PE': 'PE',
  130
+            'PI': 'PI',
  131
+            'RJ': 'RJ',
  132
+            'RN': 'RN',
  133
+            'RS': 'RS',
  134
+            'RO': 'RO',
  135
+            'RR': 'RR',
  136
+            'SC': 'SC',
  137
+            'SP': 'SP',
  138
+            'SE': 'SE',
  139
+            'TO': 'TO',
  140
+        }
  141
+        invalid = {
  142
+            'pr': error_invalid,
  143
+        }
  144
+        self.assertFieldOutput(BRStateChoiceField, valid, invalid)
3  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,5 +1,4 @@
1 1
 # -*- coding: utf-8 -*-
2  
-from localflavor.br import tests as localflavor_br_tests
3 2
 from localflavor.ca import tests as localflavor_ca_tests
4 3
 from localflavor.ch import tests as localflavor_ch_tests
5 4
 from localflavor.cl import tests as localflavor_cl_tests
@@ -28,11 +27,11 @@
28 27
 from localflavor.ar import ARLocalFlavorTests
29 28
 from localflavor.au import AULocalFlavorTests
30 29
 from localflavor.at import ATLocalFlavorTests
  30
+from localflavor.br import BRLocalFlavorTests
31 31
 from localflavor.de import DELocalFlavorTests
32 32
 
33 33
 
34 34
 __test__ = {
35  
-    'localflavor_br_tests': localflavor_br_tests,
36 35
     'localflavor_ca_tests': localflavor_ca_tests,
37 36
     'localflavor_ch_tests': localflavor_ch_tests,
38 37
     'localflavor_cl_tests': localflavor_cl_tests,
1  tests/regressiontests/forms/tests/__init__.py
@@ -16,5 +16,6 @@
16 16
     ARLocalFlavorTests,
17 17
     ATLocalFlavorTests,
18 18
     AULocalFlavorTests,
  19
+    BRLocalFlavorTests,
19 20
     DELocalFlavorTests,
20 21
 )

0 notes on commit d2ef2cd

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