1
0
mirror of https://github.com/django/django.git synced 2024-12-24 01:55:49 +00:00

Converted Spanish localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 20:30:16 +00:00
parent cc8c3ebb7d
commit b148aead7d
3 changed files with 166 additions and 341 deletions

View File

@ -1,347 +1,172 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ ES form fields.
from django.contrib.localflavor.es.forms import (ESPostalCodeField, ESPhoneNumberField,
ESIdentityCardNumberField, ESCCCField, ESRegionSelect, ESProvinceSelect)
tests = r"""
# ESPostalCodeField ##############################################################
from utils import LocalFlavorTestCase
ESPostalCodeField validates that data is a five-digit spanish postal code.
>>> from django.contrib.localflavor.es.forms import ESPostalCodeField
>>> f = ESPostalCodeField()
>>> f.clean('08028')
u'08028'
>>> f.clean('28080')
u'28080'
>>> f.clean('53001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('0801')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('080001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('00999')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('08 01')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('08A01')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = ESPostalCodeField(required=False)
>>> f.clean('08028')
u'08028'
>>> f.clean('28080')
u'28080'
>>> f.clean('53001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('0801')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('080001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('00999')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('08 01')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('08A01')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
>>> f.clean('')
u''
class ESLocalFlavorTests(LocalFlavorTestCase):
def test_ESRegionSelect(self):
f = ESRegionSelect()
out = u'''<select name="regions">
<option value="AN">Andalusia</option>
<option value="AR">Aragon</option>
<option value="O">Principality of Asturias</option>
<option value="IB">Balearic Islands</option>
<option value="PV">Basque Country</option>
<option value="CN">Canary Islands</option>
<option value="S">Cantabria</option>
<option value="CM">Castile-La Mancha</option>
<option value="CL">Castile and Leon</option>
<option value="CT" selected="selected">Catalonia</option>
<option value="EX">Extremadura</option>
<option value="GA">Galicia</option>
<option value="LO">La Rioja</option>
<option value="M">Madrid</option>
<option value="MU">Region of Murcia</option>
<option value="NA">Foral Community of Navarre</option>
<option value="VC">Valencian Community</option>
</select>'''
self.assertEqual(f.render('regions', 'CT'), out)
# ESPhoneNumberField ##############################################################
def test_ESProvinceSelect(self):
f = ESProvinceSelect()
out = u'''<select name="provinces">
<option value="01">Arava</option>
<option value="02">Albacete</option>
<option value="03">Alacant</option>
<option value="04">Almeria</option>
<option value="05">Avila</option>
<option value="06">Badajoz</option>
<option value="07">Illes Balears</option>
<option value="08" selected="selected">Barcelona</option>
<option value="09">Burgos</option>
<option value="10">Caceres</option>
<option value="11">Cadiz</option>
<option value="12">Castello</option>
<option value="13">Ciudad Real</option>
<option value="14">Cordoba</option>
<option value="15">A Coruna</option>
<option value="16">Cuenca</option>
<option value="17">Girona</option>
<option value="18">Granada</option>
<option value="19">Guadalajara</option>
<option value="20">Guipuzkoa</option>
<option value="21">Huelva</option>
<option value="22">Huesca</option>
<option value="23">Jaen</option>
<option value="24">Leon</option>
<option value="25">Lleida</option>
<option value="26">La Rioja</option>
<option value="27">Lugo</option>
<option value="28">Madrid</option>
<option value="29">Malaga</option>
<option value="30">Murcia</option>
<option value="31">Navarre</option>
<option value="32">Ourense</option>
<option value="33">Asturias</option>
<option value="34">Palencia</option>
<option value="35">Las Palmas</option>
<option value="36">Pontevedra</option>
<option value="37">Salamanca</option>
<option value="38">Santa Cruz de Tenerife</option>
<option value="39">Cantabria</option>
<option value="40">Segovia</option>
<option value="41">Seville</option>
<option value="42">Soria</option>
<option value="43">Tarragona</option>
<option value="44">Teruel</option>
<option value="45">Toledo</option>
<option value="46">Valencia</option>
<option value="47">Valladolid</option>
<option value="48">Bizkaia</option>
<option value="49">Zamora</option>
<option value="50">Zaragoza</option>
<option value="51">Ceuta</option>
<option value="52">Melilla</option>
</select>'''
self.assertEqual(f.render('provinces', '08'), out)
ESPhoneNumberField validates that data is a nine-digit spanish phone number.
>>> from django.contrib.localflavor.es.forms import ESPhoneNumberField
>>> f = ESPhoneNumberField()
>>> f.clean('650010101')
u'650010101'
>>> f.clean('931234567')
u'931234567'
>>> f.clean('800123123')
u'800123123'
>>> f.clean('555555555')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('789789789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('99123123')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('9999123123')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
def test_ESPostalCodeField(self):
error_invalid = [u'Enter a valid postal code in the range and format 01XXX - 52XXX.']
valid = {
'08028': '08028',
'28080': '28080',
}
invalid = {
'53001': error_invalid,
'0801': error_invalid,
'080001': error_invalid,
'00999': error_invalid,
'08 01': error_invalid,
'08A01': error_invalid,
}
self.assertFieldOutput(ESPostalCodeField, valid, invalid)
>>> f = ESPhoneNumberField(required=False)
>>> f.clean('650010101')
u'650010101'
>>> f.clean('931234567')
u'931234567'
>>> f.clean('800123123')
u'800123123'
>>> f.clean('555555555')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('789789789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('99123123')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('9999123123')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
>>> f.clean('')
u''
def test_ESPhoneNumberField(self):
error_invalid = [u'Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.']
valid = {
'650010101': '650010101',
'931234567': '931234567',
'800123123': '800123123',
}
invalid = {
'555555555': error_invalid,
'789789789': error_invalid,
'99123123': error_invalid,
'9999123123': error_invalid,
}
self.assertFieldOutput(ESPhoneNumberField, valid, invalid)
# ESIdentityCardNumberField ##############################################################
def test_ESIdentityCardNumberField(self):
error_invalid = [u'Please enter a valid NIF, NIE, or CIF.']
error_checksum_nif = [u'Invalid checksum for NIF.']
error_checksum_nie = [u'Invalid checksum for NIE.']
error_checksum_cif = [u'Invalid checksum for CIF.']
valid = {
'78699688J': '78699688J',
'78699688-J': '78699688J',
'78699688 J': '78699688J',
'78699688 j': '78699688J',
'X0901797J': 'X0901797J',
'X-6124387-Q': 'X6124387Q',
'X 0012953 G': 'X0012953G',
'x-3287690-r': 'X3287690R',
't-03287690r': 'T03287690R',
'P2907500I': 'P2907500I',
'B38790911': 'B38790911',
'B31234560': 'B31234560',
'B-3879091A': 'B3879091A',
'B 38790911': 'B38790911',
'P-3900800-H': 'P3900800H',
'P 39008008': 'P39008008',
'C-28795565': 'C28795565',
'C 2879556E': 'C2879556E',
}
invalid = {
'78699688T': error_checksum_nif,
'X-03287690': error_invalid,
'X-03287690-T': error_checksum_nie,
'B 38790917': error_checksum_cif,
'C28795567': error_checksum_cif,
'I38790911': error_invalid,
'78699688-2': error_invalid,
}
self.assertFieldOutput(ESIdentityCardNumberField, valid, invalid)
def test_ESCCCField(self):
error_invalid = [u'Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
error_checksum = [u'Invalid checksum for bank account number.']
valid = {
'20770338793100254321': '20770338793100254321',
'2077 0338 79 3100254321': '2077 0338 79 3100254321',
'2077-0338-79-3100254321': '2077-0338-79-3100254321',
}
invalid = {
'2077.0338.79.3100254321': error_invalid,
'2077-0338-78-3100254321': error_checksum,
'2077-0338-89-3100254321': error_checksum,
'2077-03-3879-3100254321': error_invalid,
}
self.assertFieldOutput(ESCCCField, valid, invalid)
ESIdentityCardNumberField validates that data is a identification spanish code for companies or individuals (CIF, NIF or NIE).
>>> from django.contrib.localflavor.es.forms import ESIdentityCardNumberField
>>> f = ESIdentityCardNumberField()
>>> f.clean('78699688J')
'78699688J'
>>> f.clean('78699688-J')
'78699688J'
>>> f.clean('78699688 J')
'78699688J'
>>> f.clean('78699688 j')
'78699688J'
>>> f.clean('78699688T')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for NIF.']
>>> f.clean('X0901797J')
'X0901797J'
>>> f.clean('X-6124387-Q')
'X6124387Q'
>>> f.clean('X 0012953 G')
'X0012953G'
>>> f.clean('x-3287690-r')
'X3287690R'
>>> f.clean('t-03287690r')
'T03287690R'
>>> f.clean('P2907500I')
'P2907500I'
>>> f.clean('X-03287690')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('X-03287690-T')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for NIE.']
>>> f.clean('B38790911')
'B38790911'
>>> f.clean('B31234560')
'B31234560'
>>> f.clean('B-3879091A')
'B3879091A'
>>> f.clean('B 38790917')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for CIF.']
>>> f.clean('B 38790911')
'B38790911'
>>> f.clean('P-3900800-H')
'P3900800H'
>>> f.clean('P 39008008')
'P39008008'
>>> f.clean('C-28795565')
'C28795565'
>>> f.clean('C 2879556E')
'C2879556E'
>>> f.clean('C28795567')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for CIF.']
>>> f.clean('I38790911')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('78699688-2')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = ESIdentityCardNumberField(required=False)
>>> f.clean('78699688J')
'78699688J'
>>> f.clean('78699688-J')
'78699688J'
>>> f.clean('78699688 J')
'78699688J'
>>> f.clean('78699688 j')
'78699688J'
>>> f.clean('78699688T')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for NIF.']
>>> f.clean('X0901797J')
'X0901797J'
>>> f.clean('X-6124387-Q')
'X6124387Q'
>>> f.clean('X 0012953 G')
'X0012953G'
>>> f.clean('x-3287690-r')
'X3287690R'
>>> f.clean('t-03287690r')
'T03287690R'
>>> f.clean('X-03287690')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('X-03287690-T')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for NIE.']
>>> f.clean('B38790911')
'B38790911'
>>> f.clean('B-3879091A')
'B3879091A'
>>> f.clean('B 38790917')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for CIF.']
>>> f.clean('B 38790911')
'B38790911'
>>> f.clean('P-3900800-H')
'P3900800H'
>>> f.clean('P 39008008')
'P39008008'
>>> f.clean('C-28795565')
'C28795565'
>>> f.clean('C 2879556E')
'C2879556E'
>>> f.clean('C28795567')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for CIF.']
>>> f.clean('I38790911')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('78699688-2')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid NIF, NIE, or CIF.']
>>> f.clean('')
u''
# ESCCCField ##############################################################
ESCCCField validates that data is a spanish bank account number (codigo cuenta cliente).
>>> from django.contrib.localflavor.es.forms import ESCCCField
>>> f = ESCCCField()
>>> f.clean('20770338793100254321')
'20770338793100254321'
>>> f.clean('2077 0338 79 3100254321')
'2077 0338 79 3100254321'
>>> f.clean('2077-0338-79-3100254321')
'2077-0338-79-3100254321'
>>> f.clean('2077.0338.79.3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
>>> f.clean('2077-0338-78-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for bank account number.']
>>> f.clean('2077-0338-89-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for bank account number.']
>>> f.clean('2077-03-3879-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = ESCCCField(required=False)
>>> f.clean('20770338793100254321')
'20770338793100254321'
>>> f.clean('2077 0338 79 3100254321')
'2077 0338 79 3100254321'
>>> f.clean('2077-0338-79-3100254321')
'2077-0338-79-3100254321'
>>> f.clean('2077.0338.79.3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
>>> f.clean('2077-0338-78-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for bank account number.']
>>> f.clean('2077-0338-89-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Invalid checksum for bank account number.']
>>> f.clean('2077-03-3879-3100254321')
Traceback (most recent call last):
...
ValidationError: [u'Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
>>> f.clean('')
u''
# ESRegionSelect ##############################################################
ESRegionSelect is a Select widget that uses a list of Spain regions as its choices.
>>> from django.contrib.localflavor.es.forms import ESRegionSelect
>>> w = ESRegionSelect()
>>> w.render('regions', 'CT')
u'<select name="regions">\n<option value="AN">Andalusia</option>\n<option value="AR">Aragon</option>\n<option value="O">Principality of Asturias</option>\n<option value="IB">Balearic Islands</option>\n<option value="PV">Basque Country</option>\n<option value="CN">Canary Islands</option>\n<option value="S">Cantabria</option>\n<option value="CM">Castile-La Mancha</option>\n<option value="CL">Castile and Leon</option>\n<option value="CT" selected="selected">Catalonia</option>\n<option value="EX">Extremadura</option>\n<option value="GA">Galicia</option>\n<option value="LO">La Rioja</option>\n<option value="M">Madrid</option>\n<option value="MU">Region of Murcia</option>\n<option value="NA">Foral Community of Navarre</option>\n<option value="VC">Valencian Community</option>\n</select>'
# ESProvincenSelect ##############################################################
ESProvinceSelect is a Select widget that uses a list of Spain provinces as its choices.
>>> from django.contrib.localflavor.es.forms import ESProvinceSelect
>>> w = ESProvinceSelect()
>>> w.render('provinces', '08')
u'<select name="provinces">\n<option value="01">Arava</option>\n<option value="02">Albacete</option>\n<option value="03">Alacant</option>\n<option value="04">Almeria</option>\n<option value="05">Avila</option>\n<option value="06">Badajoz</option>\n<option value="07">Illes Balears</option>\n<option value="08" selected="selected">Barcelona</option>\n<option value="09">Burgos</option>\n<option value="10">Caceres</option>\n<option value="11">Cadiz</option>\n<option value="12">Castello</option>\n<option value="13">Ciudad Real</option>\n<option value="14">Cordoba</option>\n<option value="15">A Coruna</option>\n<option value="16">Cuenca</option>\n<option value="17">Girona</option>\n<option value="18">Granada</option>\n<option value="19">Guadalajara</option>\n<option value="20">Guipuzkoa</option>\n<option value="21">Huelva</option>\n<option value="22">Huesca</option>\n<option value="23">Jaen</option>\n<option value="24">Leon</option>\n<option value="25">Lleida</option>\n<option value="26">La Rioja</option>\n<option value="27">Lugo</option>\n<option value="28">Madrid</option>\n<option value="29">Malaga</option>\n<option value="30">Murcia</option>\n<option value="31">Navarre</option>\n<option value="32">Ourense</option>\n<option value="33">Asturias</option>\n<option value="34">Palencia</option>\n<option value="35">Las Palmas</option>\n<option value="36">Pontevedra</option>\n<option value="37">Salamanca</option>\n<option value="38">Santa Cruz de Tenerife</option>\n<option value="39">Cantabria</option>\n<option value="40">Segovia</option>\n<option value="41">Seville</option>\n<option value="42">Soria</option>\n<option value="43">Tarragona</option>\n<option value="44">Teruel</option>\n<option value="45">Toledo</option>\n<option value="46">Valencia</option>\n<option value="47">Valladolid</option>\n<option value="48">Bizkaia</option>\n<option value="49">Zamora</option>\n<option value="50">Zaragoza</option>\n<option value="51">Ceuta</option>\n<option value="52">Melilla</option>\n</select>'
"""

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests
from localflavor.es import tests as localflavor_es_tests
from localflavor.fi import tests as localflavor_fi_tests
from localflavor.fr import tests as localflavor_fr_tests
from localflavor.generic import tests as localflavor_generic_tests
@ -23,20 +22,20 @@ 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.au import AULocalFlavorTests
from localflavor.be import BELocalFlavorTests
from localflavor.br import BRLocalFlavorTests
from localflavor.ca import CALocalFlavorTests
from localflavor.ch import CHLocalFlavorTests
from localflavor.cl import CLLocalFlavorTests
from localflavor.de import DELocalFlavorTests
from localflavor.es import ESLocalFlavorTests
from localflavor.il import ILLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests
__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_es_tests': localflavor_es_tests,
'localflavor_fi_tests': localflavor_fi_tests,
'localflavor_fr_tests': localflavor_fr_tests,
'localflavor_generic_tests': localflavor_generic_tests,

View File

@ -22,6 +22,7 @@ from regressiontests.forms.localflavortests import (
CHLocalFlavorTests,
CLLocalFlavorTests,
DELocalFlavorTests,
ESLocalFlavorTests,
ILLocalFlavorTests,
TRLocalFlavorTests,
)