From 575962c213c7e6b7393c9ba43ecbb11389ff7c8b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 12 Dec 2010 04:33:50 +0000 Subject: [PATCH] Fixed #14499 -- ATSocialSecurityNumberField now responds to all EMPTY_VALUES correctly. Also converted Austrian localflavor doctests to unittests. 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@14873 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/at/forms.py | 10 +- tests/regressiontests/forms/localflavor/at.py | 115 ++++++------------ .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 4 +- 4 files changed, 51 insertions(+), 81 deletions(-) diff --git a/django/contrib/localflavor/at/forms.py b/django/contrib/localflavor/at/forms.py index e428fdaa17..cfe02063c2 100644 --- a/django/contrib/localflavor/at/forms.py +++ b/django/contrib/localflavor/at/forms.py @@ -4,12 +4,14 @@ AT-specific Form helpers import re -from django.utils.translation import ugettext_lazy as _ -from django.forms.fields import Field, RegexField, Select +from django.core.validators import EMPTY_VALUES from django.forms import ValidationError +from django.forms.fields import Field, RegexField, Select +from django.utils.translation import ugettext_lazy as _ re_ssn = re.compile(r'^\d{4} \d{6}') + class ATZipCodeField(RegexField): """ A form field that validates its input is an Austrian postcode. @@ -49,6 +51,9 @@ class ATSocialSecurityNumberField(Field): } def clean(self, value): + value = super(ATSocialSecurityNumberField, self).clean(value) + if value in EMPTY_VALUES: + return u"" if not re_ssn.search(value): raise ValidationError(self.error_messages['invalid']) sqnr, date = value.split(" ") @@ -62,4 +67,3 @@ class ATSocialSecurityNumberField(Field): if res != int(check): raise ValidationError(self.error_messages['invalid']) return u'%s%s %s'%(sqnr, check, date,) - diff --git a/tests/regressiontests/forms/localflavor/at.py b/tests/regressiontests/forms/localflavor/at.py index 54ca46898e..3fa50ac4df 100644 --- a/tests/regressiontests/forms/localflavor/at.py +++ b/tests/regressiontests/forms/localflavor/at.py @@ -1,80 +1,45 @@ -# -*- coding: utf-8 -*- -# Tests for the contrib/localflavor/ AT form fields. +from django.contrib.localflavor.at.forms import (ATZipCodeField, ATStateSelect, + ATSocialSecurityNumberField) -tests = r""" -# ATZipCodeField ########################################################### - ->>> from django.contrib.localflavor.at.forms import ATZipCodeField ->>> f = ATZipCodeField() ->>> f.clean('1150') -u'1150' ->>> f.clean('4020') -u'4020' ->>> f.clean('8020') -u'8020' ->>> f.clean('111222') -Traceback (most recent call last): -... -ValidationError: [u'Enter a zip code in the format XXXX.'] ->>> f.clean('eeffee') -Traceback (most recent call last): -... -ValidationError: [u'Enter a zip code in the format XXXX.'] ->>> f.clean(u'') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] ->>> 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.'] +from utils import LocalFlavorTestCase ->>> f = ATZipCodeField(required=False) ->>> f.clean('1150') -u'1150' ->>> f.clean('4020') -u'4020' ->>> f.clean('8020') -u'8020' ->>> f.clean('111222') -Traceback (most recent call last): -... -ValidationError: [u'Enter a zip code in the format XXXX.'] ->>> f.clean('eeffee') -Traceback (most recent call last): -... -ValidationError: [u'Enter a zip code in the format XXXX.'] ->>> f.clean(None) -u'' ->>> f.clean('') -u'' ->>> f.clean(u'') -u'' +class ATLocalFlavorTests(LocalFlavorTestCase): + def test_ATStateSelect(self): + f = ATStateSelect() + out = u'''''' + self.assertEqual(f.render('bundesland', 'WI'), out) -# ATStateSelect ################################################################## + def test_ATZipCodeField(self): + error_format = [u'Enter a zip code in the format XXXX.'] + valid = { + '1150': '1150', + '4020': '4020', + '8020': '8020', + } + invalid = { + '111222': error_format, + 'eeffee': error_format, + } + self.assertFieldOutput(ATZipCodeField, valid, invalid) ->>> from django.contrib.localflavor.at.forms import ATStateSelect ->>> f = ATStateSelect() ->>> f.render('bundesland', 'WI') -u'' - -# ATSocialSecurityNumberField ################################################ - ->>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField ->>> f = ATSocialSecurityNumberField() ->>> f.clean('1237 010180') -u'1237 010180' ->>> f.clean('1237 010181') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] ->>> f.clean('12370 010180') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] -""" + def test_ATSocialSecurityNumberField(self): + error_format = [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.'] + valid = { + '1237 010180': '1237 010180', + } + invalid = { + '1237 010181': error_format, + '12370 010180': error_format, + } + self.assertFieldOutput(ATSocialSecurityNumberField, valid, invalid) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 7087cf0eb8..9a3d96f55f 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -from localflavor.at import tests as localflavor_at_tests from localflavor.au import tests as localflavor_au_tests from localflavor.br import tests as localflavor_br_tests from localflavor.ca import tests as localflavor_ca_tests @@ -28,6 +27,7 @@ from localflavor.uy import tests as localflavor_uy_tests from localflavor.za import tests as localflavor_za_tests from localflavor.ar import ARLocalFlavorTests +from localflavor.at import ATLocalFlavorTests from localflavor.de import DELocalFlavorTests from localflavor.be import BELocalFlavorTests from localflavor.il import ILLocalFlavorTests @@ -35,7 +35,6 @@ from localflavor.tr import TRLocalFlavorTests __test__ = { - 'localflavor_at_tests': localflavor_at_tests, 'localflavor_au_tests': localflavor_au_tests, 'localflavor_br_tests': localflavor_br_tests, 'localflavor_ca_tests': localflavor_ca_tests, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index caec967dd3..7489be5f9b 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -13,8 +13,10 @@ from widgets import * from regressiontests.forms.localflavortests import ( __test__, + ARLocalFlavorTests, + ATLocalFlavorTests, BELocalFlavorTests, DELocalFlavorTests, ILLocalFlavorTests, - TRLocalFlavorTests + TRLocalFlavorTests, )