From 800382bac3b21351fcfdedfdf9590dd6330e7b87 Mon Sep 17 00:00:00 2001 From: Samar Hassan Date: Thu, 21 Sep 2023 18:51:47 +0500 Subject: [PATCH] tests :ballot_box_with_check: add tests to check basket total after rounding --- tests/integration/basket/test_total.py | 69 ++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/integration/basket/test_total.py diff --git a/tests/integration/basket/test_total.py b/tests/integration/basket/test_total.py new file mode 100644 index 00000000000..7c459382a14 --- /dev/null +++ b/tests/integration/basket/test_total.py @@ -0,0 +1,69 @@ +from decimal import Decimal as D + +from django.test import TestCase +from django.test.utils import override_settings + +from oscar.apps.basket.models import Basket +from oscar.apps.offer import models +from oscar.apps.partner import strategy +from oscar.test.basket import add_product + + +def create_fixed_tax_basket(price): + basket = Basket.objects.create() + basket.strategy = strategy.UK() + add_product(basket, D(price), 1) + return basket + + +class TestBasketTotalAfterRounding(TestCase): + def setUp(self): + product_range = models.Range.objects.create( + name="All products", includes_all_products=True + ) + condition = models.CountCondition.objects.create( + range=product_range, type=models.Condition.COUNT, value=1 + ) + self.benefit = models.Benefit.objects.create( + range=product_range, + type=models.Benefit.FIXED, + value=D("11.45"), + ) + self.offer = models.ConditionalOffer( + name="Test", + offer_type=models.ConditionalOffer.SITE, + condition=condition, + benefit=self.benefit, + ) + + @override_settings(OSCAR_OFFERS_INCL_TAX=True) + def test_total_excl_tax_precision_down(self): + basket = create_fixed_tax_basket(9.99) + self.benefit.value = D("4.13") + self.offer.apply_benefit(basket) + + # 9.99-round(3.441) => 9.99-3.44 = 6.55 + self.assertEqual(basket.total_excl_tax, D("6.55")) + + @override_settings(OSCAR_OFFERS_INCL_TAX=True) + def test_total_excl_tax_precision_up(self): + basket = create_fixed_tax_basket(9.99) + self.benefit.value = D("4.14") + self.offer.apply_benefit(basket) + + # 9.99-round(3.449) => 9.99-3.45 = 6.54 + self.assertEqual(basket.total_excl_tax, D("6.54")) + + def test_total_incl_tax_precision_down(self): + basket = create_fixed_tax_basket(20.37) + self.offer.apply_benefit(basket) + + # 10.704 rounded to 10.70 + self.assertEqual(basket.total_incl_tax, D("10.70")) + + def test_total_incl_tax_precision_up(self): + basket = create_fixed_tax_basket(20.38) + self.offer.apply_benefit(basket) + + # 10.716 rounded to 10.72 + self.assertEqual(basket.total_incl_tax, D("10.72"))