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

View File

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

View File

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

View File

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