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 Chilean localflavor doctests to unittests. We have …

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

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14961 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cae809bb3c947f1cf40acfb89730e6b2f8540967 1 parent 177b090
Alex Gaynor authored December 18, 2010
130  tests/regressiontests/forms/localflavor/cl.py
... ...
@@ -1,74 +1,56 @@
1  
-
2  
-# Tests for the contrib/localflavor/ CL form fields.
3  
-
4  
-tests = r"""
5  
-## CLRutField #############################################################
6  
-
7  
-CLRutField is a Field that checks the validity of the Chilean
8  
-personal identification number (RUT). It has two modes relaxed (default) and
9  
-strict.
10  
-
11  
->>> from django.contrib.localflavor.cl.forms import CLRutField
12  
->>> rut = CLRutField()
13  
-
14  
->>> rut.clean('11-6')
15  
-u'11-6'
16  
->>> rut.clean('116')
17  
-u'11-6'
18  
-
19  
-# valid format, bad verifier.
20  
->>> rut.clean('11.111.111-0')
21  
-Traceback (most recent call last):
22  
-...
23  
-ValidationError: [u'The Chilean RUT is not valid.']
24  
->>> rut.clean('111')
25  
-Traceback (most recent call last):
26  
-...
27  
-ValidationError: [u'The Chilean RUT is not valid.']
28  
-
29  
->>> rut.clean('767484100')
30  
-u'76.748.410-0'
31  
->>> rut.clean('78.412.790-7')
32  
-u'78.412.790-7'
33  
->>> rut.clean('8.334.6043')
34  
-u'8.334.604-3'
35  
->>> rut.clean('76793310-K')
36  
-u'76.793.310-K'
37  
-
38  
-Strict RUT usage (does not allow imposible values)
39  
->>> rut = CLRutField(strict=True)
40  
-
41  
->>> rut.clean('11-6')
42  
-Traceback (most recent call last):
43  
-...
44  
-ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
45  
-
46  
-# valid format, bad verifier.
47  
->>> rut.clean('11.111.111-0')
48  
-Traceback (most recent call last):
49  
-...
50  
-ValidationError: [u'The Chilean RUT is not valid.']
51  
-
52  
-# Correct input, invalid format.
53  
->>> rut.clean('767484100')
54  
-Traceback (most recent call last):
55  
-...
56  
-ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
57  
->>> rut.clean('78.412.790-7')
58  
-u'78.412.790-7'
59  
->>> rut.clean('8.334.6043')
60  
-Traceback (most recent call last):
61  
-...
62  
-ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
63  
->>> rut.clean('76793310-K')
64  
-Traceback (most recent call last):
65  
-...
66  
-ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
67  
-
68  
-## CLRegionSelect #########################################################
69  
->>> from django.contrib.localflavor.cl.forms import CLRegionSelect
70  
->>> f = CLRegionSelect()
71  
-
72  
->>> f.render('foo', 'bar')
73  
-u'<select name="foo">\n<option value="RM">Regi\xf3n Metropolitana de Santiago</option>\n<option value="I">Regi\xf3n de Tarapac\xe1</option>\n<option value="II">Regi\xf3n de Antofagasta</option>\n<option value="III">Regi\xf3n de Atacama</option>\n<option value="IV">Regi\xf3n de Coquimbo</option>\n<option value="V">Regi\xf3n de Valpara\xedso</option>\n<option value="VI">Regi\xf3n del Libertador Bernardo O&#39;Higgins</option>\n<option value="VII">Regi\xf3n del Maule</option>\n<option value="VIII">Regi\xf3n del B\xedo B\xedo</option>\n<option value="IX">Regi\xf3n de la Araucan\xeda</option>\n<option value="X">Regi\xf3n de los Lagos</option>\n<option value="XI">Regi\xf3n de Ays\xe9n del General Carlos Ib\xe1\xf1ez del Campo</option>\n<option value="XII">Regi\xf3n de Magallanes y la Ant\xe1rtica Chilena</option>\n<option value="XIV">Regi\xf3n de Los R\xedos</option>\n<option value="XV">Regi\xf3n de Arica-Parinacota</option>\n</select>'
74  
-"""
  1
+from django.contrib.localflavor.cl.forms import CLRutField, CLRegionSelect
  2
+from django.core.exceptions import ValidationError
  3
+
  4
+from utils import LocalFlavorTestCase
  5
+
  6
+
  7
+class CLLocalFlavorTests(LocalFlavorTestCase):
  8
+    def test_CLRegionSelect(self):
  9
+        f = CLRegionSelect()
  10
