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

test_models.py 8.4KB

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