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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from decimal import Decimal as D
  2. import datetime
  3. from django.utils import unittest
  4. from oscar.test.helpers import create_product
  5. from oscar.apps.partner.abstract_models import partner_wrappers
  6. class DummyWrapper(object):
  7. def availability(self, stockrecord):
  8. return 'Dummy response'
  9. def dispatch_date(self, stockrecord):
  10. return "Another dummy response"
  11. class StockRecordTests(unittest.TestCase):
  12. def setUp(self):
  13. self.product = create_product(price=D('10.00'), num_in_stock=10)
  14. self.stockrecord = self.product.stockrecord
  15. def test_get_price_incl_tax_defaults_to_no_tax(self):
  16. self.assertEquals(D('10.00'), self.product.stockrecord.price_incl_tax)
  17. def test_get_price_excl_tax_returns_correct_value(self):
  18. self.assertEquals(D('10.00'), self.product.stockrecord.price_excl_tax)
  19. def test_net_stock_level_with_no_allocation(self):
  20. self.assertEquals(10, self.product.stockrecord.net_stock_level)
  21. def test_net_stock_level_with_allocation(self):
  22. self.product.stockrecord.allocate(5)
  23. self.assertEquals(10-5, self.product.stockrecord.net_stock_level)
  24. def test_allocated_does_not_alter_num_in_stock(self):
  25. self.stockrecord.allocate(5)
  26. self.assertEqual(10, self.stockrecord.num_in_stock)
  27. self.assertEqual(5, self.stockrecord.num_allocated)
  28. def test_consuming_allocation(self):
  29. self.stockrecord.allocate(5)
  30. self.stockrecord.consume_allocation(3)
  31. self.assertEqual(2, self.stockrecord.num_allocated)
  32. self.assertEqual(7, self.stockrecord.num_in_stock)
  33. def test_cancelling_allocation(self):
  34. self.stockrecord.allocate(5)
  35. self.stockrecord.cancel_allocation(4)
  36. self.assertEqual(1, self.stockrecord.num_allocated)
  37. self.assertEqual(10, self.stockrecord.num_in_stock)
  38. class DefaultWrapperTests(unittest.TestCase):
  39. def test_default_wrapper_for_in_stock(self):
  40. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=10)
  41. self.assertEquals("In stock (10 available)", product.stockrecord.availability)
  42. self.assertEqual("instock", product.stockrecord.availability_code)
  43. def test_default_wrapper_for_out_of_stock(self):
  44. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=0)
  45. self.assertEquals("Out of stock", product.stockrecord.availability)
  46. self.assertEqual("outofstock", product.stockrecord.availability_code)
  47. def test_dispatch_date_for_in_stock(self):
  48. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=1)
  49. tomorrow = datetime.date.today() + datetime.timedelta(days=1)
  50. self.assertEquals(tomorrow, product.stockrecord.dispatch_date)
  51. def test_dispatch_date_for_out_of_stock(self):
  52. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=0)
  53. date = datetime.date.today() + datetime.timedelta(days=7)
  54. self.assertEquals(date, product.stockrecord.dispatch_date)
  55. class CustomWrapperTests(unittest.TestCase):
  56. def setUp(self):
  57. partner_wrappers['Acme'] = DummyWrapper()
  58. def tearDown(self):
  59. del partner_wrappers['Acme']
  60. def test_wrapper_availability_gets_called(self):
  61. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=10)
  62. self.assertEquals(u"Dummy response", unicode(product.stockrecord.availability))
  63. def test_wrapper_dispatch_date_gets_called(self):
  64. product = create_product(price=D('10.00'), partner="Acme", num_in_stock=10)
  65. self.assertEquals("Another dummy response", product.stockrecord.dispatch_date)