Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

model_tests.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding: utf-8 -*-
  2. from django.test import TestCase
  3. from django.core import exceptions
  4. from nose.tools import raises
  5. from oscar.apps.order.models import ShippingAddress
  6. from oscar.core.compat import get_user_model
  7. from oscar.apps.address import models
  8. User = get_user_model()
  9. class TestUserAddress(TestCase):
  10. def setUp(self):
  11. self.country = models.Country(
  12. iso_3166_1_a2='GB', name="UNITED KINGDOM")
  13. def test_uses_title_firstname_and_lastname_in_salutation(self):
  14. a = models.UserAddress(
  15. title="Dr",
  16. first_name="Barry",
  17. last_name='Barrington')
  18. self.assertEquals("Dr Barry Barrington", a.salutation)
  19. def test_strips_whitespace_from_salutation(self):
  20. a = models.UserAddress(last_name='Barrington')
  21. self.assertEquals("Barrington", a.salutation)
  22. def test_has_name_property(self):
  23. a = models.UserAddress(
  24. title="Dr",
  25. first_name="Barry",
  26. last_name='Barrington')
  27. self.assertEquals("Barry Barrington", a.name)
  28. def test_has_summary_property(self):
  29. a = models.UserAddress(
  30. title="Dr",
  31. first_name="Barry",
  32. last_name='Barrington',
  33. line1="1 King Road",
  34. line4="London",
  35. postcode="SW1 9RE")
  36. self.assertEquals("Dr Barry Barrington, 1 King Road, London, SW1 9RE",
  37. a.summary)
  38. def test_summary_includes_country(self):
  39. c = models.Country(
  40. pk=1, iso_3166_1_a2="GB", name="UNITED KINGDOM")
  41. a = models.UserAddress(
  42. title="Dr",
  43. first_name="Barry",
  44. last_name='Barrington',
  45. line1="1 King Road",
  46. line4="London",
  47. postcode="SW1 9RE",
  48. country=c)
  49. self.assertEquals(
  50. "Dr Barry Barrington, 1 King Road, London, SW1 9RE, UNITED KINGDOM",
  51. a.summary)
  52. def test_can_be_hashed(self):
  53. a = models.UserAddress(
  54. title="Dr",
  55. first_name="Barry",
  56. last_name='Barrington',
  57. line1="1 King Road",
  58. line4="London",
  59. postcode="SW1 9RE")
  60. hash = a.generate_hash()
  61. self.assertTrue(hash is not None)
  62. def test_can_be_hashed_including_non_ascii(self):
  63. a = models.UserAddress(
  64. first_name=u"\u0141ukasz Smith",
  65. last_name=u'Smith',
  66. line1=u"75 Smith Road",
  67. postcode=u"n4 8ty")
  68. hash = a.generate_hash()
  69. self.assertTrue(hash is not None)
  70. def test_strips_whitespace_in_name_property(self):
  71. a = models.UserAddress(
  72. last_name='Barrington')
  73. self.assertEquals("Barrington", a.name)
  74. def test_uses_city_as_an_alias_of_line4(self):
  75. a = models.UserAddress(
  76. last_name='Barrington',
  77. line1="75 Smith Road",
  78. line4="London",
  79. postcode="n4 8ty")
  80. self.assertEqual('London', a.city)
  81. def test_converts_postcode_to_uppercase_when_cleaning(self):
  82. address = models.UserAddress(
  83. last_name='Barrington',
  84. line1="75 Smith Road",
  85. postcode="n4 8ty",
  86. country=self.country)
  87. address.clean()
  88. self.assertEquals("N4 8TY", address.postcode)
  89. def test_strips_whitespace_when_cleaning(self):
  90. a = models.UserAddress(
  91. last_name='Barrington',
  92. line1=" 75 Smith Road ",
  93. postcode=" n4 8ty",
  94. country=self.country)
  95. a.clean()
  96. self.assertEquals("N4 8TY", a.postcode)
  97. self.assertEquals("75 Smith Road", a.line1)
  98. def test_active_address_fields_skips_whitespace_only_fields(self):
  99. a = models.UserAddress(
  100. first_name=" ",
  101. last_name='Barrington',
  102. line1=" 75 Smith Road ",
  103. postcode=" n4 8ty",
  104. country=self.country)
  105. active_fields = a.active_address_fields()
  106. self.assertEquals("Barrington", active_fields[0])
  107. def test_ignores_whitespace_when_hashing(self):
  108. a1 = models.UserAddress(
  109. first_name=" Terry ",
  110. last_name='Barrington',
  111. line1=" 75 Smith Road ",
  112. postcode=" n4 8ty",
  113. country=self.country)
  114. a1.clean()
  115. a2 = models.UserAddress(
  116. first_name=" Terry",
  117. last_name=' Barrington',
  118. line1=" 75 Smith Road ",
  119. postcode="N4 8ty",
  120. country=self.country)
  121. a2.clean()
  122. self.assertEquals(a1.generate_hash(), a2.generate_hash())
  123. def test_populate_shipping_address_doesnt_set_id(self):
  124. a = models.UserAddress(
  125. first_name=" Terry ",
  126. last_name='Barrington',
  127. line1=" 75 Smith Road ",
  128. postcode=" n4 8ty",
  129. country=self.country)
  130. a.clean()
  131. sa = ShippingAddress()
  132. a.populate_alternative_model(sa)
  133. self.assertIsNone(sa.id)
  134. def test_populated_shipping_address_has_same_summary_user_address(self):
  135. a = models.UserAddress(
  136. first_name=" Terry ",
  137. last_name='Barrington',
  138. line1=" 75 Smith Road ",
  139. postcode=" n4 8ty",
  140. country=self.country)
  141. a.clean()
  142. sa = ShippingAddress()
  143. a.populate_alternative_model(sa)
  144. self.assertEquals(sa.summary, a.summary)
  145. def test_summary_is_property(self):
  146. a = models.UserAddress(
  147. first_name=" Terry ",
  148. last_name='Barrington',
  149. line1=" 75 Smith Road ",
  150. postcode=" n4 8ty",
  151. country=self.country)
  152. a.clean()
  153. self.assertEquals(
  154. u"Terry Barrington, 75 Smith Road, N4 8TY, UNITED KINGDOM",
  155. a.summary)
  156. VALID_POSTCODES = [
  157. ('GB', 'N1 9RT'),
  158. ('SK', '991 41'),
  159. ('CZ', '612 00'),
  160. ('CC', '6799'),
  161. ('CY', '8240'),
  162. ('MC', '98000'),
  163. ('SH', 'STHL 1ZZ'),
  164. ('JP', '150-2345'),
  165. ('PG', '314'),
  166. ('HN', '41202'),
  167. # It works for small cases as well
  168. ('GB', 'sw2 1rw'),
  169. ]
  170. INVALID_POSTCODES = [
  171. ('GB', 'not-a-postcode'),
  172. ('DE', '123b4'),
  173. ]
  174. def assert_valid_postcode(country_value, postcode_value):
  175. country = models.Country(iso_3166_1_a2=country_value)
  176. address = models.UserAddress(country=country, postcode=postcode_value)
  177. address.clean()
  178. @raises(exceptions.ValidationError)
  179. def assert_invalid_postcode(country_value, postcode_value):
  180. country = models.Country(iso_3166_1_a2=country_value)
  181. address = models.UserAddress(country=country, postcode=postcode_value)
  182. address.clean()
  183. def test_postcode_is_validated_for_country():
  184. for country, postcode in VALID_POSTCODES:
  185. yield assert_valid_postcode, country, postcode
  186. def test_postcode_is_only_valid():
  187. for country, postcode in INVALID_POSTCODES:
  188. yield assert_invalid_postcode, country, postcode