Fixed #14499 -- ATSocialSecurityNumberField now responds to all EMPTY_VALUES correctly. Also converted Austrian localflavor doctests to unittests. We have always been at war with doctests. Thanks to Idan Gazit for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14873 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-12 04:33:50 +00:00
parent aa05224dba
commit 575962c213
4 changed files with 51 additions and 81 deletions

View File

@ -4,12 +4,14 @@ AT-specific Form helpers
import re
from django.utils.translation import ugettext_lazy as _
from django.forms.fields import Field, RegexField, Select
from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError
from django.forms.fields import Field, RegexField, Select
from django.utils.translation import ugettext_lazy as _
re_ssn = re.compile(r'^\d{4} \d{6}')
class ATZipCodeField(RegexField):
"""
A form field that validates its input is an Austrian postcode.
@ -49,6 +51,9 @@ class ATSocialSecurityNumberField(Field):
}
def clean(self, value):
value = super(ATSocialSecurityNumberField, self).clean(value)
if value in EMPTY_VALUES:
return u""
if not re_ssn.search(value):
raise ValidationError(self.error_messages['invalid'])
sqnr, date = value.split(" ")
@ -62,4 +67,3 @@ class ATSocialSecurityNumberField(Field):
if res != int(check):
raise ValidationError(self.error_messages['invalid'])
return u'%s%s %s'%(sqnr, check, date,)

View File

@ -1,80 +1,45 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ AT form fields.
from django.contrib.localflavor.at.forms import (ATZipCodeField, ATStateSelect,
ATSocialSecurityNumberField)
tests = r"""
# ATZipCodeField ###########################################################
>>> from django.contrib.localflavor.at.forms import ATZipCodeField
>>> f = ATZipCodeField()
>>> f.clean('1150')
u'1150'
>>> f.clean('4020')
u'4020'
>>> f.clean('8020')
u'8020'
>>> f.clean('111222')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXX.']
>>> f.clean('eeffee')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXX.']
>>> f.clean(u'')
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.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
from utils import LocalFlavorTestCase
>>> f = ATZipCodeField(required=False)
>>> f.clean('1150')
u'1150'
>>> f.clean('4020')
u'4020'
>>> f.clean('8020')
u'8020'
>>> f.clean('111222')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXX.']
>>> f.clean('eeffee')
Traceback (most recent call last):
...
ValidationError: [u'Enter a zip code in the format XXXX.']
>>> f.clean(None)
u''
>>> f.clean('')
u''
>>> f.clean(u'')
u''
class ATLocalFlavorTests(LocalFlavorTestCase):
def test_ATStateSelect(self):
f = ATStateSelect()
out = u'''<select name="bundesland">
<option value="BL">Burgenland</option>
<option value="KA">Carinthia</option>
<option value="NO">Lower Austria</option>
<option value="OO">Upper Austria</option>
<option value="SA">Salzburg</option>
<option value="ST">Styria</option>
<option value="TI">Tyrol</option>
<option value="VO">Vorarlberg</option>
<option value="WI" selected="selected">Vienna</option>
</select>'''
self.assertEqual(f.render('bundesland', 'WI'), out)
# ATStateSelect ##################################################################
def test_ATZipCodeField(self):
error_format = [u'Enter a zip code in the format XXXX.']
valid = {
'1150': '1150',
'4020': '4020',
'8020': '8020',
}
invalid = {
'111222': error_format,
'eeffee': error_format,
}
self.assertFieldOutput(ATZipCodeField, valid, invalid)
>>> from django.contrib.localflavor.at.forms import ATStateSelect
>>> f = ATStateSelect()
>>> f.render('bundesland', 'WI')
u'<select name="bundesland">\n<option value="BL">Burgenland</option>\n<option value="KA">Carinthia</option>\n<option value="NO">Lower Austria</option>\n<option value="OO">Upper Austria</option>\n<option value="SA">Salzburg</option>\n<option value="ST">Styria</option>\n<option value="TI">Tyrol</option>\n<option value="VO">Vorarlberg</option>\n<option value="WI" selected="selected">Vienna</option>\n</select>'
# ATSocialSecurityNumberField ################################################
>>> from django.contrib.localflavor.at.forms import ATSocialSecurityNumberField
>>> f = ATSocialSecurityNumberField()
>>> f.clean('1237 010180')
u'1237 010180'
>>> f.clean('1237 010181')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
>>> f.clean('12370 010180')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
"""
def test_ATSocialSecurityNumberField(self):
error_format = [u'Enter a valid Austrian Social Security Number in XXXX XXXXXX format.']
valid = {
'1237 010180': '1237 010180',
}
invalid = {
'1237 010181': error_format,
'12370 010180': error_format,
}
self.assertFieldOutput(ATSocialSecurityNumberField, valid, invalid)

View File

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
from localflavor.at import tests as localflavor_at_tests
from localflavor.au import tests as localflavor_au_tests
from localflavor.br import tests as localflavor_br_tests
from localflavor.ca import tests as localflavor_ca_tests
@ -28,6 +27,7 @@ from localflavor.uy import tests as localflavor_uy_tests
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.be import BELocalFlavorTests
from localflavor.il import ILLocalFlavorTests
@ -35,7 +35,6 @@ from localflavor.tr import TRLocalFlavorTests
__test__ = {
'localflavor_at_tests': localflavor_at_tests,
'localflavor_au_tests': localflavor_au_tests,
'localflavor_br_tests': localflavor_br_tests,
'localflavor_ca_tests': localflavor_ca_tests,

View File

@ -13,8 +13,10 @@ from widgets import *
from regressiontests.forms.localflavortests import (
__test__,
ARLocalFlavorTests,
ATLocalFlavorTests,
BELocalFlavorTests,
DELocalFlavorTests,
ILLocalFlavorTests,
TRLocalFlavorTests
TRLocalFlavorTests,
)