/tests/regressiontests/forms/localflavor/ca.py

https://code.google.com/p/mango-py/ · Python · 108 lines · 97 code · 11 blank · 0 comment · 0 complexity · 8875d1b75260bcae41b5200148ec9fc0 MD5 · raw file

  1. import warnings
  2. from django.contrib.localflavor.ca.forms import (CAPostalCodeField,
  3. CAPhoneNumberField, CAProvinceField, CAProvinceSelect,
  4. CASocialInsuranceNumberField)
  5. from utils import LocalFlavorTestCase
  6. class CALocalFlavorTests(LocalFlavorTestCase):
  7. def setUp(self):
  8. self.save_warnings_state()
  9. warnings.filterwarnings(
  10. "ignore",
  11. category=RuntimeWarning,
  12. module='django.contrib.localflavor.ca.ca_provinces'
  13. )
  14. def tearDown(self):
  15. self.restore_warnings_state()
  16. def test_CAProvinceSelect(self):
  17. f = CAProvinceSelect()
  18. out = u'''<select name="province">
  19. <option value="AB" selected="selected">Alberta</option>
  20. <option value="BC">British Columbia</option>
  21. <option value="MB">Manitoba</option>
  22. <option value="NB">New Brunswick</option>
  23. <option value="NL">Newfoundland and Labrador</option>
  24. <option value="NT">Northwest Territories</option>
  25. <option value="NS">Nova Scotia</option>
  26. <option value="NU">Nunavut</option>
  27. <option value="ON">Ontario</option>
  28. <option value="PE">Prince Edward Island</option>
  29. <option value="QC">Quebec</option>
  30. <option value="SK">Saskatchewan</option>
  31. <option value="YT">Yukon</option>
  32. </select>'''
  33. self.assertEqual(f.render('province', 'AB'), out)
  34. def test_CAPostalCodeField(self):
  35. error_format = [u'Enter a postal code in the format XXX XXX.']
  36. valid = {
  37. 'T2S 2H7': 'T2S 2H7',
  38. 'T2S 2W7': 'T2S 2W7',
  39. 'T2S 2Z7': 'T2S 2Z7',
  40. 'T2Z 2H7': 'T2Z 2H7',
  41. }
  42. invalid = {
  43. 'T2S2H7' : error_format,
  44. 'T2S 2H' : error_format,
  45. '2T6 H8I': error_format,
  46. 'T2S2H' : error_format,
  47. 90210 : error_format,
  48. 'W2S 2H3': error_format,
  49. 'Z2S 2H3': error_format,
  50. 'F2S 2H3': error_format,
  51. 'A2S 2D3': error_format,
  52. 'A2I 2R3': error_format,
  53. 'A2Q 2R3': error_format,
  54. 'U2B 2R3': error_format,
  55. 'O2B 2R3': error_format,
  56. }
  57. self.assertFieldOutput(CAPostalCodeField, valid, invalid)
  58. def test_CAPhoneNumberField(self):
  59. error_format = [u'Phone numbers must be in XXX-XXX-XXXX format.']
  60. valid = {
  61. '403-555-1212': '403-555-1212',
  62. '4035551212': '403-555-1212',
  63. '403 555-1212': '403-555-1212',
  64. '(403) 555-1212': '403-555-1212',
  65. '403 555 1212': '403-555-1212',
  66. '403.555.1212': '403-555-1212',
  67. '403.555-1212': '403-555-1212',
  68. ' (403) 555.1212 ': '403-555-1212',
  69. }
  70. invalid = {
  71. '555-1212': error_format,
  72. '403-55-1212': error_format,
  73. }
  74. self.assertFieldOutput(CAPhoneNumberField, valid, invalid)
  75. def test_CAProvinceField(self):
  76. error_format = [u'Enter a Canadian province or territory.']
  77. valid = {
  78. 'ab': 'AB',
  79. 'BC': 'BC',
  80. 'nova scotia': 'NS',
  81. ' manitoba ': 'MB',
  82. }
  83. invalid = {
  84. 'T2S 2H7': error_format,
  85. }
  86. self.assertFieldOutput(CAProvinceField, valid, invalid)
  87. def test_CASocialInsuranceField(self):
  88. error_format = [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXX format.']
  89. valid = {
  90. '046-454-286': '046-454-286',
  91. }
  92. invalid = {
  93. '046-454-287': error_format,
  94. '046 454 286': error_format,
  95. '046-44-286': error_format,
  96. }
  97. self.assertFieldOutput(CASocialInsuranceNumberField, valid, invalid)