Converted Chilean localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14936 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 20:30:05 +00:00
parent 7fdfec7a3c
commit cc8c3ebb7d
4 changed files with 62 additions and 78 deletions

View File

@ -1,74 +1,56 @@
from django.contrib.localflavor.cl.forms import CLRutField, CLRegionSelect
from django.core.exceptions import ValidationError
# Tests for the contrib/localflavor/ CL form fields. from utils import LocalFlavorTestCase
tests = r"""
## CLRutField #############################################################
CLRutField is a Field that checks the validity of the Chilean class CLLocalFlavorTests(LocalFlavorTestCase):
personal identification number (RUT). It has two modes relaxed (default) and def test_CLRegionSelect(self):
strict. f = CLRegionSelect()
out = u'''<select name="foo">
<option value="RM">Regi\xf3n Metropolitana de Santiago</option>
<option value="I">Regi\xf3n de Tarapac\xe1</option>
<option value="II">Regi\xf3n de Antofagasta</option>
<option value="III">Regi\xf3n de Atacama</option>
<option value="IV">Regi\xf3n de Coquimbo</option>
<option value="V">Regi\xf3n de Valpara\xedso</option>
<option value="VI">Regi\xf3n del Libertador Bernardo O&#39;Higgins</option>
<option value="VII">Regi\xf3n del Maule</option>
<option value="VIII">Regi\xf3n del B\xedo B\xedo</option>
<option value="IX">Regi\xf3n de la Araucan\xeda</option>
<option value="X">Regi\xf3n de los Lagos</option>
<option value="XI">Regi\xf3n de Ays\xe9n del General Carlos Ib\xe1\xf1ez del Campo</option>
<option value="XII">Regi\xf3n de Magallanes y la Ant\xe1rtica Chilena</option>
<option value="XIV">Regi\xf3n de Los R\xedos</option>
<option value="XV">Regi\xf3n de Arica-Parinacota</option>
</select>'''
self.assertEqual(f.render('foo', 'bar'), out)
>>> from django.contrib.localflavor.cl.forms import CLRutField def test_CLRutField(self):
>>> rut = CLRutField() error_invalid = [u'The Chilean RUT is not valid.']
error_format = [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
valid = {
'11-6': '11-6',
'116': '11-6',
'767484100': '76.748.410-0',
'78.412.790-7': '78.412.790-7',
'8.334.6043': '8.334.604-3',
'76793310-K': '76.793.310-K',
}
invalid = {
'11.111.111-0': error_invalid,
'111': error_invalid,
}
self.assertFieldOutput(CLRutField, valid, invalid)
>>> rut.clean('11-6') # deal with special "Strict Mode".
u'11-6' invalid = {
>>> rut.clean('116') '11-6': error_format,
u'11-6' '767484100': error_format,
'8.334.6043': error_format,
# valid format, bad verifier. '76793310-K': error_format,
>>> rut.clean('11.111.111-0') '11.111.111-0': error_invalid
Traceback (most recent call last): }
... self.assertFieldOutput(CLRutField,
ValidationError: [u'The Chilean RUT is not valid.'] {}, invalid, field_kwargs={"strict": True}
>>> rut.clean('111') )
Traceback (most recent call last):
...
ValidationError: [u'The Chilean RUT is not valid.']
>>> rut.clean('767484100')
u'76.748.410-0'
>>> rut.clean('78.412.790-7')
u'78.412.790-7'
>>> rut.clean('8.334.6043')
u'8.334.604-3'
>>> rut.clean('76793310-K')
u'76.793.310-K'
Strict RUT usage (does not allow imposible values)
>>> rut = CLRutField(strict=True)
>>> rut.clean('11-6')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
# valid format, bad verifier.
>>> rut.clean('11.111.111-0')
Traceback (most recent call last):
...
ValidationError: [u'The Chilean RUT is not valid.']
# Correct input, invalid format.
>>> rut.clean('767484100')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
>>> rut.clean('78.412.790-7')
u'78.412.790-7'
>>> rut.clean('8.334.6043')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
>>> rut.clean('76793310-K')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.']
## CLRegionSelect #########################################################
>>> from django.contrib.localflavor.cl.forms import CLRegionSelect
>>> f = CLRegionSelect()
>>> f.render('foo', 'bar')
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>'
"""

