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 -*-
# Tests for the contrib/localflavor/ ID form fields.
import warnings
tests = r"""
from django.contrib.localflavor.id.forms import (IDPhoneNumberField,
IDPostCodeField, IDNationalIdentityNumberField, IDLicensePlateField,
IDProvinceSelect, IDLicensePlatePrefixSelect)
# IDPhoneNumberField ########################################################
>>> import warnings
>>> warnings.filterwarnings("ignore", category=RuntimeWarning, module='django.contrib.localflavor.id.id_choices')
from utils import LocalFlavorTestCase
>>> 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
>>> f = IDPostCodeField(required=False)
>>> 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']
def tearDown(self):
self.restore_warnings_state()
# 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
>>> f = IDNationalIdentityNumberField(required=False)
>>> f.clean('')
u''
>>> f.clean(' 12.3456.010178 3456 ')
u'12.3456.010178.3456'
>>> f.clean('1234560101783456')
u'12.3456.010178.3456'
>>> f.clean('12.3456.010101.3456')
u'12.3456.010101.3456'
>>> f.clean('12.3456.310278.3456')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid NIK/KTP number']
>>> f.clean('00.0000.010101.0000')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid NIK/KTP number']
>>> f.clean('1234567890123456')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid NIK/KTP number']
>>> f.clean('foo')
Traceback (most recent call last):
...
ValidationError: [u'Enter a valid NIK/KTP number']
def test_IDLicensePlatePrefixSelect(self):
f = IDLicensePlatePrefixSelect()
out = u'''<select name="codes">
<option value="A">Banten</option>
<option value="AA">Magelang</option>
<option value="AB">Yogyakarta</option>
<option value="AD">Surakarta - Solo</option>
<option value="AE">Madiun</option>
<option value="AG">Kediri</option>
<option value="B">Jakarta</option>
<option value="BA">Sumatera Barat</option>
<option value="BB">Tapanuli</option>
<option value="BD">Bengkulu</option>
<option value="BE" selected="selected">Lampung</option>
<option value="BG">Sumatera Selatan</option>
<option value="BH">Jambi</option>
<option value="BK">Sumatera Utara</option>
<option value="BL">Nanggroe Aceh Darussalam</option>
<option value="BM">Riau</option>
<option value="BN">Kepulauan Bangka Belitung</option>
<option value="BP">Kepulauan Riau</option>
<option value="CC">Corps Consulate</option>
<option value="CD">Corps Diplomatic</option>
<option value="D">Bandung</option>
<option value="DA">Kalimantan Selatan</option>
<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
>>> s = IDProvinceSelect()
>>> s.render('provinces', 'LPG')
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>'
def test_IDPostCodeField(self):
error_invalid = [u'Enter a valid post code']
valid = {
'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
>>> s = IDLicensePlatePrefixSelect()
>>> s.render('codes', 'BE')
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>'
# IDLicensePlateField #######################################################################
>>> from django.contrib.localflavor.id.forms import IDLicensePlateField
>>> f = IDLicensePlateField(required=False)
>>> f.clean('')
u''
>>> f.clean(' b 1234 ab ')
u'B 1234 AB'
>>> f.clean('B 1234 ABC')
u'B 1234 ABC'
>>> f.clean('A 12')
u'A 12'
>>> f.clean('DK 12345 12')
u'DK 12345 12'
>>> f.clean('RI 10')
u'RI 10'
>>> 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']
"""
def test_IDLicensePlateField(self):
error_invalid = [u'Enter a valid vehicle license plate number']
valid = {
' b 1234 ab ': u'B 1234 AB',
'B 1234 ABC': u'B 1234 ABC',
'A 12': u'A 12',
'DK 12345 12': u'DK 12345 12',
'RI 10': u'RI 10',
'CD 12 12': u'CD 12 12',
}
invalid = {
'CD 10 12': error_invalid,
'CD 1234 12': error_invalid,
'RI 10 AB': error_invalid,
'B 12345 01': error_invalid,
'N 1234 12': error_invalid,
'A 12 XYZ': error_invalid,
'Q 1234 AB': error_invalid,
'foo': error_invalid,
}
self.assertFieldOutput(IDLicensePlateField, valid, invalid)

View File

@ -1,9 +1,18 @@
from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES
from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils.unittest import 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=[],
field_kwargs={}, empty_value=u''):
"""Asserts that a field behaves correctly with various inputs.

View File

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
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.is_ import tests as localflavor_is_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.fr import FRLocalFlavorTests
from localflavor.generic import GenericLocalFlavorTests
from localflavor.id import IDLocalFlavorTests
from localflavor.il import ILLocalFlavorTests
from localflavor.tr import TRLocalFlavorTests
__test__ = {
'localflavor_cz_tests': localflavor_cz_tests,
'localflavor_id_tests': localflavor_id_tests,
'localflavor_ie_tests': localflavor_ie_tests,
'localflavor_is_tests': localflavor_is_tests,
'localflavor_it_tests': localflavor_it_tests,

View File

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