Began converting localflavor doctests into unittests, starting with the German ones. Also introduced a new base class to facilitate ease of testing form fields. 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@14626 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-11-19 19:33:07 +00:00
parent 59c84b4391
commit a3e7ee7c40
4 changed files with 85 additions and 38 deletions

View File

@ -1,35 +1,49 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ DE form fields.
from django.contrib.localflavor.de.forms import (DEZipCodeField, DEStateSelect,
DEIdentityCardNumberField)
tests = r"""
# DEZipCodeField ##############################################################
from utils import LocalFlavorTestCase
>>> from django.contrib.localflavor.de.forms import DEZipCodeField
>>> f = DEZipCodeField()
>>> f.clean('99423')
u'99423'
>>> f.clean(' 99423')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXXX.']
# DEStateSelect #############################################################
class DELocalFlavorTests(LocalFlavorTestCase):
def test_DEStateSelect(self):
f = DEStateSelect()
out = u'''<select name="states">
<option value="BW">Baden-Wuerttemberg</option>
<option value="BY">Bavaria</option>
<option value="BE">Berlin</option>
<option value="BB">Brandenburg</option>
<option value="HB">Bremen</option>
<option value="HH">Hamburg</option>
<option value="HE">Hessen</option>
<option value="MV">Mecklenburg-Western Pomerania</option>
<option value="NI">Lower Saxony</option>
<option value="NW">North Rhine-Westphalia</option>
<option value="RP">Rhineland-Palatinate</option>
<option value="SL">Saarland</option>
<option value="SN">Saxony</option>
<option value="ST">Saxony-Anhalt</option>
<option value="SH">Schleswig-Holstein</option>
<option value="TH" selected="selected">Thuringia</option>
</select>'''
self.assertEqual(f.render('states', 'TH'), out)
>>> from django.contrib.localflavor.de.forms import DEStateSelect
>>> w = DEStateSelect()
>>> w.render('states', 'TH')
u'<select name="states">\n<option value="BW">Baden-Wuerttemberg</option>\n<option value="BY">Bavaria</option>\n<option value="BE">Berlin</option>\n<option value="BB">Brandenburg</option>\n<option value="HB">Bremen</option>\n<option value="HH">Hamburg</option>\n<option value="HE">Hessen</option>\n<option value="MV">Mecklenburg-Western Pomerania</option>\n<option value="NI">Lower Saxony</option>\n<option value="NW">North Rhine-Westphalia</option>\n<option value="RP">Rhineland-Palatinate</option>\n<option value="SL">Saarland</option>\n<option value="SN">Saxony</option>\n<option value="ST">Saxony-Anhalt</option>\n<option value="SH">Schleswig-Holstein</option>\n<option value="TH" selected="selected">Thuringia</option>\n</select>'
def test_DEZipCodeField(self):
error_format = [u'Enter a zip code in the format XXXXX.']
valid = {
'99423': '99423',
}
invalid = {
' 99423': error_format,
}
self.assertFieldOutput(DEZipCodeField, valid, invalid)
# DEIdentityCardNumberField #################################################
>>> from django.contrib.localflavor.de.forms import DEIdentityCardNumberField
>>> f = DEIdentityCardNumberField()
>>> f.clean('7549313035D-6004103-0903042-0')
u'7549313035D-6004103-0903042-0'
>>> f.clean('9786324830D 6104243 0910271 2')
u'9786324830D-6104243-0910271-2'
>>> f.clean('0434657485D-6407276-0508137-9')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.']
"""
def test_DEIdentityCardNumberField(self):
error_format = [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.']
valid = {
'7549313035D-6004103-0903042-0': '7549313035D-6004103-0903042-0',
'9786324830D 6104243 0910271 2': '9786324830D-6104243-0910271-2',
}
invalid = {
'0434657485D-6407276-0508137-9': error_format,
}
self.assertFieldOutput(DEIdentityCardNumberField, valid, invalid)

View File

@ -0,0 +1,38 @@
from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES
from django.utils.unittest import TestCase
class LocalFlavorTestCase(TestCase):
def assertFieldOutput(self, fieldclass, valid, invalid):
"""Asserts that a field behaves correctly with various inputs.
Args:
fieldclass: the class of the field to be tested.
valid: a dictionary mapping valid inputs to their expected
cleaned values.
invalid: a dictionary mapping invalid inputs to one or more
raised error messages.
"""
required = fieldclass()
optional = fieldclass(required=False)
# test valid inputs
for input, output in valid.items():
self.assertEqual(required.clean(input), output)
self.assertEqual(optional.clean(input), output)
# test invalid inputs
for input, errors in invalid.items():
self.assertRaisesRegexp(ValidationError, unicode(errors),
required.clean, input
)
self.assertRaisesRegexp(ValidationError, unicode(errors),
optional.clean, input
)
# test required inputs
error_required = u'This field is required'
for e in EMPTY_VALUES:
self.assertRaisesRegexp(ValidationError, error_required,
required.clean, e
)
self.assertEqual(optional.clean(e), u'')

View File

@ -7,7 +7,7 @@ from localflavor.ca import tests as localflavor_ca_tests
from localflavor.ch import tests as localflavor_ch_tests
from localflavor.cl import tests as localflavor_cl_tests
from localflavor.cz import tests as localflavor_cz_tests
from localflavor.de import tests as localflavor_de_tests
from localflavor.de import DELocalFlavorTests
from localflavor.es import tests as localflavor_es_tests
from localflavor.fi import tests as localflavor_fi_tests
from localflavor.fr import tests as localflavor_fr_tests
@ -41,7 +41,6 @@ __test__ = {
'localflavor_ch_tests': localflavor_ch_tests,
'localflavor_cl_tests': localflavor_cl_tests,
'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_de_tests': localflavor_de_tests,
'localflavor_es_tests': localflavor_es_tests,
'localflavor_fi_tests': localflavor_fi_tests,
'localflavor_fr_tests': localflavor_fr_tests,
@ -63,7 +62,3 @@ __test__ = {
'localflavor_uy_tests': localflavor_uy_tests,
'localflavor_za_tests': localflavor_za_tests,
}
if __name__ == "__main__":
import doctest
doctest.testmod()

View File

@ -11,5 +11,5 @@ from util import *
from validators import TestFieldWithValidators
from widgets import *
from regressiontests.forms.localflavortests import __test__
from regressiontests.forms.localflavortests import BETests, IsraelLocalFlavorTests
from regressiontests.forms.localflavortests import (__test__, BETests,
DELocalFlavorTests, IsraelLocalFlavorTests)