Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Converted Japanese localflavor doctests to unittests. We have…

… always been at war with doctests. Thanks to Idan Gazit. Backport of [14946].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14970 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5c3ede2171ba6b7f2fb30956cb1ecb84064f143f 1 parent f2a8534
Alex Gaynor authored December 18, 2010
79  tests/regressiontests/forms/localflavor/jp.py
... ...
@@ -1,60 +1,13 @@
1  
-# -*- coding: utf-8 -*-
2  
-# Tests for the contrib/localflavor/ JP form fields.
  1
+from django.contrib.localflavor.jp.forms import (JPPostalCodeField,
  2
+    JPPrefectureSelect)
3 3
 
4  
-tests = r"""
5  
-# JPPostalCodeField ###############################################################
  4
+from utils import LocalFlavorTestCase
6 5
 
7  
-A form field that validates its input is a Japanese postcode.
8 6
 
9  
-Accepts 7 digits(with/out hyphen).
10  
->>> from django.contrib.localflavor.jp.forms import JPPostalCodeField
11  
->>> f = JPPostalCodeField()
12  
->>> f.clean('251-0032')
13  
-u'2510032'
14  
->>> f.clean('2510032')
15  
-u'2510032'
16  
->>> f.clean('2510-032')
17  
-Traceback (most recent call last):
18  
-...
19  
-ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
20  
->>> f.clean('251a0032')
21  
-Traceback (most recent call last):
22  
-...
23  
-ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
24  
->>> f.clean('a51-0032')
25  
-Traceback (most recent call last):
26  
-...
27  
-ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
28  
->>> f.clean('25100321')
29  
-Traceback (most recent call last):
30  
-...
31  
-ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
32  
->>> f.clean('')
33  
-Traceback (most recent call last):
34  
-...
35  
-ValidationError: [u'This field is required.']
36  
-
37  
->>> f = JPPostalCodeField(required=False)
38  
->>> f.clean('251-0032')
39  
-u'2510032'
40  
->>> f.clean('2510032')
41  
-u'2510032'
42  
->>> f.clean('2510-032')
43  
-Traceback (most recent call last):
44  
-...
45  
-ValidationError: [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
46  
->>> f.clean('')
47  
-u''
48  
->>> f.clean(None)
49  
-u''
50  
-
51  
-# JPPrefectureSelect ###############################################################
52  
-
53  
-A Select widget that uses a list of Japanese prefectures as its choices.
54  
->>> from django.contrib.localflavor.jp.forms import JPPrefectureSelect
55  
->>> w = JPPrefectureSelect()
56  
->>> print w.render('prefecture', 'kanagawa')
57  
-<select name="prefecture">
  7
+class JPLocalFlavorTests(LocalFlavorTestCase):
  8
+    def test_JPPrefectureSelect(self):
  9
+        f = JPPrefectureSelect()
  10
+        out = u'''<select name="prefecture">
58 11
 <option value="hokkaido">Hokkaido</option>
59 12
 <option value="aomori">Aomori</option>
60 13
 <option value="iwate">Iwate</option>
@@ -102,5 +55,19 @@
102 55
 <option value="miyazaki">Miyazaki</option>
103 56
 <option value="kagoshima">Kagoshima</option>
104 57
 <option value="okinawa">Okinawa</option>
105  
-</select>
106  
-"""
  58
+</select>'''
  59
+        self.assertEqual(f.render('prefecture', 'kanagawa'), out)
  60
+
  61
+    def test_JPPostalCodeField(self):
  62
+        error_format = [u'Enter a postal code in the format XXXXXXX or XXX-XXXX.']
  63
+        valid = {
  64
+                '251-0032': '2510032',
  65
+                '2510032': '2510032',
  66
+        }
  67
+        invalid = {
  68
+                '2510-032': error_format,
  69
+                '251a0032': error_format,
  70
+                'a51-0032': error_format,
  71
+                '25100321': error_format,
  72
+        }
  73
+        self.assertFieldOutput(JPPostalCodeField, valid, invalid)
3  tests/regressiontests/forms/localflavortests.py
... ...
@@ -1,6 +1,5 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from localflavor.cz import tests as localflavor_cz_tests
3  
-from localflavor.jp import tests as localflavor_jp_tests
4 3
 from localflavor.kw import tests as localflavor_kw_tests
5 4
 from localflavor.nl import tests as localflavor_nl_tests
6 5
 from localflavor.pl import tests as localflavor_pl_tests
@@ -30,11 +29,11 @@
30 29
 from localflavor.ie import IELocalFlavorTests
31 30
 from localflavor.is_ import ISLocalFlavorTests
32 31
 from localflavor.it import ITLocalFlavorTests
  32
+from localflavor.jp import JPLocalFlavorTests
33 33
 
34 34
 
35 35
 __test__ = {
36 36
     'localflavor_cz_tests': localflavor_cz_tests,
37  
-    'localflavor_jp_tests': localflavor_jp_tests,
38 37
     'localflavor_kw_tests': localflavor_kw_tests,
39 38
     'localflavor_nl_tests': localflavor_nl_tests,
40 39
     'localflavor_pl_tests': localflavor_pl_tests,
1  tests/regressiontests/forms/tests/__init__.py
@@ -29,4 +29,5 @@
29 29
     IELocalFlavorTests,
30 30
     ISLocalFlavorTests,
31 31
     ITLocalFlavorTests,
  32
+    JPLocalFlavorTests,
32 33
 )

0 notes on commit 5c3ede2

Please sign in to comment.
Something went wrong with that request. Please try again.