[1.2.X] Converted the Czech localflavor tests to be unittests. We have always been at war with doctests. Thanks to Idan Gazit. Partial backport of [15062].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15063 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-27 07:47:25 +00:00
parent b1487c499b
commit 05588a1897
4 changed files with 100 additions and 156 deletions

View File

@ -138,4 +138,3 @@ class CZICNumberField(Field):
return u'%s' % value
raise ValidationError(self.error_messages['invalid'])

View File

@ -1,126 +1,96 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ CZ Form Fields
from django.contrib.localflavor.cz.forms import (CZPostalCodeField,
CZRegionSelect, CZBirthNumberField, CZICNumberField)
tests = r"""
# CZPostalCodeField #########################################################
from django.core.exceptions import ValidationError
from utils import LocalFlavorTestCase
>>> from django.contrib.localflavor.cz.forms import CZPostalCodeField
>>> f = CZPostalCodeField()
>>> f.clean('84545x')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.']
>>> f.clean('91909')
u'91909'
>>> f.clean('917 01')
u'91701'
>>> f.clean('12345')
u'12345'
>>> f.clean('123456')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.']
>>> f.clean('1234')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.']
>>> f.clean('123 4')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.']
# CZRegionSelect ############################################################
class CZLocalFlavorTests(LocalFlavorTestCase):
def test_CZRegionSelect(self):
f = CZRegionSelect()
out = u'''<select name="regions">
<option value="PR">Prague</option>
<option value="CE">Central Bohemian Region</option>
<option value="SO">South Bohemian Region</option>
<option value="PI">Pilsen Region</option>
<option value="CA">Carlsbad Region</option>
<option value="US">Usti Region</option>
<option value="LB">Liberec Region</option>
<option value="HK">Hradec Region</option>
<option value="PA">Pardubice Region</option>
<option value="VY">Vysocina Region</option>
<option value="SM">South Moravian Region</option>
<option value="OL">Olomouc Region</option>
<option value="ZL">Zlin Region</option>
<option value="MS">Moravian-Silesian Region</option>
</select>'''
self.assertEqual(f.render('regions', 'TT'), out)
>>> from django.contrib.localflavor.cz.forms import CZRegionSelect
>>> w = CZRegionSelect()
>>> w.render('regions', 'TT')
u'<select name="regions">\n<option value="PR">Prague</option>\n<option value="CE">Central Bohemian Region</option>\n<option value="SO">South Bohemian Region</option>\n<option value="PI">Pilsen Region</option>\n<option value="CA">Carlsbad Region</option>\n<option value="US">Usti Region</option>\n<option value="LB">Liberec Region</option>\n<option value="HK">Hradec Region</option>\n<option value="PA">Pardubice Region</option>\n<option value="VY">Vysocina Region</option>\n<option value="SM">South Moravian Region</option>\n<option value="OL">Olomouc Region</option>\n<option value="ZL">Zlin Region</option>\n<option value="MS">Moravian-Silesian Region</option>\n</select>'
def test_CZPostalCodeField(self):
error_format = [u'Enter a postal code in the format XXXXX or XXX XX.']
valid = {
'91909': '91909',
'917 01': '91701',
'12345': '12345',
}
invalid = {
'84545x': error_format,
'123456': error_format,
'1234': error_format,
'123 4': error_format,
}
self.assertFieldOutput(CZPostalCodeField, valid, invalid)
# CZBirthNumberField ########################################################
def test_CZBirthNumberField(self):
error_format = [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.']
error_invalid = [u'Enter a valid birth number.']
valid = {
'880523/1237': '880523/1237',
'8805231237': '8805231237',
'880523/000': '880523/000',
'880523000': '880523000',
'882101/0011': '882101/0011',
}
invalid = {
'123456/12': error_format,
'123456/12345': error_format,
'12345612': error_format,
'12345612345': error_format,
'880523/1239': error_invalid,
'8805231239': error_invalid,
'990101/0011': error_invalid,
}
self.assertFieldOutput(CZBirthNumberField, valid, invalid)
>>> from django.contrib.localflavor.cz.forms import CZBirthNumberField
>>> f = CZBirthNumberField()
>>> f.clean('880523/1237')
u'880523/1237'
>>> f.clean('8805231237')
u'8805231237'
>>> f.clean('880523/000')
u'880523/000'
>>> f.clean('880523000')
u'880523000'
>>> f.clean('882101/0011')
u'882101/0011'
>>> f.clean('880523/1237', 'm')
u'880523/1237'
>>> f.clean('885523/1231', 'f')
u'885523/1231'
>>> f.clean('123456/12')
Traceback (most recent call last):
...
ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.']
>>> f.clean('123456/12345')
Traceback (most recent call last):
...
ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.']
>>> f.clean('12345612')
Traceback (most recent call last):
...
ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.']
>>> f.clean('12345612345')
Traceback (most recent call last):
...
ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.']
>>> f.clean('881523/0000', 'm')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('885223/0000', 'm')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('881223/0000', 'f')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('886523/0000', 'f')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('880523/1239')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('8805231239')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
>>> f.clean('990101/0011')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid birth number.']
# These tests should go away in 1.4.
# http://code.djangoproject.com/ticket/14593
f = CZBirthNumberField()
self.assertEqual(f.clean('880523/1237', 'm'), '880523/1237'),
self.assertEqual(f.clean('885523/1231', 'f'), '885523/1231')
for args in [
('881523/0000', 'm'),
('885223/0000', 'm'),
('881523/0000', 'f'),
('885223/0000', 'f'),
]:
try:
f.clean(*args)
except ValidationError, e:
self.assertEqual(e.messages, error_invalid)
else:
self.fail()
# CZICNumberField ########################################################
>>> from django.contrib.localflavor.cz.forms import CZICNumberField
>>> f = CZICNumberField()
>>> f.clean('12345679')
u'12345679'
>>> f.clean('12345601')
u'12345601'
>>> f.clean('12345661')
u'12345661'
>>> f.clean('12345610')
u'12345610'
>>> f.clean('1234567')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IC number.']
>>> f.clean('12345660')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IC number.']
>>> f.clean('12345600')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid IC number.']
"""
def test_CZICNumberField(self):
error_invalid = [u'Enter a valid IC number.']
valid ={
'12345679': '12345679',
'12345601': '12345601',
'12345661': '12345661',
'12345610': '12345610',
}
invalid = {
'1234567': error_invalid,
'12345660': error_invalid,
'12345600': error_invalid,
}
self.assertFieldOutput(CZICNumberField, valid, invalid)

