From a3e7ee7c4037a390d6ab24932e53682d1c394191 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 19 Nov 2010 19:33:07 +0000 Subject: [PATCH] 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 --- tests/regressiontests/forms/localflavor/de.py | 74 +++++++++++-------- .../forms/localflavor/utils.py | 38 ++++++++++ .../regressiontests/forms/localflavortests.py | 7 +- tests/regressiontests/forms/tests/__init__.py | 4 +- 4 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 tests/regressiontests/forms/localflavor/utils.py diff --git a/tests/regressiontests/forms/localflavor/de.py b/tests/regressiontests/forms/localflavor/de.py index e02323fc62..7c68bcc129 100644 --- a/tests/regressiontests/forms/localflavor/de.py +++ b/tests/regressiontests/forms/localflavor/de.py @@ -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'''''' + self.assertEqual(f.render('states', 'TH'), out) ->>> from django.contrib.localflavor.de.forms import DEStateSelect ->>> w = DEStateSelect() ->>> w.render('states', 'TH') -u'' + 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) diff --git a/tests/regressiontests/forms/localflavor/utils.py b/tests/regressiontests/forms/localflavor/utils.py new file mode 100644 index 0000000000..3297d4e10c --- /dev/null +++ b/tests/regressiontests/forms/localflavor/utils.py @@ -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'') diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 30c06ef79a..9dad13d096 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -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() diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 0e2df05488..fbd7a0d9a2 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -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)