Converted the Brazillian 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@14933 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
b035759199
commit
0cd72cabab
|
@ -1,220 +1,144 @@
|
||||||
# -*- coding: utf-8 -*-
|
from django.contrib.localflavor.br.forms import (BRZipCodeField,
|
||||||
# Tests for the contrib/localflavor/ BR form fields.
|
BRCNPJField, BRCPFField, BRPhoneNumberField, BRStateSelect,
|
||||||
|
BRStateChoiceField)
|
||||||
|
|
||||||
tests = r"""
|
from utils import LocalFlavorTestCase
|
||||||
# BRZipCodeField ############################################################
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRZipCodeField
|
|
||||||
>>> f = BRZipCodeField()
|
|
||||||
>>> f.clean('12345-123')
|
|
||||||
u'12345-123'
|
|
||||||
>>> f.clean('12345_123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('1234-123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('abcde-abc')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('12345-')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('-123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field is required.']
|
|
||||||
>>> f.clean(None)
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field is required.']
|
|
||||||
|
|
||||||
>>> f = BRZipCodeField(required=False)
|
|
||||||
>>> f.clean(None)
|
|
||||||
u''
|
|
||||||
>>> f.clean('')
|
|
||||||
u''
|
|
||||||
>>> f.clean('-123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('12345-')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('abcde-abc')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('1234-123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('12345_123')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Enter a zip code in the format XXXXX-XXX.']
|
|
||||||
>>> f.clean('12345-123')
|
|
||||||
u'12345-123'
|
|
||||||
|
|
||||||
# BRCNPJField ############################################################
|
class BRLocalFlavorTests(LocalFlavorTestCase):
|
||||||
|
def test_BRZipCodeField(self):
|
||||||
|
error_format = [u'Enter a zip code in the format XXXXX-XXX.']
|
||||||
|
valid = {
|
||||||
|
'12345-123': '12345-123',
|
||||||
|
}
|
||||||
|
invalid = {
|
||||||
|
'12345_123': error_format,
|
||||||
|
'1234-123': error_format,
|
||||||
|
'abcde-abc': error_format,
|
||||||
|
'12345-': error_format,
|
||||||
|
'-123': error_format,
|
||||||
|
}
|
||||||
|
self.assertFieldOutput(BRZipCodeField, valid, invalid)
|
||||||
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRCNPJField
|
def test_BRCNPJField(self):
|
||||||
>>> f = BRCNPJField(required=True)
|
error_format = [u'Invalid CNPJ number.']
|
||||||
>>> f.clean('')
|
error_numbersonly = [u'This field requires only numbers.']
|
||||||
Traceback (most recent call last):
|
valid = {
|
||||||
...
|
'64.132.916/0001-88': '64.132.916/0001-88',
|
||||||
ValidationError: [u'This field is required.']
|
'64-132-916/0001-88': '64-132-916/0001-88',
|
||||||
>>> f.clean('12-345-678/9012-10')
|
'64132916/0001-88': '64132916/0001-88',
|
||||||
Traceback (most recent call last):
|
}
|
||||||
...
|
invalid = {
|
||||||
ValidationError: [u'Invalid CNPJ number.']
|
'12-345-678/9012-10': error_format,
|
||||||
>>> f.clean('12.345.678/9012-10')
|
'12.345.678/9012-10': error_format,
|
||||||
Traceback (most recent call last):
|
'12345678/9012-10': error_format,
|
||||||
...
|
'64.132.916/0001-XX': error_numbersonly,
|
||||||
ValidationError: [u'Invalid CNPJ number.']
|
}
|
||||||
>>> f.clean('12345678/9012-10')
|
self.assertFieldOutput(BRCNPJField, valid, invalid)
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Invalid CNPJ number.']
|
|
||||||
>>> f.clean('64.132.916/0001-88')
|
|
||||||
'64.132.916/0001-88'
|
|
||||||
>>> f.clean('64-132-916/0001-88')
|
|
||||||
'64-132-916/0001-88'
|
|
||||||
>>> f.clean('64132916/0001-88')
|
|
||||||
'64132916/0001-88'
|
|
||||||
>>> f.clean('64.132.916/0001-XX')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field requires only numbers.']
|
|
||||||
>>> f = BRCNPJField(required=False)
|
|
||||||
>>> f.clean('')
|
|
||||||
u''
|
|
||||||
|
|
||||||
# BRCPFField #################################################################
|
def test_BRCPFField(self):
|
||||||
|
error_format = [u'Invalid CPF number.']
|
||||||
|
error_numbersonly = [u'This field requires only numbers.']
|
||||||
|
error_atmost_chars = [u'Ensure this value has at most 14 characters (it has 15).']
|
||||||
|
error_atleast_chars = [u'Ensure this value has at least 11 characters (it has 10).']
|
||||||
|
error_atmost = [u'This field requires at most 11 digits or 14 characters.']
|
||||||
|
valid = {
|
||||||
|
'663.256.017-26': '663.256.017-26',
|
||||||
|
'66325601726': '66325601726',
|
||||||
|
'375.788.573-20': '375.788.573-20',
|
||||||
|
'84828509895': '84828509895',
|
||||||
|
}
|
||||||
|
invalid = {
|
||||||
|
'489.294.654-54': error_format,
|
||||||
|
'295.669.575-98': error_format,
|
||||||
|
'539.315.127-22': error_format,
|
||||||
|
'375.788.573-XX': error_numbersonly,
|
||||||
|
'375.788.573-000': error_atmost_chars,
|
||||||
|
'123.456.78': error_atleast_chars,
|
||||||
|
'123456789555': error_atmost,
|
||||||
|
}
|
||||||
|
self.assertFieldOutput(BRCPFField, valid, invalid)
|
||||||
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRCPFField
|
def test_BRPhoneNumberField(self):
|
||||||
>>> f = BRCPFField()
|
# TODO: this doesn't test for any invalid inputs.
|
||||||
>>> f.clean('')
|
valid = {
|
||||||
Traceback (most recent call last):
|
'41-3562-3464': u'41-3562-3464',
|
||||||
...
|
'4135623464': u'41-3562-3464',
|
||||||
ValidationError: [u'This field is required.']
|
'41 3562-3464': u'41-3562-3464',
|
||||||
>>> f.clean(None)
|
'41 3562 3464': u'41-3562-3464',
|
||||||
Traceback (most recent call last):
|
'(41) 3562 3464': u'41-3562-3464',
|
||||||
...
|
'41.3562.3464': u'41-3562-3464',
|
||||||
ValidationError: [u'This field is required.']
|
'41.3562-3464': u'41-3562-3464',
|
||||||
>>> f.clean('489.294.654-54')
|
' (41) 3562.3464': u'41-3562-3464',
|
||||||
Traceback (most recent call last):
|
}
|
||||||
...
|
invalid = {}
|
||||||
ValidationError: [u'Invalid CPF number.']
|
self.assertFieldOutput(BRPhoneNumberField, valid, invalid)
|
||||||
>>> f.clean('295.669.575-98')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Invalid CPF number.']
|
|
||||||
>>> f.clean('539.315.127-22')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Invalid CPF number.']
|
|
||||||
>>> f.clean('663.256.017-26')
|
|
||||||
u'663.256.017-26'
|
|
||||||
>>> f.clean('66325601726')
|
|
||||||
u'66325601726'
|
|
||||||
>>> f.clean('375.788.573-20')
|
|
||||||
u'375.788.573-20'
|
|
||||||
>>> f.clean('84828509895')
|
|
||||||
u'84828509895'
|
|
||||||
>>> f.clean('375.788.573-XX')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field requires only numbers.']
|
|
||||||
>>> f.clean('375.788.573-000')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Ensure this value has at most 14 characters (it has 15).']
|
|
||||||
>>> f.clean('123.456.78')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Ensure this value has at least 11 characters (it has 10).']
|
|
||||||
>>> f.clean('123456789555')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field requires at most 11 digits or 14 characters.']
|
|
||||||
>>> f = BRCPFField(required=False)
|
|
||||||
>>> f.clean('')
|
|
||||||
u''
|
|
||||||
>>> f.clean(None)
|
|
||||||
u''
|
|
||||||
|
|
||||||
# BRPhoneNumberField #########################################################
|
def test_BRStateSelect(self):
|
||||||
|
f = BRStateSelect()
|
||||||
|
out = u'''<select name="states">
|
||||||
|
<option value="AC">Acre</option>
|
||||||
|
<option value="AL">Alagoas</option>
|
||||||
|
<option value="AP">Amap\xe1</option>
|
||||||
|
<option value="AM">Amazonas</option>
|
||||||
|
<option value="BA">Bahia</option>
|
||||||
|
<option value="CE">Cear\xe1</option>
|
||||||
|
<option value="DF">Distrito Federal</option>
|
||||||
|
<option value="ES">Esp\xedrito Santo</option>
|
||||||
|
<option value="GO">Goi\xe1s</option>
|
||||||
|
<option value="MA">Maranh\xe3o</option>
|
||||||
|
<option value="MT">Mato Grosso</option>
|
||||||
|
<option value="MS">Mato Grosso do Sul</option>
|
||||||
|
<option value="MG">Minas Gerais</option>
|
||||||
|
<option value="PA">Par\xe1</option>
|
||||||
|
<option value="PB">Para\xedba</option>
|
||||||
|
<option value="PR" selected="selected">Paran\xe1</option>
|
||||||
|
<option value="PE">Pernambuco</option>
|
||||||
|
<option value="PI">Piau\xed</option>
|
||||||
|
<option value="RJ">Rio de Janeiro</option>
|
||||||
|
<option value="RN">Rio Grande do Norte</option>
|
||||||
|
<option value="RS">Rio Grande do Sul</option>
|
||||||
|
<option value="RO">Rond\xf4nia</option>
|
||||||
|
<option value="RR">Roraima</option>
|
||||||
|
<option value="SC">Santa Catarina</option>
|
||||||
|
<option value="SP">S\xe3o Paulo</option>
|
||||||
|
<option value="SE">Sergipe</option>
|
||||||
|
<option value="TO">Tocantins</option>
|
||||||
|
</select>'''
|
||||||
|
self.assertEqual(f.render('states', 'PR'), out)
|
||||||
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRPhoneNumberField
|
def test_BRStateChoiceField(self):
|
||||||
>>> f = BRPhoneNumberField()
|
error_invalid = [u'Select a valid brazilian state. That state is not one of the available states.']
|
||||||
>>> f.clean('41-3562-3464')
|
valid = {
|
||||||
u'41-3562-3464'
|
'AC': 'AC',
|
||||||
>>> f.clean('4135623464')
|
'AL': 'AL',
|
||||||
u'41-3562-3464'
|
'AP': 'AP',
|
||||||
>>> f.clean('41 3562-3464')
|
'AM': 'AM',
|
||||||
u'41-3562-3464'
|
'BA': 'BA',
|
||||||
>>> f.clean('41 3562 3464')
|
'CE': 'CE',
|
||||||
u'41-3562-3464'
|
'DF': 'DF',
|
||||||
>>> f.clean('(41) 3562 3464')
|
'ES': 'ES',
|
||||||
u'41-3562-3464'
|
'GO': 'GO',
|
||||||
>>> f.clean('41.3562.3464')
|
'MA': 'MA',
|
||||||
u'41-3562-3464'
|
'MT': 'MT',
|
||||||
>>> f.clean('41.3562-3464')
|
'MS': 'MS',
|
||||||
u'41-3562-3464'
|
'MG': 'MG',
|
||||||
>>> f.clean(' (41) 3562.3464')
|
'PA': 'PA',
|
||||||
u'41-3562-3464'
|
'PB': 'PB',
|
||||||
>>> f.clean(None)
|
'PR': 'PR',
|
||||||
Traceback (most recent call last):
|
'PE': 'PE',
|
||||||
...
|
'PI': 'PI',
|
||||||
ValidationError: [u'This field is required.']
|
'RJ': 'RJ',
|
||||||
>>> f.clean('')
|
'RN': 'RN',
|
||||||
Traceback (most recent call last):
|
'RS': 'RS',
|
||||||
...
|
'RO': 'RO',
|
||||||
ValidationError: [u'This field is required.']
|
'RR': 'RR',
|
||||||
|
'SC': 'SC',
|
||||||
>>> f = BRPhoneNumberField(required=False)
|
'SP': 'SP',
|
||||||
>>> f.clean('')
|
'SE': 'SE',
|
||||||
u''
|
'TO': 'TO',
|
||||||
>>> f.clean(None)
|
}
|
||||||
u''
|
invalid = {
|
||||||
>>> f.clean(' (41) 3562.3464')
|
'pr': error_invalid,
|
||||||
u'41-3562-3464'
|
}
|
||||||
>>> f.clean('41.3562-3464')
|
self.assertFieldOutput(BRStateChoiceField, valid, invalid)
|
||||||
u'41-3562-3464'
|
|
||||||
>>> f.clean('(41) 3562 3464')
|
|
||||||
u'41-3562-3464'
|
|
||||||
>>> f.clean('4135623464')
|
|
||||||
u'41-3562-3464'
|
|
||||||
>>> f.clean('41 3562-3464')
|
|
||||||
u'41-3562-3464'
|
|
||||||
|
|
||||||
# BRStateSelect ##############################################################
|
|
||||||
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRStateSelect
|
|
||||||
>>> w = BRStateSelect()
|
|
||||||
>>> w.render('states', 'PR')
|
|
||||||
u'<select name="states">\n<option value="AC">Acre</option>\n<option value="AL">Alagoas</option>\n<option value="AP">Amap\xe1</option>\n<option value="AM">Amazonas</option>\n<option value="BA">Bahia</option>\n<option value="CE">Cear\xe1</option>\n<option value="DF">Distrito Federal</option>\n<option value="ES">Esp\xedrito Santo</option>\n<option value="GO">Goi\xe1s</option>\n<option value="MA">Maranh\xe3o</option>\n<option value="MT">Mato Grosso</option>\n<option value="MS">Mato Grosso do Sul</option>\n<option value="MG">Minas Gerais</option>\n<option value="PA">Par\xe1</option>\n<option value="PB">Para\xedba</option>\n<option value="PR" selected="selected">Paran\xe1</option>\n<option value="PE">Pernambuco</option>\n<option value="PI">Piau\xed</option>\n<option value="RJ">Rio de Janeiro</option>\n<option value="RN">Rio Grande do Norte</option>\n<option value="RS">Rio Grande do Sul</option>\n<option value="RO">Rond\xf4nia</option>\n<option value="RR">Roraima</option>\n<option value="SC">Santa Catarina</option>\n<option value="SP">S\xe3o Paulo</option>\n<option value="SE">Sergipe</option>\n<option value="TO">Tocantins</option>\n</select>'
|
|
||||||
|
|
||||||
# BRStateChoiceField #########################################################
|
|
||||||
>>> from django.contrib.localflavor.br.forms import BRStateChoiceField
|
|
||||||
>>> f = BRStateChoiceField()
|
|
||||||
>>> ', '.join([f.clean(s) for s, _ in f.widget.choices])
|
|
||||||
u'AC, AL, AP, AM, BA, CE, DF, ES, GO, MA, MT, MS, MG, PA, PB, PR, PE, PI, RJ, RN, RS, RO, RR, SC, SP, SE, TO'
|
|
||||||
>>> f.clean('')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'This field is required.']
|
|
||||||
>>> f.clean('pr')
|
|
||||||
Traceback (most recent call last):
|
|
||||||
...
|
|
||||||
ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.']
|
|
||||||
"""
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from localflavor.br import tests as localflavor_br_tests
|
|
||||||
from localflavor.ca import tests as localflavor_ca_tests
|
from localflavor.ca import tests as localflavor_ca_tests
|
||||||
from localflavor.ch import tests as localflavor_ch_tests
|
from localflavor.ch import tests as localflavor_ch_tests
|
||||||
from localflavor.cl import tests as localflavor_cl_tests
|
from localflavor.cl import tests as localflavor_cl_tests
|
||||||
|
@ -30,12 +29,12 @@ from localflavor.at import ATLocalFlavorTests
|
||||||
from localflavor.de import DELocalFlavorTests
|
from localflavor.de import DELocalFlavorTests
|
||||||
from localflavor.au import AULocalFlavorTests
|
from localflavor.au import AULocalFlavorTests
|
||||||
from localflavor.be import BELocalFlavorTests
|
from localflavor.be import BELocalFlavorTests
|
||||||
|
from localflavor.br import BRLocalFlavorTests
|
||||||
from localflavor.il import ILLocalFlavorTests
|
from localflavor.il import ILLocalFlavorTests
|
||||||
from localflavor.tr import TRLocalFlavorTests
|
from localflavor.tr import TRLocalFlavorTests
|
||||||
|
|
||||||
|
|
||||||
__test__ = {
|
__test__ = {
|
||||||
'localflavor_br_tests': localflavor_br_tests,
|
|
||||||
'localflavor_ca_tests': localflavor_ca_tests,
|
'localflavor_ca_tests': localflavor_ca_tests,
|
||||||
'localflavor_ch_tests': localflavor_ch_tests,
|
'localflavor_ch_tests': localflavor_ch_tests,
|
||||||
'localflavor_cl_tests': localflavor_cl_tests,
|
'localflavor_cl_tests': localflavor_cl_tests,
|
||||||
|
|
|
@ -17,6 +17,7 @@ from regressiontests.forms.localflavortests import (
|
||||||
ATLocalFlavorTests,
|
ATLocalFlavorTests,
|
||||||
AULocalFlavorTests,
|
AULocalFlavorTests,
|
||||||
BELocalFlavorTests,
|
BELocalFlavorTests,
|
||||||
|
BRLocalFlavorTests,
|
||||||
DELocalFlavorTests,
|
DELocalFlavorTests,
|
||||||
ILLocalFlavorTests,
|
ILLocalFlavorTests,
|
||||||
TRLocalFlavorTests,
|
TRLocalFlavorTests,
|
||||||
|
|
Loading…
Reference in New Issue