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.

stock_alert_tests.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from oscar.test.factories import create_product, create_stockrecord
  4. from oscar.apps.basket.models import Basket
  5. from oscar.apps.partner.models import StockRecord, StockAlert
  6. from oscar.apps.order.utils import OrderCreator
  7. from oscar.test.basket import add_product
  8. from oscar.test import factories
  9. class TestPlacingAnOrder(TestCase):
  10. def setUp(self):
  11. self.product = create_product()
  12. self.stockrecord = create_stockrecord(self.product, D('12.00'),
  13. num_in_stock=5)
  14. self.basket = factories.create_basket(empty=True)
  15. add_product(self.basket, product=self.product)
  16. def set_threshold(self, threshold):
  17. self.stockrecord.low_stock_threshold = threshold
  18. self.stockrecord.save()
  19. def test_correctly_allocates_stock(self):
  20. OrderCreator().place_order(self.basket)
  21. record = StockRecord.objects.get(product=self.product)
  22. self.assertEqual(5, record.num_in_stock)
  23. self.assertEqual(1, record.num_allocated)
  24. self.assertEqual(4, record.net_stock_level)
  25. def test_does_not_raise_an_alert_if_threshold_not_broken(self):
  26. self.set_threshold(4)
  27. OrderCreator().place_order(self.basket)
  28. alerts = StockAlert.objects.all()
  29. self.assertEqual(0, len(alerts))
  30. def test_raises_alert_when_threshold_is_reached(self):
  31. self.set_threshold(5)
  32. OrderCreator().place_order(self.basket)
  33. alerts = StockAlert.objects.filter(
  34. stockrecord=self.stockrecord)
  35. self.assertEqual(1, len(alerts))
  36. def test_only_raises_an_alert_once(self):
  37. self.set_threshold(5)
  38. StockAlert.objects.create(stockrecord=self.stockrecord,
  39. threshold=10)
  40. OrderCreator().place_order(self.basket)
  41. alerts = StockAlert.objects.filter(
  42. stockrecord=self.stockrecord)
  43. self.assertEqual(1, len(alerts))
  44. class TestRestockingProduct(TestCase):
  45. def setUp(self):
  46. self.product = create_product()
  47. self.stockrecord = create_stockrecord(self.product, D('12.00'),
  48. num_in_stock=5)
  49. self.basket = factories.create_basket(empty=True)
  50. add_product(self.basket, product=self.product)
  51. def set_threshold(self, threshold):
  52. self.stockrecord.low_stock_threshold = threshold
  53. self.stockrecord.save()
  54. def test_closes_open_alert(self):
  55. self.set_threshold(5)
  56. OrderCreator().place_order(self.basket)
  57. alert = StockAlert.objects.get(stockrecord=self.stockrecord)
  58. self.assertEqual(StockAlert.OPEN, alert.status)
  59. # Restock product
  60. self.stockrecord.num_in_stock = 15
  61. self.stockrecord.save()
  62. alert = StockAlert.objects.get(stockrecord=self.stockrecord)
  63. self.assertEqual(StockAlert.CLOSED, alert.status)