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.

model_tests.py 6.3KB

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