Converted Indonesian localflavor doctests to unittests. We have always been a war with doctests. Thanks to Idan Gazit.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14941 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2010-12-18 20:31:00 +00:00
parent 1eee7757b7
commit 55c81c36e6
4 changed files with 194 additions and 168 deletions

View File

@ -1,177 +1,194 @@
# -*- coding: utf-8 -*- import warnings
# Tests for the contrib/localflavor/ ID form fields.
tests = r""" from django.contrib.localflavor.id.forms import (IDPhoneNumberField,
IDPostCodeField, IDNationalIdentityNumberField, IDLicensePlateField,
IDProvinceSelect, IDLicensePlatePrefixSelect)
# IDPhoneNumberField ######################################################## from utils import LocalFlavorTestCase
>>> import warnings
>>> warnings.filterwarnings("ignore", category=RuntimeWarning, module='django.contrib.localflavor.id.id_choices')
>>> from django.contrib.localflavor.id.forms import IDPhoneNumberField
>>> f = IDPhoneNumberField(required=False)
>>> f.clean('')
u''
>>> f.clean('0812-3456789')
u'0812-3456789'
>>> f.clean('081234567890')
u'081234567890'
>>> f.clean('021 345 6789')
u'021 345 6789'
>>> f.clean('0213456789')
u'0213456789'
>>> f.clean('0123456789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
>>> f.clean('+62-21-3456789')
u'+62-21-3456789'
>>> f.clean('+62-021-3456789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
>>> f.clean('(021) 345 6789')
u'(021) 345 6789'
>>> f.clean('+62-021-3456789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
>>> f.clean('+62-0812-3456789')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
>>> f.clean('0812345678901')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid phone number']
# IDPostCodeField ############################################################ class IDLocalFlavorTests(LocalFlavorTestCase):
def setUp(self):
self.save_warnings_state()
warnings.filterwarnings(
"ignore",
category=RuntimeWarning,
module='django.contrib.localflavor.id.id_choices'
)
>>> from django.contrib.localflavor.id.forms import IDPostCodeField def tearDown(self):
>>> f = IDPostCodeField(required=False) self.restore_warnings_state()
>>> f.clean('')
u''
>>> f.clean('12340')
u'12340'
>>> f.clean('25412')
u'25412'
>>> f.clean(' 12340 ')
u'12340'
>>> f.clean('12 3 4 0')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid post code']
>>> f.clean('12345')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid post code']
>>> f.clean('10100')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid post code']
>>> f.clean('123456')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid post code']
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid post code']
# IDNationalIdentityNumberField ######################################################### def test_IDProvinceSelect(self):
f = IDProvinceSelect()
out = u'''<select name="provinces">
<option value="ACE">Aceh</option>
<option value="BLI">Bali</option>
<option value="BTN">Banten</option>
<option value="BKL">Bengkulu</option>
<option value="DIY">Yogyakarta</option>
<option value="JKT">Jakarta</option>
<option value="GOR">Gorontalo</option>
<option value="JMB">Jambi</option>
<option value="JBR">Jawa Barat</option>
<option value="JTG">Jawa Tengah</option>
<option value="JTM">Jawa Timur</option>
<option value="KBR">Kalimantan Barat</option>
<option value="KSL">Kalimantan Selatan</option>
<option value="KTG">Kalimantan Tengah</option>
<option value="KTM">Kalimantan Timur</option>
<option value="BBL">Kepulauan Bangka-Belitung</option>
<option value="KRI">Kepulauan Riau</option>
<option value="LPG" selected="selected">Lampung</option>
<option value="MLK">Maluku</option>
<option value="MUT">Maluku Utara</option>
<option value="NTB">Nusa Tenggara Barat</option>
<option value="NTT">Nusa Tenggara Timur</option>
<option value="PPA">Papua</option>
<option value="PPB">Papua Barat</option>
<option value="RIU">Riau</option>
<option value="SLB">Sulawesi Barat</option>
<option value="SLS">Sulawesi Selatan</option>
<option value="SLT">Sulawesi Tengah</option>
<option value="SLR">Sulawesi Tenggara</option>
<option value="SLU">Sulawesi Utara</option>
<option value="SMB">Sumatera Barat</option>
<option value="SMS">Sumatera Selatan</option>
<option value="SMU">Sumatera Utara</option>
</select>'''
self.assertEqual(f.render('provinces', 'LPG'), out)
>>> from django.contrib.localflavor.id.forms import IDNationalIdentityNumberField def test_IDLicensePlatePrefixSelect(self):
>>> f = IDNationalIdentityNumberField(required=False) f = IDLicensePlatePrefixSelect()
>>> f.clean('') out = u'''<select name="codes">
u'' <option value="A">Banten</option>
>>> f.clean(' 12.3456.010178 3456 ') <option value="AA">Magelang</option>
u'12.3456.010178.3456' <option value="AB">Yogyakarta</option>
>>> f.clean('1234560101783456') <option value="AD">Surakarta - Solo</option>
u'12.3456.010178.3456' <option value="AE">Madiun</option>
>>> f.clean('12.3456.010101.3456') <option value="AG">Kediri</option>
u'12.3456.010101.3456' <option value="B">Jakarta</option>
>>> f.clean('12.3456.310278.3456') <option value="BA">Sumatera Barat</option>
Traceback (most recent call last): <option value="BB">Tapanuli</option>
... <option value="BD">Bengkulu</option>
ValidationError: [u'Enter a valid NIK/KTP number'] <option value="BE" selected="selected">Lampung</option>
>>> f.clean('00.0000.010101.0000') <option value="BG">Sumatera Selatan</option>
Traceback (most recent call last): <option value="BH">Jambi</option>
... <option value="BK">Sumatera Utara</option>
ValidationError: [u'Enter a valid NIK/KTP number'] <option value="BL">Nanggroe Aceh Darussalam</option>
>>> f.clean('1234567890123456') <option value="BM">Riau</option>
Traceback (most recent call last): <option value="BN">Kepulauan Bangka Belitung</option>
... <option value="BP">Kepulauan Riau</option>
ValidationError: [u'Enter a valid NIK/KTP number'] <option value="CC">Corps Consulate</option>
>>> f.clean('foo') <option value="CD">Corps Diplomatic</option>
Traceback (most recent call last): <option value="D">Bandung</option>
... <option value="DA">Kalimantan Selatan</option>
ValidationError: [u'Enter a valid NIK/KTP number'] <option value="DB">Sulawesi Utara Daratan</option>
<option value="DC">Sulawesi Barat</option>
<option value="DD">Sulawesi Selatan</option>
<option value="DE">Maluku</option>
<option value="DG">Maluku Utara</option>
<option value="DH">NTT - Timor</option>
<option value="DK">Bali</option>
<option value="DL">Sulawesi Utara Kepulauan</option>
<option value="DM">Gorontalo</option>
<option value="DN">Sulawesi Tengah</option>
<option value="DR">NTB - Lombok</option>
<option value="DS">Papua dan Papua Barat</option>
<option value="DT">Sulawesi Tenggara</option>
<option value="E">Cirebon</option>
<option value="EA">NTB - Sumbawa</option>
<option value="EB">NTT - Flores</option>
<option value="ED">NTT - Sumba</option>
<option value="F">Bogor</option>
<option value="G">Pekalongan</option>
<option value="H">Semarang</option>
<option value="K">Pati</option>
<option value="KB">Kalimantan Barat</option>
<option value="KH">Kalimantan Tengah</option>
<option value="KT">Kalimantan Timur</option>
<option value="L">Surabaya</option>
<option value="M">Madura</option>
<option value="N">Malang</option>
<option value="P">Jember</option>
<option value="R">Banyumas</option>
<option value="RI">Federal Government</option>
<option value="S">Bojonegoro</option>
<option value="T">Purwakarta</option>
<option value="W">Sidoarjo</option>
<option value="Z">Garut</option>
</select>'''
self.assertEqual(f.render('codes', 'BE'), out)
# IDProvinceSelect ########################################################## def test_IDPhoneNumberField(self):
error_invalid = [u'Enter a valid phone number']
valid = {
'0812-3456789': u'0812-3456789',
'081234567890': u'081234567890',
'021 345 6789': u'021 345 6789',
'0213456789': u'0213456789',
'+62-21-3456789': u'+62-21-3456789',
'(021) 345 6789': u'(021) 345 6789',
}
invalid = {
'0123456789': error_invalid,
'+62-021-3456789': error_invalid,
'+62-021-3456789': error_invalid,
'+62-0812-3456789': error_invalid,
'0812345678901': error_invalid,
'foo': error_invalid,
}
self.assertFieldOutput(IDPhoneNumberField, valid, invalid)
>>> from django.contrib.localflavor.id.forms import IDProvinceSelect def test_IDPostCodeField(self):
>>> s = IDProvinceSelect() error_invalid = [u'Enter a valid post code']
>>> s.render('provinces', 'LPG') valid = {
u'<select name="provinces">\n<option value="ACE">Aceh</option>\n<option value="BLI">Bali</option>\n<option value="BTN">Banten</option>\n<option value="BKL">Bengkulu</option>\n<option value="DIY">Yogyakarta</option>\n<option value="JKT">Jakarta</option>\n<option value="GOR">Gorontalo</option>\n<option value="JMB">Jambi</option>\n<option value="JBR">Jawa Barat</option>\n<option value="JTG">Jawa Tengah</option>\n<option value="JTM">Jawa Timur</option>\n<option value="KBR">Kalimantan Barat</option>\n<option value="KSL">Kalimantan Selatan</option>\n<option value="KTG">Kalimantan Tengah</option>\n<option value="KTM">Kalimantan Timur</option>\n<option value="BBL">Kepulauan Bangka-Belitung</option>\n<option value="KRI">Kepulauan Riau</option>\n<option value="LPG" selected="selected">Lampung</option>\n<option value="MLK">Maluku</option>\n<option value="MUT">Maluku Utara</option>\n<option value="NTB">Nusa Tenggara Barat</option>\n<option value="NTT">Nusa Tenggara Timur</option>\n<option value="PPA">Papua</option>\n<option value="PPB">Papua Barat</option>\n<option value="RIU">Riau</option>\n<option value="SLB">Sulawesi Barat</option>\n<option value="SLS">Sulawesi Selatan</option>\n<option value="SLT">Sulawesi Tengah</option>\n<option value="SLR">Sulawesi Tenggara</option>\n<option value="SLU">Sulawesi Utara</option>\n<option value="SMB">Sumatera Barat</option>\n<option value="SMS">Sumatera Selatan</option>\n<option value="SMU">Sumatera Utara</option>\n</select>' '12340': u'12340',
'25412': u'25412',
' 12340 ': u'12340',
}
invalid = {
'12 3 4 0': error_invalid,
'12345': error_invalid,
'10100': error_invalid,
'123456': error_invalid,
'foo': error_invalid,
}
self.assertFieldOutput(IDPostCodeField, valid, invalid)
# IDLicensePlatePrefixelect ######################################################################## def test_IDNationalIdentityNumberField(self):
error_invalid = [u'Enter a valid NIK/KTP number']
valid = {
' 12.3456.010178 3456 ': u'12.3456.010178.3456',
'1234560101783456': u'12.3456.010178.3456',
'12.3456.010101.3456': u'12.3456.010101.3456',
}
invalid = {
'12.3456.310278.3456': error_invalid,
'00.0000.010101.0000': error_invalid,
'1234567890123456': error_invalid,
'foo': error_invalid,
}
self.assertFieldOutput(IDNationalIdentityNumberField, valid, invalid)
>>> from django.contrib.localflavor.id.forms import IDLicensePlatePrefixSelect def test_IDLicensePlateField(self):
>>> s = IDLicensePlatePrefixSelect() error_invalid = [u'Enter a valid vehicle license plate number']
>>> s.render('codes', 'BE') valid = {
u'<select name="codes">\n<option value="A">Banten</option>\n<option value="AA">Magelang</option>\n<option value="AB">Yogyakarta</option>\n<option value="AD">Surakarta - Solo</option>\n<option value="AE">Madiun</option>\n<option value="AG">Kediri</option>\n<option value="B">Jakarta</option>\n<option value="BA">Sumatera Barat</option>\n<option value="BB">Tapanuli</option>\n<option value="BD">Bengkulu</option>\n<option value="BE" selected="selected">Lampung</option>\n<option value="BG">Sumatera Selatan</option>\n<option value="BH">Jambi</option>\n<option value="BK">Sumatera Utara</option>\n<option value="BL">Nanggroe Aceh Darussalam</option>\n<option value="BM">Riau</option>\n<option value="BN">Kepulauan Bangka Belitung</option>\n<option value="BP">Kepulauan Riau</option>\n<option value="CC">Corps Consulate</option>\n<option value="CD">Corps Diplomatic</option>\n<option value="D">Bandung</option>\n<option value="DA">Kalimantan Selatan</option>\n<option value="DB">Sulawesi Utara Daratan</option>\n<option value="DC">Sulawesi Barat</option>\n<option value="DD">Sulawesi Selatan</option>\n<option value="DE">Maluku</option>\n<option value="DG">Maluku Utara</option>\n<option value="DH">NTT - Timor</option>\n<option value="DK">Bali</option>\n<option value="DL">Sulawesi Utara Kepulauan</option>\n<option value="DM">Gorontalo</option>\n<option value="DN">Sulawesi Tengah</option>\n<option value="DR">NTB - Lombok</option>\n<option value="DS">Papua dan Papua Barat</option>\n<option value="DT">Sulawesi Tenggara</option>\n<option value="E">Cirebon</option>\n<option value="EA">NTB - Sumbawa</option>\n<option value="EB">NTT - Flores</option>\n<option value="ED">NTT - Sumba</option>\n<option value="F">Bogor</option>\n<option value="G">Pekalongan</option>\n<option value="H">Semarang</option>\n<option value="K">Pati</option>\n<option value="KB">Kalimantan Barat</option>\n<option value="KH">Kalimantan Tengah</option>\n<option value="KT">Kalimantan Timur</option>\n<option value="L">Surabaya</option>\n<option value="M">Madura</option>\n<option value="N">Malang</option>\n<option value="P">Jember</option>\n<option value="R">Banyumas</option>\n<option value="RI">Federal Government</option>\n<option value="S">Bojonegoro</option>\n<option value="T">Purwakarta</option>\n<option value="W">Sidoarjo</option>\n<option value="Z">Garut</option>\n</select>' ' b 1234 ab ': u'B 1234 AB',
'B 1234 ABC': u'B 1234 ABC',
# IDLicensePlateField ####################################################################### 'A 12': u'A 12',
'DK 12345 12': u'DK 12345 12',
>>> from django.contrib.localflavor.id.forms import IDLicensePlateField 'RI 10': u'RI 10',
>>> f = IDLicensePlateField(required=False) 'CD 12 12': u'CD 12 12',
>>> f.clean('') }
u'' invalid = {
>>> f.clean(' b 1234 ab ') 'CD 10 12': error_invalid,
u'B 1234 AB' 'CD 1234 12': error_invalid,
>>> f.clean('B 1234 ABC') 'RI 10 AB': error_invalid,
u'B 1234 ABC' 'B 12345 01': error_invalid,
>>> f.clean('A 12') 'N 1234 12': error_invalid,
u'A 12' 'A 12 XYZ': error_invalid,
>>> f.clean('DK 12345 12') 'Q 1234 AB': error_invalid,
u'DK 12345 12' 'foo': error_invalid,
>>> f.clean('RI 10') }
u'RI 10' self.assertFieldOutput(IDLicensePlateField, valid, invalid)
>>> f.clean('CD 12 12')
u'CD 12 12'
>>> f.clean('CD 10 12')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('CD 1234 12')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('RI 10 AB')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('B 12345 01')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('N 1234 12')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('A 12 XYZ')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('Q 1234 AB')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid vehicle license plate number']
"""