View File

@ -4,7 +4,8 @@ from django.utils.unittest import TestCase
class LocalFlavorTestCase(TestCase): class LocalFlavorTestCase(TestCase):
def assertFieldOutput(self, fieldclass, valid, invalid): def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
field_kwargs={}, empty_value=u''):
"""Asserts that a field behaves correctly with various inputs. """Asserts that a field behaves correctly with various inputs.
Args: Args:
@ -13,10 +14,12 @@ class LocalFlavorTestCase(TestCase):
cleaned values. cleaned values.
invalid: a dictionary mapping invalid inputs to one or more invalid: a dictionary mapping invalid inputs to one or more
raised error messages. raised error messages.
fieldargs: the args passed to instantiate the field
fieldkwargs: the kwargs passed to instantiate the field
emptyvalue: the expected clean output for inputs in EMPTY_VALUES
""" """
required = fieldclass(*field_args, **field_kwargs)
required = fieldclass() optional = fieldclass(*field_args, required=False, **field_kwargs)
optional = fieldclass(required=False)
# test valid inputs # test valid inputs
for input, output in valid.items(): for input, output in valid.items():
self.assertEqual(required.clean(input), output) self.assertEqual(required.clean(input), output)
@ -33,6 +36,5 @@ class LocalFlavorTestCase(TestCase):
error_required = u'This field is required' error_required = u'This field is required'
for e in EMPTY_VALUES: for e in EMPTY_VALUES:
self.assertRaisesRegexp(ValidationError, error_required, self.assertRaisesRegexp(ValidationError, error_required,
required.clean, e required.clean, e)
) self.assertEqual(optional.clean(e), empty_value)
self.assertEqual(optional.clean(e), u'')

View File

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from localflavor.cl import tests as localflavor_cl_tests
from localflavor.cz import tests as localflavor_cz_tests from localflavor.cz import tests as localflavor_cz_tests
from localflavor.es import tests as localflavor_es_tests from localflavor.es import tests as localflavor_es_tests
from localflavor.fi import tests as localflavor_fi_tests from localflavor.fi import tests as localflavor_fi_tests
@ -30,12 +29,12 @@ from localflavor.be import BELocalFlavorTests
from localflavor.br import BRLocalFlavorTests from localflavor.br import BRLocalFlavorTests
from localflavor.ca import CALocalFlavorTests from localflavor.ca import CALocalFlavorTests
from localflavor.ch import CHLocalFlavorTests from localflavor.ch import CHLocalFlavorTests
from localflavor.cl import CLLocalFlavorTests
from localflavor.il import ILLocalFlavorTests from localflavor.il import ILLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests from localflavor.tr import TRLocalFlavorTests
__test__ = { __test__ = {
'localflavor_cl_tests': localflavor_cl_tests,
'localflavor_cz_tests': localflavor_cz_tests, 'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_es_tests': localflavor_es_tests, 'localflavor_es_tests': localflavor_es_tests,
'localflavor_fi_tests': localflavor_fi_tests, 'localflavor_fi_tests': localflavor_fi_tests,

View File

@ -20,6 +20,7 @@ from regressiontests.forms.localflavortests import (
BRLocalFlavorTests, BRLocalFlavorTests,
CALocalFlavorTests, CALocalFlavorTests,
CHLocalFlavorTests, CHLocalFlavorTests,
CLLocalFlavorTests,
DELocalFlavorTests, DELocalFlavorTests,
ILLocalFlavorTests, ILLocalFlavorTests,
TRLocalFlavorTests, TRLocalFlavorTests,