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

model_tests.py 7.0KB

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