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

test_strategy.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from django.test import TestCase
  2. from decimal import Decimal as D
  3. from oscar.apps.partner import strategy
  4. from oscar.apps.catalogue import models
  5. from oscar.test import factories
  6. from oscar.apps.basket.models import Line
  7. class ProductWithEmptyPriceTestMixin(object):
  8. def test_product_with_empty_price(self):
  9. product_class = factories.ProductClassFactory(track_stock=False)
  10. product = factories.ProductFactory(product_class=product_class, stockrecords=[])
  11. factories.StockRecordFactory(price_excl_tax=None, product=product)
  12. info = self.strategy.fetch_for_product(product)
  13. self.assertFalse(info.availability.is_available_to_buy)
  14. class TestDefaultStrategy(ProductWithEmptyPriceTestMixin, TestCase):
  15. def setUp(self):
  16. self.strategy = strategy.Default()
  17. def test_no_stockrecords(self):
  18. product = factories.create_product()
  19. info = self.strategy.fetch_for_product(product)
  20. self.assertFalse(info.availability.is_available_to_buy)
  21. self.assertIsNone(info.price.incl_tax)
  22. def test_one_stockrecord(self):
  23. product = factories.create_product(price=D('1.99'), num_in_stock=4)
  24. info = self.strategy.fetch_for_product(product)
  25. self.assertTrue(info.availability.is_available_to_buy)
  26. self.assertEqual(D('1.99'), info.price.excl_tax)
  27. self.assertEqual(D('1.99'), info.price.incl_tax)
  28. def test_product_which_doesnt_track_stock(self):
  29. product_class = models.ProductClass.objects.create(
  30. name="Digital", track_stock=False)
  31. product = factories.create_product(
  32. product_class=product_class,
  33. price=D('1.99'), num_in_stock=None)
  34. info = self.strategy.fetch_for_product(product)
  35. self.assertTrue(info.availability.is_available_to_buy)
  36. def test_line_method_is_same_as_product_one(self):
  37. product = factories.create_product()
  38. line = Line(product=product)
  39. info = self.strategy.fetch_for_line(line)
  40. self.assertFalse(info.availability.is_available_to_buy)
  41. self.assertIsNone(info.price.incl_tax)
  42. def test_free_product_is_available_to_buy(self):
  43. product = factories.create_product(price=D('0'), num_in_stock=1)
  44. info = self.strategy.fetch_for_product(product)
  45. self.assertTrue(info.availability.is_available_to_buy)
  46. self.assertTrue(info.price.exists)
  47. def test_product_with_empty_price(self):
  48. product_class = factories.ProductClassFactory(track_stock=False)
  49. product = factories.ProductFactory(product_class=product_class, stockrecords=[])
  50. factories.StockRecordFactory(price_excl_tax=None, product=product)
  51. info = self.strategy.fetch_for_product(product)
  52. self.assertFalse(info.availability.is_available_to_buy)
  53. class TestDefaultStrategyForParentProductWhoseVariantsHaveNoStockRecords(TestCase):
  54. def setUp(self):
  55. self.strategy = strategy.Default()
  56. parent = factories.create_product(structure='parent')
  57. for x in range(3):
  58. factories.create_product(parent=parent)
  59. self.info = self.strategy.fetch_for_parent(parent)
  60. def test_specifies_product_is_unavailable(self):
  61. self.assertFalse(self.info.availability.is_available_to_buy)
  62. def test_specifies_correct_availability_code(self):
  63. self.assertEqual('unavailable', self.info.availability.code)
  64. def test_specifies_product_has_no_price(self):
  65. self.assertFalse(self.info.price.exists)
  66. class TestDefaultStrategyForParentProductWithInStockVariant(TestCase):
  67. def setUp(self):
  68. self.strategy = strategy.Default()
  69. parent = factories.create_product(structure='parent')
  70. factories.create_product(parent=parent, price=D('10.00'),
  71. num_in_stock=3)
  72. for x in range(2):
  73. factories.create_product(parent=parent)
  74. self.info = self.strategy.fetch_for_parent(parent)
  75. def test_specifies_product_is_available(self):
  76. self.assertTrue(self.info.availability.is_available_to_buy)
  77. def test_specifies_correct_availability_code(self):
  78. self.assertEqual('available', self.info.availability.code)
  79. def test_specifies_product_has_correct_price(self):
  80. self.assertEqual(D('10.00'), self.info.price.incl_tax)
  81. class TestDefaultStrategyForParentProductWithOutOfStockVariant(TestCase):
  82. def setUp(self):
  83. self.strategy = strategy.Default()
  84. parent = factories.create_product(structure='parent')
  85. factories.create_product(
  86. parent=parent, price=D('10.00'), num_in_stock=0)
  87. for x in range(2):
  88. factories.create_product(parent=parent)
  89. self.info = self.strategy.fetch_for_parent(parent)
  90. def test_specifies_product_is_unavailable(self):
  91. self.assertFalse(self.info.availability.is_available_to_buy)
  92. def test_specifies_correct_availability_code(self):
  93. self.assertEqual('unavailable', self.info.availability.code)
  94. def test_specifies_product_has_correct_price(self):
  95. self.assertEqual(D('10.00'), self.info.price.incl_tax)
  96. class TestFixedRateTax(ProductWithEmptyPriceTestMixin, TestCase):
  97. def setUp(self):
  98. self.strategy = strategy.UK()
  99. class TestDeferredTax(ProductWithEmptyPriceTestMixin, TestCase):
  100. def setUp(self):
  101. self.strategy = strategy.US()