[1.2.X] Converted French localflavor doctests into unittests. We have always been at war with doctests. Thanks to Idan Gazit. Backport of [14939].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 22:13:20 +00:00
parent aaaaa831fe
commit 4aedbcb57e
3 changed files with 36 additions and 113 deletions

View File

@ -1,117 +1,40 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ FR form fields.
from django.contrib.localflavor.fr.forms import (FRZipCodeField,
FRPhoneNumberField, FRDepartmentSelect)
tests = r"""
# FRZipCodeField #############################################################
FRZipCodeField validates that the data is a valid FR zipcode.
>>> from django.contrib.localflavor.fr.forms import FRZipCodeField
>>> f = FRZipCodeField()
>>> f.clean('75001')
u'75001'
>>> f.clean('93200')
u'93200'
>>> f.clean('2A200')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXXX.']
>>> f.clean('980001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXXX.']
>>> 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.']
>>> f = FRZipCodeField(required=False)
>>> f.clean('75001')
u'75001'
>>> f.clean('93200')
u'93200'
>>> f.clean('2A200')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXXX.']
>>> f.clean('980001')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXXX.']
>>> f.clean(None)
u''
>>> f.clean('')
u''
from utils import LocalFlavorTestCase
# FRPhoneNumberField ##########################################################
class FRLocalFlavorTests(LocalFlavorTestCase):
def test_FRZipCodeField(self):
error_format = [u'Enter a zip code in the format XXXXX.']
valid = {
'75001': '75001',
'93200': '93200',
}
invalid = {
'2A200': error_format,
'980001': error_format,
}
self.assertFieldOutput(FRZipCodeField, valid, invalid)
FRPhoneNumberField validates that the data is a valid french phone number.
It's normalized to 0X XX XX XX XX format. Dots are valid too.
>>> from django.contrib.localflavor.fr.forms import FRPhoneNumberField
>>> f = FRPhoneNumberField()
>>> f.clean('01 55 44 58 64')
u'01 55 44 58 64'
>>> f.clean('0155445864')
u'01 55 44 58 64'
>>> f.clean('01 5544 5864')
u'01 55 44 58 64'
>>> f.clean('01 55.44.58.64')
u'01 55 44 58 64'
>>> f.clean('01.55.44.58.64')
u'01 55 44 58 64'
>>> f.clean('01,55,44,58,64')
Traceback (most recent call last):
...
ValidationError: [u'Phone numbers must be in 0X XX XX XX XX format.']
>>> f.clean('555 015 544')
Traceback (most recent call last):
...
ValidationError: [u'Phone numbers must be in 0X XX XX XX XX format.']
>>> 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.']
def test_FRPhoneNumberField(self):
error_format = [u'Phone numbers must be in 0X XX XX XX XX format.']
valid = {
'01 55 44 58 64': '01 55 44 58 64',
'0155445864': '01 55 44 58 64',
'01 5544 5864': '01 55 44 58 64',
'01 55.44.58.64': '01 55 44 58 64',
'01.55.44.58.64': '01 55 44 58 64',
}
invalid = {
'01,55,44,58,64': error_format,
'555 015 544': error_format,
}
self.assertFieldOutput(FRPhoneNumberField, valid, invalid)
>>> f = FRPhoneNumberField(required=False)
>>> f.clean('01 55 44 58 64')
u'01 55 44 58 64'
>>> f.clean('0155445864')
u'01 55 44 58 64'
>>> f.clean('01 5544 5864')
u'01 55 44 58 64'
>>> f.clean('01 55.44.58.64')
u'01 55 44 58 64'
>>> f.clean('01.55.44.58.64')
u'01 55 44 58 64'
>>> f.clean('01,55,44,58,64')
Traceback (most recent call last):
...
ValidationError: [u'Phone numbers must be in 0X XX XX XX XX format.']
>>> f.clean('555 015 544')
Traceback (most recent call last):
...
ValidationError: [u'Phone numbers must be in 0X XX XX XX XX format.']
>>> f.clean(None)
u''
>>> f.clean('')
u''
# FRDepartmentSelect ###############################################################
FRDepartmentSelect is a Select widget that uses a list of french departments
including DOM TOM
>>> from django.contrib.localflavor.fr.forms import FRDepartmentSelect
>>> w = FRDepartmentSelect()
>>> print w.render('dep', 'Paris')
<select name="dep">
def test_FRDepartmentSelect(self):
f = FRDepartmentSelect()
out = u'''<select name="dep">
<option value="01">01 - Ain</option>
<option value="02">02 - Aisne</option>
<option value="03">03 - Allier</option>
@ -218,5 +141,5 @@ including DOM TOM
<option value="986">986 - Wallis et Futuna</option>
<option value="987">987 - Polynesie Francaise</option>
<option value="988">988 - Nouvelle-Caledonie</option>
</select>
"""
</select>'''
self.assertEqual(f.render('dep', 'Paris'), out)

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests
from localflavor.fr import tests as localflavor_fr_tests
from localflavor.generic import tests as localflavor_generic_tests
from localflavor.id import tests as localflavor_id_tests
from localflavor.ie import tests as localflavor_ie_tests
@ -30,11 +29,11 @@ from localflavor.cl import CLLocalFlavorTests
from localflavor.de import DELocalFlavorTests
from localflavor.es import ESLocalFlavorTests
from localflavor.fi import FILocalFlavorTests
from localflavor.fr import FRLocalFlavorTests
__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_fr_tests': localflavor_fr_tests,
'localflavor_generic_tests': localflavor_generic_tests,
'localflavor_id_tests': localflavor_id_tests,
'localflavor_ie_tests': localflavor_ie_tests,

View File

@ -23,4 +23,5 @@ from regressiontests.forms.localflavortests import (
DELocalFlavorTests,
ESLocalFlavorTests,
FILocalFlavorTests,
FRLocalFlavorTests,
)