/tests/regressiontests/forms/localflavor/se.py

https://code.google.com/p/mango-py/ · Python · 165 lines · 120 code · 12 blank · 33 comment · 0 complexity · b708c6ce4b1973fec9f4c2580441c45f MD5 · raw file

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