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.

scales_tests.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from decimal import Decimal as D
  2. from django.test import TestCase
  3. from nose.plugins.attrib import attr
  4. from oscar.apps.shipping import Scales
  5. from oscar.apps.basket.models import Basket
  6. from oscar.test import factories
  7. @attr('shipping')
  8. class TestScales(TestCase):
  9. def test_weighs_uses_specified_attribute(self):
  10. scales = Scales(attribute_code='weight')
  11. p = factories.create_product(attributes={'weight': '1'})
  12. self.assertEqual(1, scales.weigh_product(p))
  13. def test_uses_default_weight_when_attribute_is_missing(self):
  14. scales = Scales(attribute_code='weight', default_weight=0.5)
  15. p = factories.create_product()
  16. self.assertEqual(0.5, scales.weigh_product(p))
  17. def test_raises_exception_when_attribute_is_missing(self):
  18. scales = Scales(attribute_code='weight')
  19. p = factories.create_product()
  20. with self.assertRaises(ValueError):
  21. scales.weigh_product(p)
  22. def test_returns_zero_for_empty_basket(self):
  23. basket = Basket()
  24. scales = Scales(attribute_code='weight')
  25. self.assertEquals(0, scales.weigh_basket(basket))
  26. def test_returns_correct_weight_for_nonempty_basket(self):
  27. basket = factories.create_basket(empty=True)
  28. record = factories.create_stockrecord(price_excl_tax=D('5.00'))
  29. info = factories.create_stockinfo(record)
  30. basket.add_product(
  31. factories.create_product(attributes={'weight': '1'}), info)
  32. basket.add_product(
  33. factories.create_product(attributes={'weight': '2'}), info)
  34. scales = Scales(attribute_code='weight')
  35. self.assertEquals(1+2, scales.weigh_basket(basket))
  36. def test_returns_correct_weight_for_nonempty_basket_with_line_quantities(self):
  37. basket = factories.create_basket(empty=True)
  38. record = factories.create_stockrecord(price_excl_tax=D('5.00'))
  39. info = factories.create_stockinfo(record)
  40. basket.add_product(factories.create_product(
  41. attributes={'weight': '1'}), info, quantity=3)
  42. basket.add_product(factories.create_product(
  43. attributes={'weight': '2'}), info, quantity=4)
  44. scales = Scales(attribute_code='weight')
  45. self.assertEquals(1*3+2*4, scales.weigh_basket(basket))