From 55c81c36e64b74342b60af67fb8712856f504d4c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 18 Dec 2010 20:31:00 +0000 Subject: [PATCH] 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 --- tests/regressiontests/forms/localflavor/id.py | 349 +++++++++--------- .../forms/localflavor/utils.py | 9 + .../regressiontests/forms/localflavortests.py | 3 +- tests/regressiontests/forms/tests/__init__.py | 1 + 4 files changed, 194 insertions(+), 168 deletions(-) diff --git a/tests/regressiontests/forms/localflavor/id.py b/tests/regressiontests/forms/localflavor/id.py index 247303aa36..2e8c51098d 100644 --- a/tests/regressiontests/forms/localflavor/id.py +++ b/tests/regressiontests/forms/localflavor/id.py @@ -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'''''' + 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'''''' + 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'' + 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'' - -# 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) diff --git a/tests/regressiontests/forms/localflavor/utils.py b/tests/regressiontests/forms/localflavor/utils.py index df1c3c2de3..790529c8b5 100644 --- a/tests/regressiontests/forms/localflavor/utils.py +++ b/tests/regressiontests/forms/localflavor/utils.py @@ -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. diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 8e4f65ce32..264efadc1e 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -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, diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index a23290ee2a..190577c9f6 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -26,6 +26,7 @@ from regressiontests.forms.localflavortests import ( FILocalFlavorTests, FRLocalFlavorTests, GenericLocalFlavorTests, + IDLocalFlavorTests, ILLocalFlavorTests, TRLocalFlavorTests, )