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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from oscar.test.helpers import create_product
  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. # These aren't really unit tests - move them?
  8. class TestPlacingAnOrder(TestCase):
  9. def setUp(self):
  10. self.product = create_product(price=D('12.00'), num_in_stock=5)
  11. self.basket = Basket()
  12. self.basket.add_product(self.product)
  13. def set_threshold(self, threshold):
  14. self.product.stockrecord.low_stock_threshold = threshold
  15. self.product.stockrecord.save()
  16. def test_correctly_allocates_stock(self):
  17. OrderCreator().place_order(self.basket)
  18. record = StockRecord.objects.get(product=self.product)
  19. self.assertEqual(5, record.num_in_stock)
  20. self.assertEqual(1, record.num_allocated)
  21. self.assertEqual(4, record.net_stock_level)
  22. def test_does_not_raise_an_alert_if_threshold_not_broken(self):
  23. self.set_threshold(4)
  24. OrderCreator().place_order(self.basket)
  25. alerts = StockAlert.objects.all()
  26. self.assertEqual(0, len(alerts))
  27. def test_raises_alert_when_threshold_is_reached(self):
  28. self.set_threshold(5)
  29. OrderCreator().place_order(self.basket)
  30. alerts = StockAlert.objects.filter(
  31. stockrecord=self.product.stockrecord)
  32. self.assertEqual(1, len(alerts))
  33. def test_only_raises_an_alert_once(self):
  34. self.set_threshold(5)
  35. StockAlert.objects.create(stockrecord=self.product.stockrecord,
  36. threshold=10)
  37. OrderCreator().place_order(self.basket)
  38. alerts = StockAlert.objects.filter(
  39. stockrecord=self.product.stockrecord)
  40. self.assertEqual(1, len(alerts))
  41. class TestRestockingProduct(TestCase):
  42. def setUp(self):
  43. self.product = create_product(price=D('12.00'), num_in_stock=5)
  44. self.basket = Basket()
  45. self.basket.add_product(self.product)
  46. def set_threshold(self, threshold):
  47. self.product.stockrecord.low_stock_threshold = threshold
  48. self.product.stockrecord.save()
  49. def test_closes_open_alert(self):
  50. self.set_threshold(5)
  51. OrderCreator().place_order(self.basket)
  52. alert = StockAlert.objects.get(stockrecord=self.product.stockrecord)
  53. self.assertEqual(StockAlert.OPEN, alert.status)
  54. # Restock product
  55. self.product.stockrecord.num_in_stock = 15
  56. self.product.stockrecord.save()
  57. alert = StockAlert.objects.get(stockrecord=self.product.stockrecord)
  58. self.assertEqual(StockAlert.CLOSED, alert.status)