Skip to content

Commit

Permalink
Converted Japanese localflavor doctests to unittests. We have always …
Browse files Browse the repository at this point in the history
…been at war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14946 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Dec 18, 2010
1 parent f8caeef commit 6e3be3b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 58 deletions.
79 changes: 23 additions & 56 deletions tests/regressiontests/forms/localflavor/jp.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,13 @@
# -*- coding: utf-8 -*-
# Tests for the contrib/localflavor/ JP form fields.
from django.contrib.localflavor.jp.forms import (JPPostalCodeField,
JPPrefectureSelect)

tests = r"""
# JPPostalCodeField ###############################################################
from utils import LocalFlavorTestCase

A form field that validates its input is a Japanese postcode.

Accepts 7 digits(with/out hyphen).
>>> from django.contrib.localflavor.jp.forms import JPPostalCodeField
>>> f = JPPostalCodeField()
>>> f.clean('251-0032')
u'2510032'
>>> f.clean('2510032')
u'2510032'
>>> f.clean('2510-032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('251a0032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('a51-0032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('25100321')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('')
Traceback (most recent call last):
...
ValidationError: [u'This field is required.']
>>> f = JPPostalCodeField(required=False)
>>> f.clean('251-0032')
u'2510032'
>>> f.clean('2510032')
u'2510032'
>>> f.clean('2510-032')
Traceback (most recent call last):
...
ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
>>> f.clean('')
u''
>>> f.clean(None)
u''
# JPPrefectureSelect ###############################################################
A Select widget that uses a list of Japanese prefectures as its choices.
>>> from django.contrib.localflavor.jp.forms import JPPrefectureSelect
>>> w = JPPrefectureSelect()
>>> print w.render('prefecture', 'kanagawa')
<select name="prefecture">
class JPLocalFlavorTests(LocalFlavorTestCase):
def test_JPPrefectureSelect(self):
f = JPPrefectureSelect()
out = u'''<select name="prefecture">
<option value="hokkaido">Hokkaido</option>
<option value="aomori">Aomori</option>
<option value="iwate">Iwate</option>
Expand Down Expand Up @@ -102,5 +55,19 @@
<option value="miyazaki">Miyazaki</option>
<option value="kagoshima">Kagoshima</option>
<option value="okinawa">Okinawa</option>
</select>
"""
</select>'''
self.assertEqual(f.render('prefecture', 'kanagawa'), out)

def test_JPPostalCodeField(self):
error_format = [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
valid = {
'251-0032': '2510032',
'2510032': '2510032',
}
invalid = {
'2510-032': error_format,
'251a0032': error_format,
'a51-0032': error_format,
'25100321': error_format,
}
self.assertFieldOutput(JPPostalCodeField, valid, invalid)
3 changes: 1 addition & 2 deletions tests/regressiontests/forms/localflavortests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests
from localflavor.jp import tests as localflavor_jp_tests
from localflavor.kw import tests as localflavor_kw_tests
from localflavor.nl import tests as localflavor_nl_tests
from localflavor.pl import tests as localflavor_pl_tests
Expand Down Expand Up @@ -31,12 +30,12 @@
from localflavor.il import ILLocalFlavorTests
from localflavor.is_ import ISLocalFlavorTests
from localflavor.it import ITLocalFlavorTests
from localflavor.jp import JPLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests


__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_jp_tests': localflavor_jp_tests,
'localflavor_kw_tests': localflavor_kw_tests,
'localflavor_nl_tests': localflavor_nl_tests,
'localflavor_pl_tests': localflavor_pl_tests,
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/forms/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
ILLocalFlavorTests,
ISLocalFlavorTests,
ITLocalFlavorTests,
JPLocalFlavorTests,
TRLocalFlavorTests,
)

0 comments on commit 6e3be3b

Please sign in to comment.