# Tests for contrib/localflavor/ CN Form Fields from django.contrib.localflavor.cn.forms import (CNProvinceSelect, CNPostCodeField, CNIDCardField, CNPhoneNumberField, CNCellNumberField) from utils import LocalFlavorTestCase class CNLocalFlavorTests(LocalFlavorTestCase): def test_CNProvinceSelect(self): f = CNProvinceSelect() correct_output = u'''''' self.assertEqual(f.render('provinces', 'hubei'), correct_output) def test_CNPostCodeField(self): error_format = [u'Enter a post code in the format XXXXXX.'] valid = { '091209': u'091209' } invalid = { '09120': error_format, '09120916': error_format } self.assertFieldOutput(CNPostCodeField, valid, invalid) def test_CNIDCardField(self): valid = { # A valid 1st generation ID Card Number. '110101491001001': u'110101491001001', # A valid 2nd generation ID Card number. '11010119491001001X': u'11010119491001001X', # Another valid 2nd gen ID Number with a case change '11010119491001001x': u'11010119491001001X' } wrong_format = [u'ID Card Number consists of 15 or 18 digits.'] wrong_location = [u'Invalid ID Card Number: Wrong location code'] wrong_bday = [u'Invalid ID Card Number: Wrong birthdate'] wrong_checksum = [u'Invalid ID Card Number: Wrong checksum'] invalid = { 'abcdefghijklmnop': wrong_format, '1010101010101010': wrong_format, '010101491001001' : wrong_location, # 1st gen, 01 is invalid '110101491041001' : wrong_bday, # 1st gen. There wasn't day 41 '92010119491001001X': wrong_location, # 2nd gen, 92 is invalid '91010119491301001X': wrong_bday, # 2nd gen, 19491301 is invalid date '910101194910010014': wrong_checksum #2nd gen } self.assertFieldOutput(CNIDCardField, valid, invalid) def test_CNPhoneNumberField(self): error_format = [u'Enter a valid phone number.'] valid = { '010-12345678': u'010-12345678', '010-1234567': u'010-1234567', '0101-12345678': u'0101-12345678', '0101-1234567': u'0101-1234567', '010-12345678-020':u'010-12345678-020' } invalid = { '01x-12345678': error_format, '12345678': error_format, '01123-12345678': error_format, '010-123456789': error_format, '010-12345678-': error_format } self.assertFieldOutput(CNPhoneNumberField, valid, invalid) def test_CNCellNumberField(self): error_format = [u'Enter a valid cell number.'] valid = { '13012345678': u'13012345678', } invalid = { '130123456789': error_format, '14012345678': error_format } self.assertFieldOutput(CNCellNumberField, valid, invalid)