+        out = u'''<select name="foo">
  11
+<option value="RM">Regi\xf3n Metropolitana de Santiago</option>
  12
+<option value="I">Regi\xf3n de Tarapac\xe1</option>
  13
+<option value="II">Regi\xf3n de Antofagasta</option>
  14
+<option value="III">Regi\xf3n de Atacama</option>
  15
+<option value="IV">Regi\xf3n de Coquimbo</option>
  16
+<option value="V">Regi\xf3n de Valpara\xedso</option>
  17
+<option value="VI">Regi\xf3n del Libertador Bernardo O&#39;Higgins</option>
  18
+<option value="VII">Regi\xf3n del Maule</option>
  19
+<option value="VIII">Regi\xf3n del B\xedo B\xedo</option>
  20
+<option value="IX">Regi\xf3n de la Araucan\xeda</option>
  21
+<option value="X">Regi\xf3n de los Lagos</option>
  22
+<option value="XI">Regi\xf3n de Ays\xe9n del General Carlos Ib\xe1\xf1ez del Campo</option>
  23
+<option value="XII">Regi\xf3n de Magallanes y la Ant\xe1rtica Chilena</option>
  24
+<option value="XIV">Regi\xf3n de Los R\xedos</option>
  25
+<option value="XV">Regi\xf3n de Arica-Parinacota</option>
  26
+</select>'''
  27
+        self.assertEqual(f.render('foo', 'bar'), out)
  28
+
  29
+    def test_CLRutField(self):
  30
+        error_invalid =  [u'The Chilean RUT is not valid.']
  31
+        error_format = [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
  32
+        valid = {
  33
+            '11-6': '11-6',
  34
+            '116': '11-6',
  35
+            '767484100': '76.748.410-0',
  36
+            '78.412.790-7': '78.412.790-7',
  37
+            '8.334.6043': '8.334.604-3',
  38
+            '76793310-K': '76.793.310-K',
  39
+        }
  40
+        invalid = {
  41
+            '11.111.111-0': error_invalid,
  42
+            '111': error_invalid,
  43
+        }
  44
+        self.assertFieldOutput(CLRutField, valid, invalid)
  45
+
  46
+        # deal with special "Strict Mode".
  47
+        invalid = {
  48
+            '11-6': error_format,
  49
+            '767484100': error_format,
  50
+            '8.334.6043': error_format,
  51
+            '76793310-K': error_format,
  52
+            '11.111.111-0': error_invalid
  53
+        }
  54
+        self.assertFieldOutput(CLRutField,
  55
+            {}, invalid, field_kwargs={"strict": True}
  56
+        )
13  tests/regressiontests/forms/localflavor/utils.py
@@ -5,7 +5,8 @@
5 5
 
6 6
 
7 7
 class LocalFlavorTestCase(TestCase):
8  
-    def assertFieldOutput(self, fieldclass, valid, invalid):
  8
+    def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
  9
+        field_kwargs={}, empty_value=u''):
9 10
         """Asserts that a field behaves correctly with various inputs.
10 11
 
11 12
         Args:
@@ -14,10 +15,12 @@ def assertFieldOutput(self, fieldclass, valid, invalid):
14 15
                     cleaned values.
15 16
             invalid: a dictionary mapping invalid inputs to one or more
16 17
                     raised error messages.
  18
+            fieldargs: the args passed to instantiate the field
  19
+            fieldkwargs: the kwargs passed to instantiate the field
  20
+            emptyvalue: the expected clean output for inputs in EMPTY_VALUES
17 21
         """
18  
-
19  
-        required = fieldclass()
20  
-        optional = fieldclass(required=False)
  22
+        required = fieldclass(*field_args, **field_kwargs)
  23
+        optional = fieldclass(*field_args, required=False, **field_kwargs)
21 24
         # test valid inputs
22 25
         for input, output in valid.items():
23 26
             self.assertEqual(required.clean(input), output)
@@ -45,4 +48,4 @@ def assertFieldOutput(self, fieldclass, valid, invalid):
45 48
                 self.assertEqual(error_required, e.messages)
46 49
             else:
47 50
                 self.fail()
48  
-            self.assertEqual(optional.clean(val), u'')
  51
+            self.assertEqual(optional.clean(val), empty_value)
3  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,5 +1,4 @@
1 1
 # -*- coding: utf-8 -*-
2  
-from localflavor.cl import tests as localflavor_cl_tests
3 2
 from localflavor.cz import tests as localflavor_cz_tests
4 3
 from localflavor.es import tests as localflavor_es_tests
5 4
 from localflavor.fi import tests as localflavor_fi_tests
@@ -28,11 +27,11 @@
28 27
 from localflavor.br import BRLocalFlavorTests
29 28
 from localflavor.ca import CALocalFlavorTests
30 29
 from localflavor.ch import CHLocalFlavorTests
  30
+from localflavor.cl import CLLocalFlavorTests
31 31
 from localflavor.de import DELocalFlavorTests
32 32
 
33 33
 
34 34
 __test__ = {
35  
-    'localflavor_cl_tests': localflavor_cl_tests,
36 35
     'localflavor_cz_tests': localflavor_cz_tests,
37 36
     'localflavor_es_tests': localflavor_es_tests,
38 37
     'localflavor_fi_tests': localflavor_fi_tests,
1  tests/regressiontests/forms/tests/__init__.py
@@ -19,5 +19,6 @@
19 19
     BRLocalFlavorTests,
20 20
     CALocalFlavorTests,
21 21
     CHLocalFlavorTests,
  22
+    CLLocalFlavorTests,
22 23
     DELocalFlavorTests,
23 24
 )

0 notes on commit cae809b

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