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
This commit is contained in:
Alex Gaynor 2010-12-18 20:32:54 +00:00
parent 8f012072af
commit d01cb6ed17
4 changed files with 138 additions and 167 deletions

View File

@ -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(')','')

View File

@ -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'''<select name="county">
<option value="AB">Alba</option>
<option value="AR">Arad</option>
<option value="AG">Arge\u015f</option>
<option value="BC">Bac\u0103u</option>
<option value="BH">Bihor</option>
<option value="BN">Bistri\u0163a-N\u0103s\u0103ud</option>
<option value="BT">Boto\u015fani</option>
<option value="BV">Bra\u015fov</option>
<option value="BR">Br\u0103ila</option>
<option value="B">Bucure\u015fti</option>
<option value="BZ">Buz\u0103u</option>
<option value="CS">Cara\u015f-Severin</option>
<option value="CL">C\u0103l\u0103ra\u015fi</option>
<option value="CJ" selected="selected">Cluj</option>
<option value="CT">Constan\u0163a</option>
<option value="CV">Covasna</option>
<option value="DB">D\xe2mbovi\u0163a</option>
<option value="DJ">Dolj</option>
<option value="GL">Gala\u0163i</option>
<option value="GR">Giurgiu</option>
<option value="GJ">Gorj</option>
<option value="HR">Harghita</option>
<option value="HD">Hunedoara</option>
<option value="IL">Ialomi\u0163a</option>
<option value="IS">Ia\u015fi</option>
<option value="IF">Ilfov</option>
<option value="MM">Maramure\u015f</option>
<option value="MH">Mehedin\u0163i</option>
<option value="MS">Mure\u015f</option>
<option value="NT">Neam\u0163</option>
<option value="OT">Olt</option>
<option value="PH">Prahova</option>
<option value="SM">Satu Mare</option>
<option value="SJ">S\u0103laj</option>
<option value="SB">Sibiu</option>
<option value="SV">Suceava</option>
<option value="TR">Teleorman</option>
<option value="TM">Timi\u015f</option>
<option value="TL">Tulcea</option>
<option value="VS">Vaslui</option>
<option value="VL">V\xe2lcea</option>
<option value="VN">Vrancea</option>
</select>'''
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'<select name="county">\n<option value="AB">Alba</option>\n<option value="AR">A
rad</option>\n<option value="AG">Arge\u015f</option>\n<option value="BC">Bac\u01
03u</option>\n<option value="BH">Bihor</option>\n<option value="BN">Bistri\u0163
a-N\u0103s\u0103ud</option>\n<option value="BT">Boto\u015fani</option>\n<option
value="BV">Bra\u015fov</option>\n<option value="BR">Br\u0103ila</option>\n<optio
n value="B">Bucure\u015fti</option>\n<option value="BZ">Buz\u0103u</option>\n<op
tion value="CS">Cara\u015f-Severin</option>\n<option value="CL">C\u0103l\u0103ra
\u015fi</option>\n<option value="CJ" selected="selected">Cluj</option>\n<option
value="CT">Constan\u0163a</option>\n<option value="CV">Covasna</option>\n<option
value="DB">D\xe2mbovi\u0163a</option>\n<option value="DJ">Dolj</option>\n<optio
n value="GL">Gala\u0163i</option>\n<option value="GR">Giurgiu</option>\n<option
value="GJ">Gorj</option>\n<option value="HR">Harghita</option>\n<option value="H
D">Hunedoara</option>\n<option value="IL">Ialomi\u0163a</option>\n<option value=
"IS">Ia\u015fi</option>\n<option value="IF">Ilfov</option>\n<option value="MM">M
aramure\u015f</option>\n<option value="MH">Mehedin\u0163i</option>\n<option valu
e="MS">Mure\u015f</option>\n<option value="NT">Neam\u0163</option>\n<option valu
e="OT">Olt</option>\n<option value="PH">Prahova</option>\n<option value="SM">Sat
u Mare</option>\n<option value="SJ">S\u0103laj</option>\n<option value="SB">Sibi
u</option>\n<option value="SV">Suceava</option>\n<option value="TR">Teleorman</o
ption>\n<option value="TM">Timi\u015f</option>\n<option value="TL">Tulcea</optio
n>\n<option value="VS">Vaslui</option>\n<option value="VL">V\xe2lcea</option>\n<
option value="VN">Vrancea</option>\n</select>'
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)

View File

@ -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,

View File

@ -36,5 +36,6 @@ from regressiontests.forms.localflavortests import (
NLLocalFlavorTests,
PLLocalFlavorTests,
PTLocalFlavorTests,
ROLocalFlavorTests,
TRLocalFlavorTests,
)