View File

@ -1,9 +1,18 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils.unittest import TestCase from django.utils.unittest import TestCase
class LocalFlavorTestCase(TestCase): class LocalFlavorTestCase(TestCase):
# NOTE: These are copied from the TestCase Django uses for tests which
# access the database
def save_warnings_state(self):
self._warnings_state = get_warnings_state()
def restore_warnings_state(self):
restore_warnings_state(self._warnings_state)
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[], def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[],
field_kwargs={}, empty_value=u''): field_kwargs={}, empty_value=u''):
"""Asserts that a field behaves correctly with various inputs. """Asserts that a field behaves correctly with various inputs.

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from localflavor.cz import tests as localflavor_cz_tests from localflavor.cz import tests as localflavor_cz_tests
from localflavor.id import tests as localflavor_id_tests
from localflavor.ie import tests as localflavor_ie_tests from localflavor.ie import tests as localflavor_ie_tests
from localflavor.is_ import tests as localflavor_is_tests from localflavor.is_ import tests as localflavor_is_tests
from localflavor.it import tests as localflavor_it_tests from localflavor.it import tests as localflavor_it_tests
@ -30,13 +29,13 @@ from localflavor.es import ESLocalFlavorTests
from localflavor.fi import FILocalFlavorTests from localflavor.fi import FILocalFlavorTests
from localflavor.fr import FRLocalFlavorTests from localflavor.fr import FRLocalFlavorTests
from localflavor.generic import GenericLocalFlavorTests from localflavor.generic import GenericLocalFlavorTests
from localflavor.id import IDLocalFlavorTests
from localflavor.il import ILLocalFlavorTests from localflavor.il import ILLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests from localflavor.tr import TRLocalFlavorTests
__test__ = { __test__ = {
'localflavor_cz_tests': localflavor_cz_tests, 'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_id_tests': localflavor_id_tests,
'localflavor_ie_tests': localflavor_ie_tests, 'localflavor_ie_tests': localflavor_ie_tests,
'localflavor_is_tests': localflavor_is_tests, 'localflavor_is_tests': localflavor_is_tests,
'localflavor_it_tests': localflavor_it_tests, 'localflavor_it_tests': localflavor_it_tests,

View File

@ -26,6 +26,7 @@ from regressiontests.forms.localflavortests import (
FILocalFlavorTests, FILocalFlavorTests,
FRLocalFlavorTests, FRLocalFlavorTests,
GenericLocalFlavorTests, GenericLocalFlavorTests,
IDLocalFlavorTests,
ILLocalFlavorTests, ILLocalFlavorTests,
TRLocalFlavorTests, TRLocalFlavorTests,
) )