From d01cb6ed17647ed8af8b5c3d083fbd3ca21039df Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:32:54 +0000 Subject: [PATCH] Fixed #14864, #14864 -- ROCIFField didn't accept values starting with RO, as it was supposed to, and ROCNPField, ROIBANField, and ROPhoneNumberField didn't handle all EMPTY_VALUES correctly. Also converted Romanian localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit. Fixing RO localflavor clean() #14864 git-svn-id: http://code.djangoproject.com/svn/django/trunk@14951 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/ro/forms.py | 8 +- tests/regressiontests/forms/localflavor/ro.py | 293 ++++++++---------- .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 4 files changed, 138 insertions(+), 167 deletions(-) diff --git a/django/contrib/localflavor/ro/forms.py b/django/contrib/localflavor/ro/forms.py index dd86fce9f2..a218bfd167 100644 --- a/django/contrib/localflavor/ro/forms.py +++ b/django/contrib/localflavor/ro/forms.py @@ -20,7 +20,7 @@ class ROCIFField(RegexField): } def __init__(self, *args, **kwargs): - super(ROCIFField, self).__init__(r'^[0-9]{2,10}', max_length=10, + super(ROCIFField, self).__init__(r'^(RO)?[0-9]{2,10}', max_length=10, min_length=2, *args, **kwargs) def clean(self, value): @@ -65,6 +65,8 @@ class ROCNPField(RegexField): CNP validations """ value = super(ROCNPField, self).clean(value) + if value in EMPTY_VALUES: + return u'' # check birthdate digits import datetime try: @@ -150,6 +152,8 @@ class ROIBANField(RegexField): Strips - and spaces, performs country code and checksum validation """ value = super(ROIBANField, self).clean(value) + if value in EMPTY_VALUES: + return u'' value = value.replace('-','') value = value.replace(' ','') value = value.upper() @@ -180,6 +184,8 @@ class ROPhoneNumberField(RegexField): Strips -, (, ) and spaces. Checks the final length. """ value = super(ROPhoneNumberField, self).clean(value) + if value in EMPTY_VALUES: + return u'' value = value.replace('-','') value = value.replace('(','') value = value.replace(')','') diff --git a/tests/regressiontests/forms/localflavor/ro.py b/tests/regressiontests/forms/localflavor/ro.py index e885030029..5b546cd68b 100644 --- a/tests/regressiontests/forms/localflavor/ro.py +++ b/tests/regressiontests/forms/localflavor/ro.py @@ -1,175 +1,140 @@ # -*- coding: utf-8 -*- -# Tests for the contrib/localflavor/ RO form fields. +from django.contrib.localflavor.ro.forms import (ROCIFField, ROCNPField, + ROCountyField, ROCountySelect, ROIBANField, ROPhoneNumberField, + ROPostalCodeField) -tests = r""" ->>> from django.contrib.localflavor.ro.forms import * +from utils import LocalFlavorTestCase -##ROCIFField ################################################################ -f = ROCIFField() -f.clean('21694681') -u'21694681' -f.clean('RO21694681') -u'21694681' -f.clean('21694680') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid CIF'] -f.clean('21694680000') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 10 characters (it has 11).'] -f.clean('0') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 2 characters (it has 1).'] -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.'] +class ROLocalFlavorTests(LocalFlavorTestCase): + def test_ROCountySelect(self): + f = ROCountySelect() + out = u'''''' + self.assertEqual(f.render('county', 'CJ'), out) -##ROCNPField ################################################################# + def test_ROCIFField(self): + error_invalid = [u'Enter a valid CIF.'] + error_atmost = [u'Ensure this value has at most 10 characters (it has 11).'] + error_atleast = [u'Ensure this value has at least 2 characters (it has 1).'] + valid = { + '21694681': u'21694681', + 'RO21694681': u'21694681', + } + invalid = { + '21694680': error_invalid, + '21694680000': error_atmost, + '0': error_atleast, + } + self.assertFieldOutput(ROCIFField, valid, invalid) -f = ROCNPField() -f.clean('1981211204489') -u'1981211204489' -f.clean('1981211204487') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid CNP'] -f.clean('1981232204489') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid CNP'] -f.clean('9981211204489') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid CNP'] -f.clean('9981211209') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 13 characters (it has 10).'] -f.clean('19812112044891') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 13 characters (it has 14).'] -f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] + def test_ROCNPField(self): + error_invalid = [u'Enter a valid CNP.'] + error_atleast = [u'Ensure this value has at least 13 characters (it has 10).'] + error_atmost = [u'Ensure this value has at most 13 characters (it has 14).'] + valid = { + '1981211204489': '1981211204489', + } + invalid = { + '1981211204487': error_invalid, + '1981232204489': error_invalid, + '9981211204489': error_invalid, + '9981211209': error_atleast, + '19812112044891': error_atmost, + } + self.assertFieldOutput(ROCNPField, valid, invalid) -##ROCountyField ############################################################## + def test_ROCountyField(self): + error_format = [u'Enter a Romanian county code or name.'] + valid = { + 'CJ': 'CJ', + 'cj': 'CJ', + u'Argeş': 'AG', + u'argeş': 'AG', + } + invalid = { + 'Arges': error_format, + } + self.assertFieldOutput(ROCountyField, valid, invalid) -f = ROCountyField() -f.clean('CJ') -'CJ' -f.clean('cj') -'CJ' -f.clean('Argeş') -'AG' -f.clean('argeş') -'AG' -f.clean('Arges') -Traceback (most recent call last): -... -ValidationError: [u'Enter a Romanian county code or name.'] -f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] + def test_ROIBANField(self): + error_invalid = [u'Enter a valid IBAN in ROXX-XXXX-XXXX-XXXX-XXXX-XXXX format'] + error_atleast = [u'Ensure this value has at least 24 characters (it has 23).'] + valid = { + 'RO56RZBR0000060003291177': 'RO56RZBR0000060003291177', + 'RO56-RZBR-0000-0600-0329-1177': 'RO56RZBR0000060003291177', + } + invalid = { + 'RO56RZBR0000060003291176': error_invalid, + 'AT61 1904 3002 3457 3201': error_invalid, + 'RO56RZBR000006000329117': error_atleast, + } + self.assertFieldOutput(ROIBANField, valid, invalid) -##ROCountySelect ############################################################# + def test_ROPhoneNumberField(self): + error_format = [u'Phone numbers must be in XXXX-XXXXXX format.'] + error_atleast = [u'Ensure this value has at least 10 characters (it has 9).'] + valid = { + '0264485936': '0264485936', + '(0264)-485936': '0264485936', + } + invalid = { + '02644859368': error_format, + '026448593': error_atleast, + } + self.assertFieldOutput(ROPhoneNumberField, valid, invalid) -f = ROCountySelect() -f.render('county','CJ') -u'' + def test_ROPostalCodeField(self): + error_atleast = [u'Ensure this value has at least 6 characters (it has 5).'] + error_atmost = [u'Ensure this value has at most 6 characters (it has 7).'] -##ROIBANField ################################################################# - -f = ROIBANField() -f.clean('RO56RZBR0000060003291177') -u'RO56RZBR0000060003291177' -f.clean('RO56RZBR0000060003291176') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid IBAN in ROXX-XXXX-XXXX-XXXX-XXXX-XXXX format'] - -f.clean('RO56-RZBR-0000-0600-0329-1177') -u'RO56RZBR0000060003291177' -f.clean('AT61 1904 3002 3457 3201') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid IBAN in ROXX-XXXX-XXXX-XXXX-XXXX-XXXX format'] - -f.clean('RO56RZBR000006000329117') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 24 characters (it has 23).'] -f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - -##ROPhoneNumberField ########################################################## - -f = ROPhoneNumberField() -f.clean('0264485936') -u'0264485936' -f.clean('(0264)-485936') -u'0264485936' -f.clean('02644859368') -Traceback (most recent call last): -... -ValidationError: [u'Phone numbers must be in XXXX-XXXXXX format.'] -f.clean('026448593') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 10 characters (it has 9).'] -f.clean(None) -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] - -##ROPostalCodeField ########################################################### - -f = ROPostalCodeField() -f.clean('400473') -u'400473' -f.clean('40047') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at least 6 characters (it has 5).'] -f.clean('4004731') -Traceback (most recent call last): -... -ValidationError: [u'Ensure this value has at most 6 characters (it has 7).'] -f.clean('') -Traceback (most recent call last): -... -ValidationError: [u'This field is required.'] -""" + valid = { + '400473': '400473', + } + invalid = { + '40047': error_atleast, + '4004731': error_atmost, + } + self.assertFieldOutput(ROPostalCodeField, valid, invalid) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index c6a050edc8..9f2295fff0 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.ro import tests as localflavor_ro_tests from localflavor.se import tests as localflavor_se_tests from localflavor.sk import tests as localflavor_sk_tests from localflavor.uk import tests as localflavor_uk_tests @@ -31,12 +30,12 @@ from localflavor.kw import KWLocalFlavorTests from localflavor.nl import NLLocalFlavorTests from localflavor.pl import PLLocalFlavorTests from localflavor.pt import PTLocalFlavorTests +from localflavor.ro import ROLocalFlavorTests from localflavor.tr import TRLocalFlavorTests __test__ = { 'localflavor_cz_tests': localflavor_cz_tests, - 'localflavor_ro_tests': localflavor_ro_tests, 'localflavor_se_tests': localflavor_se_tests, 'localflavor_sk_tests': localflavor_sk_tests, 'localflavor_uk_tests': localflavor_uk_tests, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 124646cbb1..6019d85e2b 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -36,5 +36,6 @@ from regressiontests.forms.localflavortests import ( NLLocalFlavorTests, PLLocalFlavorTests, PTLocalFlavorTests, + ROLocalFlavorTests, TRLocalFlavorTests, )