You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_models.py 7.1KB

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