Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add more tests and hopefully fix absolute discounts

  • Loading branch information...
commit 55a94b570de52c290399300947e111b3f0e7c684 1 parent fbbbbae
@patrys patrys authored
Showing with 42 additions and 3 deletions.
  1. +7 −3 oscar/apps/offer/models.py
  2. +35 −0 oscar/apps/offer/tests.py
View
10 oscar/apps/offer/models.py
@@ -494,13 +494,17 @@ def apply(self, basket, condition=None):
# Update line with discounts
line_discount = self.round(min(remaining_discount, quantity_affected * price))
- line.discount(line_discount, quantity_affected)
-
+ if condition:
+ # Pass zero as quantity to avoid double consumption
+ line.discount(line_discount, 0)
+ else:
+ line.discount(line_discount, quantity_affected)
# Update loop vars
affected_items += quantity_affected
remaining_discount -= line_discount
discount += line_discount
-
+ if discount > 0 and condition:
+ condition.consume_items(basket)
return discount
View
35 oscar/apps/offer/tests.py
@@ -312,12 +312,47 @@ def test_absolute_does_not_consume_twice(self):
benefit = AbsoluteDiscountBenefit(range=rng, type='Absolute', value=Decimal('100'))
self.assertTrue(condition.is_satisfied(basket))
self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertTrue(condition.is_satisfied(basket))
self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertTrue(condition.is_satisfied(basket))
self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertTrue(condition.is_satisfied(basket))
+ self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertTrue(condition.is_satisfied(basket))
self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertFalse(condition.is_satisfied(basket))
+ self.assertEquals(Decimal('0'), benefit.apply(basket, condition))
+
+ def test_absolute_consumes_all(self):
+ product1 = create_product(Decimal('150'))
+ product2 = create_product(Decimal('300'))
+ product3 = create_product(Decimal('300'))
+ rng = Range.objects.create(name='Dummy')
+ rng.included_products.add(product1)
+ rng.included_products.add(product2)
+ rng.included_products.add(product3)
+ condition = ValueCondition(range=rng, type='Value', value=Decimal('500'))
+ basket = Basket.objects.create()
+ basket.add_product(product1, 1)
+ basket.add_product(product2, 1)
+ basket.add_product(product3, 1)
+ benefit = AbsoluteDiscountBenefit(range=rng, type='Absolute', value=Decimal('100'))
+ self.assertTrue(condition.is_satisfied(basket))
self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
self.assertEquals(Decimal('0'), benefit.apply(basket, condition))
+ def test_absolute_applies_line_discount(self):
+ product = create_product(Decimal('500'))
+ rng = Range.objects.create(name='Dummy')
+ rng.included_products.add(product)
+ condition = ValueCondition(range=rng, type='Value', value=Decimal('500'))
+ basket = Basket.objects.create()
+ basket.add_product(product, 1)
+ benefit = AbsoluteDiscountBenefit(range=rng, type='Absolute', value=Decimal('100'))
+ self.assertTrue(condition.is_satisfied(basket))
+ self.assertEquals(Decimal('100'), benefit.apply(basket, condition))
+ self.assertEquals(Decimal('100'), basket.all_lines()[0]._discount)
+
class MultibuyDiscountBenefitTest(OfferTest):
Please sign in to comment.
Something went wrong with that request. Please try again.