From e7703398c4de1121616aecb857843665a6cbfa88 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 22:15:41 +0000 Subject: [PATCH] [1.2.X] Converted United Kingdom localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit. Backport of [14953]. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14977 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/localflavor/uk.py | 92 ++++++------------- .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 3 files changed, 30 insertions(+), 66 deletions(-) diff --git a/tests/regressiontests/forms/localflavor/uk.py b/tests/regressiontests/forms/localflavor/uk.py index d041e8f179..6fd536f936 100644 --- a/tests/regressiontests/forms/localflavor/uk.py +++ b/tests/regressiontests/forms/localflavor/uk.py @@ -1,66 +1,30 @@ -# -*- coding: utf-8 -*- -# Tests for the contrib/localflavor/ UK form fields. +from django.contrib.localflavor.uk.forms import UKPostcodeField -tests = r""" -# UKPostcodeField ############################################################# +from utils import LocalFlavorTestCase -UKPostcodeField validates that the data is a valid UK postcode. ->>> from django.contrib.localflavor.uk.forms import UKPostcodeField ->>> f = UKPostcodeField() ->>> f.clean('BT32 4PX') -u'BT32 4PX' ->>> f.clean('GIR 0AA') -u'GIR 0AA' ->>> f.clean('BT324PX') -u'BT32 4PX' ->>> f.clean('1NV 4L1D') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid postcode.'] ->>> f.clean('1NV4L1D') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid postcode.'] ->>> f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> f.clean(' so11aa ') -u'SO1 1AA' ->>> f.clean(' so1 1aa ') -u'SO1 1AA' ->>> f.clean('G2 3wt') -u'G2 3WT' ->>> f.clean('EC1A 1BB') -u'EC1A 1BB' ->>> f.clean('Ec1a1BB') -u'EC1A 1BB' ->>> f.clean(' b0gUS') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid postcode.'] ->>> f = UKPostcodeField(required=False) ->>> f.clean('BT32 4PX') -u'BT32 4PX' ->>> f.clean('GIR 0AA') -u'GIR 0AA' ->>> f.clean('1NV 4L1D') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid postcode.'] ->>> f.clean('BT324PX') -u'BT32 4PX' ->>> f.clean(None) -u'' ->>> f.clean('') -u'' ->>> f = UKPostcodeField(error_messages={'invalid': 'Enter a bloody postcode!'}) ->>> f.clean('1NV 4L1D') -Traceback (most recent call last): -... -ValidationError: [u'Enter a bloody postcode!'] -""" + +class UKLocalFlavorTests(LocalFlavorTestCase): + def test_UKPostcodeField(self): + error_invalid = [u'Enter a valid postcode.'] + valid = { + 'BT32 4PX': 'BT32 4PX', + 'GIR 0AA': 'GIR 0AA', + 'BT324PX': 'BT32 4PX', + ' so11aa ': 'SO1 1AA', + ' so1 1aa ': 'SO1 1AA', + 'G2 3wt': 'G2 3WT', + 'EC1A 1BB': 'EC1A 1BB', + 'Ec1a1BB': 'EC1A 1BB', + } + invalid = { + '1NV 4L1D': error_invalid, + '1NV4L1D': error_invalid, + ' b0gUS': error_invalid, + } + self.assertFieldOutput(UKPostcodeField, valid, invalid) + valid = {} + invalid = { + '1NV 4L1D': [u'Enter a bloody postcode!'], + } + kwargs = {'error_messages': {'invalid': 'Enter a bloody postcode!'}} + self.assertFieldOutput(UKPostcodeField, valid, invalid, field_kwargs=kwargs) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 1283d6e9d7..9204f42b64 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from localflavor.cz import tests as localflavor_cz_tests from localflavor.se import tests as localflavor_se_tests -from localflavor.uk import tests as localflavor_uk_tests from localflavor.us import tests as localflavor_us_tests from localflavor.uy import tests as localflavor_uy_tests from localflavor.za import tests as localflavor_za_tests @@ -30,12 +29,12 @@ from localflavor.pl import PLLocalFlavorTests from localflavor.pt import PTLocalFlavorTests from localflavor.ro import ROLocalFlavorTests from localflavor.sk import SKLocalFlavorTests +from localflavor.uk import UKLocalFlavorTests __test__ = { 'localflavor_cz_tests': localflavor_cz_tests, 'localflavor_se_tests': localflavor_se_tests, - 'localflavor_uk_tests': localflavor_uk_tests, 'localflavor_us_tests': localflavor_us_tests, 'localflavor_uy_tests': localflavor_uy_tests, 'localflavor_za_tests': localflavor_za_tests, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 01bd657edc..2b8ededa82 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -36,4 +36,5 @@ from regressiontests.forms.localflavortests import ( PTLocalFlavorTests, ROLocalFlavorTests, SKLocalFlavorTests, + UKLocalFlavorTests, )