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

model_tests.py 6.9KB

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