View File

@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests
from localflavor.ar import ARLocalFlavorTests
from localflavor.at import ATLocalFlavorTests
from localflavor.au import AULocalFlavorTests
@ -9,6 +6,7 @@ from localflavor.br import BRLocalFlavorTests
from localflavor.ca import CALocalFlavorTests
from localflavor.ch import CHLocalFlavorTests
from localflavor.cl import CLLocalFlavorTests
from localflavor.cz import CZLocalFlavorTests
from localflavor.de import DELocalFlavorTests
from localflavor.es import ESLocalFlavorTests
from localflavor.fi import FILocalFlavorTests
@ -31,7 +29,3 @@ from localflavor.us import USLocalFlavorTests
from localflavor.uy import UYLocalFlavorTests
from localflavor.za import ZALocalFlavorTests
__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
}

View File

@ -12,33 +12,14 @@ from validators import TestFieldWithValidators
from widgets import *
from regressiontests.forms.localflavortests import (
__test__,
ARLocalFlavorTests,
ATLocalFlavorTests,
AULocalFlavorTests,
BRLocalFlavorTests,
CALocalFlavorTests,
CHLocalFlavorTests,
CLLocalFlavorTests,
DELocalFlavorTests,
ESLocalFlavorTests,
FILocalFlavorTests,
FRLocalFlavorTests,
GenericLocalFlavorTests,
IDLocalFlavorTests,
IELocalFlavorTests,
ISLocalFlavorTests,
ITLocalFlavorTests,
JPLocalFlavorTests,
KWLocalFlavorTests,
NLLocalFlavorTests,
PLLocalFlavorTests,
PTLocalFlavorTests,
ROLocalFlavorTests,
SELocalFlavorTests,
SKLocalFlavorTests,
UKLocalFlavorTests,
USLocalFlavorTests,
UYLocalFlavorTests,
ZALocalFlavorTests,
ARLocalFlavorTests, ATLocalFlavorTests, AULocalFlavorTests,
BRLocalFlavorTests, CALocalFlavorTests, CHLocalFlavorTests,
CLLocalFlavorTests, CZLocalFlavorTests, DELocalFlavorTests,
ESLocalFlavorTests, FILocalFlavorTests, FRLocalFlavorTests,
GenericLocalFlavorTests, IDLocalFlavorTests, IELocalFlavorTests,
ISLocalFlavorTests, ITLocalFlavorTests, JPLocalFlavorTests,
KWLocalFlavorTests, NLLocalFlavorTests, PLLocalFlavorTests,
PTLocalFlavorTests, ROLocalFlavorTests, SELocalFlavorTests,
SKLocalFlavorTests, UKLocalFlavorTests, USLocalFlavorTests,
UYLocalFlavorTests, ZALocalFlavorTests
)