From f9e6ca10265ed59459932fa6eb73a7527dbd5c7c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:32:31 +0000 Subject: [PATCH] Fixed #14860 -- PLPESELField, PLNIPField, and PLREGONField didn't handle all EMPTY_VALUES correctly. Also converted teh Polish 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@14949 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/pl/forms.py | 7 + tests/regressiontests/forms/localflavor/pl.py | 545 +++++++++++++++--- .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 4 files changed, 467 insertions(+), 89 deletions(-) diff --git a/django/contrib/localflavor/pl/forms.py b/django/contrib/localflavor/pl/forms.py index b908580568..d1e9773a80 100644 --- a/django/contrib/localflavor/pl/forms.py +++ b/django/contrib/localflavor/pl/forms.py @@ -7,6 +7,7 @@ import re from django.forms import ValidationError from django.forms.fields import Select, RegexField from django.utils.translation import ugettext_lazy as _ +from django.core.validators import EMPTY_VALUES class PLProvinceSelect(Select): """ @@ -45,6 +46,8 @@ class PLPESELField(RegexField): def clean(self,value): super(PLPESELField, self).clean(value) + if value in EMPTY_VALUES: + return u'' if not self.has_valid_checksum(value): raise ValidationError(self.error_messages['checksum']) return u'%s' % value @@ -78,6 +81,8 @@ class PLNIPField(RegexField): def clean(self,value): super(PLNIPField, self).clean(value) + if value in EMPTY_VALUES: + return u'' value = re.sub("[-]", "", value) if not self.has_valid_checksum(value): raise ValidationError(self.error_messages['checksum']) @@ -116,6 +121,8 @@ class PLREGONField(RegexField): def clean(self,value): super(PLREGONField, self).clean(value) + if value in EMPTY_VALUES: + return u'' if not self.has_valid_checksum(value): raise ValidationError(self.error_messages['checksum']) return u'%s' % value diff --git a/tests/regressiontests/forms/localflavor/pl.py b/tests/regressiontests/forms/localflavor/pl.py index 52f1305ca8..51721f83f8 100644 --- a/tests/regressiontests/forms/localflavor/pl.py +++ b/tests/regressiontests/forms/localflavor/pl.py @@ -1,91 +1,462 @@ -# -*- coding: utf-8 -*- -# Tests for the contrib/localflavor/ PL form fields. +from django.contrib.localflavor.pl.forms import (PLProvinceSelect, + PLCountySelect, PLPostalCodeField, PLNIPField, PLPESELField, PLREGONField) -tests = r""" -# PLProvinceSelect ########################################################## +from utils import LocalFlavorTestCase ->>> from django.contrib.localflavor.pl.forms import PLProvinceSelect ->>> f = PLProvinceSelect() ->>> f.render('voivodeships','pomerania') -u'' -# PLCountySelect ########################################################## +class PLLocalFlavorTests(LocalFlavorTestCase): + def test_PLProvinceSelect(self): + f = PLProvinceSelect() + out = u'''''' + self.assertEqual(f.render('voivodeships', 'pomerania'), out) + + def test_PLCountrySelect(self): + f = PLCountySelect() + out = u'''''' + self.assertEqual(f.render('administrativeunit', 'katowice'), out) + + def test_PLPostalCodeField(self): + error_format = [u'Enter a postal code in the format XX-XXX.'] + valid = { + '41-403': '41-403', + } + invalid = { + '43--434': error_format, + } + self.assertFieldOutput(PLPostalCodeField, valid, invalid) + + def test_PLNIPField(self): + error_format = [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'] + error_checksum = [u'Wrong checksum for the Tax Number (NIP).'] + valid = { + '64-62-414-124': '6462414124', + '646-241-41-24': '6462414124', + } + invalid = { + '43-343-234-323': error_format, + '646-241-41-23': error_checksum, + } + self.assertFieldOutput(PLNIPField, valid, invalid) + + def test_PLPESELField(self): + error_checksum = [u'Wrong checksum for the National Identification Number.'] + error_format = [u'National Identification Number consists of 11 digits.'] + valid = { + '80071610614': '80071610614', + } + invalid = { + '80071610610': error_checksum, + '80': error_format, + '800716106AA': error_format, + } + self.assertFieldOutput(PLPESELField, valid, invalid) + + def test_PLREGONField(self): + error_checksum = [u'Wrong checksum for the National Business Register Number (REGON).'] + error_format = [u'National Business Register Number (REGON) consists of 9 or 14 digits.'] + valid = { + '12345678512347': '12345678512347', + '590096454': '590096454', + } + invalid = { + '123456784': error_checksum, + '12345678412342': error_checksum, + '590096453': error_checksum, + '590096': error_format, + } + self.assertFieldOutput(PLREGONField, valid, invalid) ->>> from django.contrib.localflavor.pl.forms import PLCountySelect ->>> f = PLCountySelect() ->>> f.render('administrativeunit','katowice') -u'' - -# PLPostalCodeField ############################################################## - ->>> from django.contrib.localflavor.pl.forms import PLPostalCodeField ->>> f = PLPostalCodeField() ->>> f.clean('43--434') -Traceback (most recent call last): -... -ValidationError: [u'Enter a postal code in the format XX-XXX.'] ->>> f.clean('41-403') -u'41-403' - -# PLNIPField ############################################################### - ->>> from django.contrib.localflavor.pl.forms import PLNIPField ->>> f = PLNIPField() ->>> f.clean('43-343-234-323') -Traceback (most recent call last): -... -ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'] ->>> f.clean('64-62-414-124') -u'6462414124' ->>> f.clean('646-241-41-24') -u'6462414124' ->>> f.clean('646-241-41-23') -Traceback (most recent call last): -... -ValidationError: [u'Wrong checksum for the Tax Number (NIP).'] - -# PLPESELField ############################################ - ->>> from django.contrib.localflavor.pl.forms import PLPESELField ->>> f = PLPESELField() ->>> f.clean('80071610614') -u'80071610614' ->>> f.clean('80071610610') -Traceback (most recent call last): -... -ValidationError: [u'Wrong checksum for the National Identification Number.'] ->>> f.clean('80') -Traceback (most recent call last): -... -ValidationError: [u'National Identification Number consists of 11 digits.'] ->>> f.clean('800716106AA') -Traceback (most recent call last): -... -ValidationError: [u'National Identification Number consists of 11 digits.'] - -# PLREGONField ################################################ - ->>> from django.contrib.localflavor.pl.forms import PLREGONField ->>> f = PLREGONField() ->>> f.clean('12345678512347') -u'12345678512347' ->>> f.clean('590096454') -u'590096454' ->>> f.clean('123456784') -Traceback (most recent call last): -... -ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] ->>> f.clean('12345678412342') -Traceback (most recent call last): -... -ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] ->>> f.clean('590096453') -Traceback (most recent call last): -... -ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] ->>> f.clean('590096') -Traceback (most recent call last): -... -ValidationError: [u'National Business Register Number (REGON) consists of 9 or 14 digits.'] - -""" diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index ef4136ec60..5c54c43a85 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.pl import tests as localflavor_pl_tests from localflavor.pt import tests as localflavor_pt_tests from localflavor.ro import tests as localflavor_ro_tests from localflavor.se import tests as localflavor_se_tests @@ -31,12 +30,12 @@ from localflavor.it import ITLocalFlavorTests from localflavor.jp import JPLocalFlavorTests from localflavor.kw import KWLocalFlavorTests from localflavor.nl import NLLocalFlavorTests +from localflavor.pl import PLLocalFlavorTests from localflavor.tr import TRLocalFlavorTests __test__ = { 'localflavor_cz_tests': localflavor_cz_tests, - 'localflavor_pl_tests': localflavor_pl_tests, 'localflavor_pt_tests': localflavor_pt_tests, 'localflavor_ro_tests': localflavor_ro_tests, 'localflavor_se_tests': localflavor_se_tests, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 00633ad21c..b458b4a6b0 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -34,5 +34,6 @@ from regressiontests.forms.localflavortests import ( JPLocalFlavorTests, KWLocalFlavorTests, NLLocalFlavorTests, + PLLocalFlavorTests, TRLocalFlavorTests, )