/tests/regressiontests/forms/localflavor/se.py
Python | 165 lines | 156 code | 6 blank | 3 comment | 2 complexity | b708c6ce4b1973fec9f4c2580441c45f MD5 | raw file
Possible License(s): BSD-3-Clause
1# -*- coding: utf-8 -*- 2from django.contrib.localflavor.se.forms import (SECountySelect, 3 SEOrganisationNumberField, SEPersonalIdentityNumberField, 4 SEPostalCodeField) 5import datetime 6 7from utils import LocalFlavorTestCase 8 9 10class SELocalFlavorTests(LocalFlavorTestCase): 11 12 def setUp(self): 13 # Mocking datetime.date to make sure 14 # localflavor.se.utils.validate_id_birthday works 15 class MockDate(datetime.date): 16 def today(cls): 17 return datetime.date(2008, 5, 14) 18 today = classmethod(today) 19 self._olddate = datetime.date 20 datetime.date = MockDate 21 22 def tearDown(self): 23 datetime.date = self._olddate 24 25 def test_SECountySelect(self): 26 f = SECountySelect() 27 out = u'''<select name="swedish_county"> 28<option value="AB">Stockholm</option> 29<option value="AC">V\xe4sterbotten</option> 30<option value="BD">Norrbotten</option> 31<option value="C">Uppsala</option> 32<option value="D">S\xf6dermanland</option> 33<option value="E" selected="selected">\xd6sterg\xf6tland</option> 34<option value="F">J\xf6nk\xf6ping</option> 35<option value="G">Kronoberg</option> 36<option value="H">Kalmar</option> 37<option value="I">Gotland</option> 38<option value="K">Blekinge</option> 39<option value="M">Sk\xe5ne</option> 40<option value="N">Halland</option> 41<option value="O">V\xe4stra G\xf6taland</option> 42<option value="S">V\xe4rmland</option> 43<option value="T">\xd6rebro</option> 44<option value="U">V\xe4stmanland</option> 45<option value="W">Dalarna</option> 46<option value="X">G\xe4vleborg</option> 47<option value="Y">V\xe4sternorrland</option> 48<option value="Z">J\xe4mtland</option> 49</select>''' 50 self.assertEqual(f.render('swedish_county', 'E'), out) 51 52 def test_SEOrganizationNumberField(self): 53 error_invalid = [u'Enter a valid Swedish organisation number.'] 54 valid = { 55 '870512-1989': '198705121989', 56 '19870512-1989': '198705121989', 57 '870512-2128': '198705122128', 58 '081015-6315': '190810156315', 59 '081015+6315': '180810156315', 60 '0810156315': '190810156315', 61 # Test some different organisation numbers 62 # IKEA Link??ping 63 '556074-7569': '5560747569', 64 # Volvo Personvagnar 65 '556074-3089': '5560743089', 66 # LJS (organisation) 67 '822001-5476': '8220015476', 68 # LJS (organisation) 69 '8220015476': '8220015476', 70 # Katedralskolan Link??ping (school) 71 '2120000449': '2120000449', 72 # Faux organisation number, which tests that the checksum can be 0 73 '232518-5060': '2325185060', 74 } 75 invalid = { 76 # Ordinary personal identity numbers for sole proprietors 77 # The same rules as for SEPersonalIdentityField applies here 78 '081015 6315': error_invalid, 79 '950231-4496': error_invalid, 80 '6914104499': error_invalid, 81 '950d314496': error_invalid, 82 'invalid!!!': error_invalid, 83 '870514-1111': error_invalid, 84 # Co-ordination number checking 85 # Co-ordination numbers are not valid organisation numbers 86 '870574-1315': error_invalid, 87 '870573-1311': error_invalid, 88 # Volvo Personvagnar, bad format 89 '556074+3089': error_invalid, 90 # Invalid checksum 91 '2120000441': error_invalid, 92 # Valid checksum but invalid organisation type 93 '1120000441': error_invalid, 94 } 95 self.assertFieldOutput(SEOrganisationNumberField, valid, invalid) 96 97 def test_SEPersonalIdentityNumberField(self): 98 error_invalid = [u'Enter a valid Swedish personal identity number.'] 99 error_coord = [u'Co-ordination numbers are not allowed.'] 100 valid = { 101 '870512-1989': '198705121989', 102 '870512-2128': '198705122128', 103 '19870512-1989': '198705121989', 104 '198705121989': '198705121989', 105 '081015-6315': '190810156315', 106 '0810156315': '190810156315', 107 # This is a "special-case" in the checksum calculation, 108 # where the sum is divisible by 10 (the checksum digit == 0) 109 '8705141060': '198705141060', 110 # + means that the person is older than 100 years 111 '081015+6315': '180810156315', 112 # Co-ordination number checking 113 '870574-1315': '198705741315', 114 '870574+1315': '188705741315', 115 '198705741315': '198705741315', 116 } 117 invalid = { 118 '081015 6315': error_invalid, 119 '950d314496': error_invalid, 120 'invalid!!!': error_invalid, 121 # Invalid dates 122 # February 31st does not exist 123 '950231-4496': error_invalid, 124 # Month 14 does not exist 125 '6914104499': error_invalid, 126 # There are no Swedish personal id numbers where year < 1800 127 '17430309-7135': error_invalid, 128 # Invalid checksum 129 '870514-1111': error_invalid, 130 # Co-ordination number with bad checksum 131 '870573-1311': error_invalid, 132 } 133 self.assertFieldOutput(SEPersonalIdentityNumberField, valid, invalid) 134 135 valid = {} 136 invalid = { 137 # Check valid co-ordination numbers that should not be accepted 138 # because of coordination_number=False 139 '870574-1315': error_coord, 140 '870574+1315': error_coord, 141 '8705741315': error_coord, 142 # Invalid co-ordination numbers should be treated as invalid, and not 143 # as co-ordination numbers 144 '870573-1311': error_invalid, 145 } 146 kwargs = {'coordination_number': False,} 147 self.assertFieldOutput(SEPersonalIdentityNumberField, valid, invalid, 148 field_kwargs=kwargs) 149 150 def test_SEPostalCodeField(self): 151 error_format = [u'Enter a Swedish postal code in the format XXXXX.'] 152 valid = { 153 '589 37': '58937', 154 '58937': '58937', 155 } 156 invalid = { 157 'abcasfassadf': error_format, 158 # Only one space is allowed for separation 159 '589 37': error_format, 160 # The postal code must not start with 0 161 '01234': error_format, 162 163 } 164 self.assertFieldOutput(SEPostalCodeField, valid, invalid) 165