From f8caeefff193e4ace0db13c7213194be933584f7 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:31:49 +0000 Subject: [PATCH] Fixed #14859 -- ITSocialSecurityNumberField and ITVatNumberField didn't handle all EMPTY_VALUES gracefully. Also converted the Italias localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14945 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/it/forms.py | 8 +- tests/regressiontests/forms/localflavor/it.py | 122 ++++++++++-------- .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 4 files changed, 71 insertions(+), 63 deletions(-) diff --git a/django/contrib/localflavor/it/forms.py b/django/contrib/localflavor/it/forms.py index baa56a21ba..bf0227608a 100644 --- a/django/contrib/localflavor/it/forms.py +++ b/django/contrib/localflavor/it/forms.py @@ -50,8 +50,8 @@ class ITSocialSecurityNumberField(RegexField): def clean(self, value): value = super(ITSocialSecurityNumberField, self).clean(value) - if value == u'': - return value + if value in EMPTY_VALUES: + return u'' value = re.sub('\s', u'', value).upper() try: check_digit = ssn_check_digit(value) @@ -71,8 +71,8 @@ class ITVatNumberField(Field): def clean(self, value): value = super(ITVatNumberField, self).clean(value) - if value == u'': - return value + if value in EMPTY_VALUES: + return u'' try: vat_number = int(value) except ValueError: diff --git a/tests/regressiontests/forms/localflavor/it.py b/tests/regressiontests/forms/localflavor/it.py index 10023a7cf6..7181e25e84 100644 --- a/tests/regressiontests/forms/localflavor/it.py +++ b/tests/regressiontests/forms/localflavor/it.py @@ -1,62 +1,70 @@ -# -*- coding: utf-8 -*- -# Tests for the contrib/localflavor/ IT form fields. +from django.contrib.localflavor.it.forms import (ITZipCodeField, ITRegionSelect, + ITSocialSecurityNumberField, ITVatNumberField) -tests = r""" -# ITZipCodeField ############################################################# +from utils import LocalFlavorTestCase ->>> from django.contrib.localflavor.it.forms import ITZipCodeField ->>> f = ITZipCodeField() ->>> f.clean('00100') -u'00100' ->>> f.clean(' 00100') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid zip code.'] -# ITRegionSelect ############################################################# +class ITLocalFlavorTests(LocalFlavorTestCase): + def test_ITRegionSelect(self): + f = ITRegionSelect() + out = u'''''' + self.assertEqual(f.render('regions', 'PMN'), out) ->>> from django.contrib.localflavor.it.forms import ITRegionSelect ->>> w = ITRegionSelect() ->>> w.render('regions', 'PMN') -u'' + def test_ITZipCodeField(self): + error_invalid = [u'Enter a valid zip code.'] + valid = { + '00100': '00100', + } + invalid = { + ' 00100': error_invalid, + } + self.assertFieldOutput(ITZipCodeField, valid, invalid) + + def test_ITSocialSecurityNumberField(self): + error_invalid = [u'Enter a valid Social Security number.'] + valid = { + 'LVSGDU99T71H501L': 'LVSGDU99T71H501L', + 'LBRRME11A01L736W': 'LBRRME11A01L736W', + 'lbrrme11a01l736w': 'LBRRME11A01L736W', + 'LBR RME 11A01 L736W': 'LBRRME11A01L736W', + } + invalid = { + 'LBRRME11A01L736A': error_invalid, + '%BRRME11A01L736W': error_invalid, + } + self.assertFieldOutput(ITSocialSecurityNumberField, valid, invalid) + + def test_ITVatNumberField(self): + error_invalid = [u'Enter a valid VAT number.'] + valid = { + '07973780013': '07973780013', + '7973780013': '07973780013', + 7973780013: '07973780013', + } + invalid = { + '07973780014': error_invalid, + 'A7973780013': error_invalid, + } + self.assertFieldOutput(ITVatNumberField, valid, invalid) -# ITSocialSecurityNumberField ################################################# - ->>> from django.contrib.localflavor.it.forms import ITSocialSecurityNumberField ->>> f = ITSocialSecurityNumberField() ->>> f.clean('LVSGDU99T71H501L') -u'LVSGDU99T71H501L' ->>> f.clean('LBRRME11A01L736W') -u'LBRRME11A01L736W' ->>> f.clean('lbrrme11a01l736w') -u'LBRRME11A01L736W' ->>> f.clean('LBR RME 11A01 L736W') -u'LBRRME11A01L736W' ->>> f.clean('LBRRME11A01L736A') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid Social Security number.'] ->>> f.clean('%BRRME11A01L736W') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid Social Security number.'] - -# ITVatNumberField ########################################################### - ->>> from django.contrib.localflavor.it.forms import ITVatNumberField ->>> f = ITVatNumberField() ->>> f.clean('07973780013') -u'07973780013' ->>> f.clean('7973780013') -u'07973780013' ->>> f.clean(7973780013) -u'07973780013' ->>> f.clean('07973780014') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid VAT number.'] ->>> f.clean('A7973780013') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid VAT number.'] -""" diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 2f5216f4d9..e7d5637392 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- from localflavor.cz import tests as localflavor_cz_tests -from localflavor.it import tests as localflavor_it_tests from localflavor.jp import tests as localflavor_jp_tests from localflavor.kw import tests as localflavor_kw_tests from localflavor.nl import tests as localflavor_nl_tests @@ -31,12 +30,12 @@ from localflavor.id import IDLocalFlavorTests from localflavor.ie import IELocalFlavorTests from localflavor.il import ILLocalFlavorTests from localflavor.is_ import ISLocalFlavorTests +from localflavor.it import ITLocalFlavorTests from localflavor.tr import TRLocalFlavorTests __test__ = { 'localflavor_cz_tests': localflavor_cz_tests, - 'localflavor_it_tests': localflavor_it_tests, 'localflavor_jp_tests': localflavor_jp_tests, 'localflavor_kw_tests': localflavor_kw_tests, 'localflavor_nl_tests': localflavor_nl_tests, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 7c862ad294..345ed855f5 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -30,5 +30,6 @@ from regressiontests.forms.localflavortests import ( IELocalFlavorTests, ILLocalFlavorTests, ISLocalFlavorTests, + ITLocalFlavorTests, TRLocalFlavorTests, )