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.

shipping_tests.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from django.contrib.auth.models import User
  4. from oscar.apps.shipping.methods import Free, FixedPrice
  5. from oscar.apps.shipping.models import OrderAndItemCharges, WeightBased
  6. from oscar.apps.shipping.repository import Repository
  7. from oscar.apps.shipping import Scales
  8. from oscar.apps.basket.models import Basket
  9. from oscar.test.helpers import create_product
  10. from oscar.test.decorators import dataProvider
  11. class FreeTest(TestCase):
  12. def setUp(self):
  13. self.method = Free()
  14. def test_shipping_is_free_for_empty_basket(self):
  15. basket = Basket()
  16. self.method.set_basket(basket)
  17. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  18. self.assertEquals(D('0.00'), self.method.basket_charge_excl_tax())
  19. def test_shipping_is_free_for_nonempty_basket(self):
  20. basket = Basket()
  21. basket.add_product(create_product())
  22. self.method.set_basket(basket)
  23. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  24. self.assertEquals(D('0.00'), self.method.basket_charge_excl_tax())
  25. class FixedPriceTest(TestCase):
  26. def test_fixed_price_shipping_charges_for_empty_basket(self):
  27. method = FixedPrice(D('10.00'), D('10.00'))
  28. basket = Basket()
  29. method.set_basket(basket)
  30. self.assertEquals(D('10.00'), method.basket_charge_incl_tax())
  31. self.assertEquals(D('10.00'), method.basket_charge_excl_tax())
  32. def test_fixed_price_shipping_assumes_no_tax(self):
  33. method = FixedPrice(D('10.00'))
  34. basket = Basket()
  35. method.set_basket(basket)
  36. self.assertEquals(D('10.00'), method.basket_charge_excl_tax())
  37. shipping_values = lambda: [('1.00',),
  38. ('5.00',),
  39. ('10.00',),
  40. ('12.00',)]
  41. @dataProvider(shipping_values)
  42. def test_different_values(self, value):
  43. method = FixedPrice(D(value))
  44. basket = Basket()
  45. method.set_basket(basket)
  46. self.assertEquals(D(value), method.basket_charge_excl_tax())
  47. class OrderAndItemChargesTests(TestCase):
  48. def setUp(self):
  49. self.method = OrderAndItemCharges(price_per_order=D('5.00'), price_per_item=D('1.00'))
  50. self.basket = Basket.objects.create()
  51. self.method.set_basket(self.basket)
  52. def test_order_level_charge_for_empty_basket(self):
  53. self.assertEquals(D('5.00'), self.method.basket_charge_incl_tax())
  54. def test_single_item_basket(self):
  55. p = create_product()
  56. self.basket.add_product(p)
  57. self.assertEquals(D('5.00') + D('1.00'), self.method.basket_charge_incl_tax())
  58. def test_multi_item_basket(self):
  59. p = create_product()
  60. self.basket.add_product(p, 7)
  61. self.assertEquals(D('5.00') + 7*D('1.00'), self.method.basket_charge_incl_tax())
  62. class ZeroFreeThresholdTest(TestCase):
  63. def setUp(self):
  64. self.method = OrderAndItemCharges(price_per_order=D('10.00'), free_shipping_threshold=D('0.00'))
  65. self.basket = Basket.objects.create()
  66. self.method.set_basket(self.basket)
  67. def test_free_shipping_with_empty_basket(self):
  68. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  69. def test_free_shipping_with_nonempty_basket(self):
  70. p = create_product(D('5.00'))
  71. self.basket.add_product(p)
  72. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  73. class NonZeroFreeThresholdTest(TestCase):
  74. def setUp(self):
  75. self.method = OrderAndItemCharges(price_per_order=D('10.00'), free_shipping_threshold=D('20.00'))
  76. self.basket = Basket.objects.create()
  77. self.method.set_basket(self.basket)
  78. def test_basket_below_threshold(self):
  79. p = create_product(D('5.00'))
  80. self.basket.add_product(p)
  81. self.assertEquals(D('10.00'), self.method.basket_charge_incl_tax())
  82. def test_basket_on_threshold(self):
  83. p = create_product(D('5.00'))
  84. self.basket.add_product(p, 4)
  85. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  86. def test_basket_above_threshold(self):
  87. p = create_product(D('5.00'))
  88. self.basket.add_product(p, 8)
  89. self.assertEquals(D('0.00'), self.method.basket_charge_incl_tax())
  90. class ScalesTests(TestCase):
  91. def test_simple_weight_calculation(self):
  92. scales = Scales(attribute_code='weight')
  93. p = create_product(attributes={'weight': 1})
  94. self.assertEqual(1, scales.weigh_product(p))
  95. def test_default_weight_is_used_when_attribute_is_missing(self):
  96. scales = Scales(attribute_code='weight', default_weight=0.5)
  97. p = create_product()
  98. self.assertEqual(0.5, scales.weigh_product(p))
  99. def test_exception_is_raised_when_attribute_is_missing(self):
  100. scales = Scales(attribute_code='weight')
  101. p = create_product()
  102. with self.assertRaises(ValueError):
  103. scales.weigh_product(p)
  104. def test_weight_calculation_of_empty_basket(self):
  105. basket = Basket()
  106. scales = Scales(attribute_code='weight')
  107. self.assertEquals(0, scales.weigh_basket(basket))
  108. def test_weight_calculation_of_basket(self):
  109. basket = Basket()
  110. basket.add_product(create_product(attributes={'weight': 1}))
  111. basket.add_product(create_product(attributes={'weight': 2}))
  112. scales = Scales(attribute_code='weight')
  113. self.assertEquals(1+2, scales.weigh_basket(basket))
  114. class WeightBasedMethodTests(TestCase):
  115. def setUp(self):
  116. self.standard = WeightBased.objects.create(name='Standard')
  117. self.express = WeightBased.objects.create(name='Express')
  118. def tearDown(self):
  119. self.standard.delete()
  120. self.express.delete()
  121. def test_get_band_for_lower_weight(self):
  122. band = self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  123. fetched_band = self.standard.get_band_for_weight(0.5)
  124. self.assertEqual(band.id, fetched_band.id)
  125. def test_get_band_for_higher_weight(self):
  126. band = self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  127. fetched_band = self.standard.get_band_for_weight(1.5)
  128. self.assertIsNone(fetched_band)
  129. def test_get_band_for_matching_weight(self):
  130. band = self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  131. fetched_band = self.standard.get_band_for_weight(1)
  132. self.assertEqual(band.id, fetched_band.id)
  133. def test_weight_to_is_upper_bound(self):
  134. band = self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  135. self.assertEqual(1, band.weight_to)
  136. def test_weight_from_for_single_band(self):
  137. band = self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  138. self.assertEqual(0, band.weight_from)
  139. def test_weight_from_for_multiple_bands(self):
  140. self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  141. band = self.standard.objects.create(upper_limit=2, charge=D('8.00'))
  142. self.assertEqual(1, band.weight_from)
  143. def test_weight_from_for_multiple_bands(self):
  144. self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  145. band = self.express.bands.create(upper_limit=2, charge=D('8.00'))
  146. self.assertEqual(0, band.weight_from)
  147. def test_get_band_for_series_of_bands(self):
  148. self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  149. self.standard.bands.create(upper_limit=2, charge=D('8.00'))
  150. self.standard.bands.create(upper_limit=3, charge=D('12.00'))
  151. self.assertEqual(D('4.00'), self.standard.get_band_for_weight(0.5).charge)
  152. self.assertEqual(D('8.00'), self.standard.get_band_for_weight(1.5).charge)
  153. self.assertEqual(D('12.00'), self.standard.get_band_for_weight(2.5).charge)
  154. def test_get_band_for_series_of_bands_from_different_methods(self):
  155. self.express.bands.create(upper_limit=2, charge=D('8.00'))
  156. self.standard.bands.create(upper_limit=1, charge=D('4.00'))
  157. self.standard.bands.create(upper_limit=3, charge=D('12.00'))
  158. self.assertEqual(D('12.00'), self.standard.get_band_for_weight(2.5).charge)
  159. def test_for_smoke_with_basket_charge(self):
  160. basket = Basket()
  161. self.standard.set_basket(basket)
  162. charge = self.standard.basket_charge_incl_tax()
  163. self.assertEqual(D('0.00'), charge)
  164. class RepositoryTests(TestCase):
  165. def setUp(self):
  166. self.repo = Repository()
  167. def test_default_method_is_free(self):
  168. user, basket = User(), Basket()
  169. methods = self.repo.get_shipping_methods(user, basket)
  170. self.assertEqual(1, len(methods))
  171. self.assertTrue(isinstance(methods[0], Free))