Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

test_models.py 8.4KB

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