From e076f2615ccdaead1dbab61317f715228f4c81b7 Mon Sep 17 00:00:00 2001 From: cp-at-mit Date: Thu, 23 Apr 2026 06:40:47 -0400 Subject: [PATCH 1/2] Make basket serializer test deterministic (#3517) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- ecommerce/serializers/serializers_test.py | 26 +++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ecommerce/serializers/serializers_test.py b/ecommerce/serializers/serializers_test.py index 414f767ecc..bdfc88d699 100644 --- a/ecommerce/serializers/serializers_test.py +++ b/ecommerce/serializers/serializers_test.py @@ -214,10 +214,21 @@ def test_basket_with_product_serializer(): """ Tests serialization of a basket with the attached products (and any discounts applied). + + Uses deterministic test values and consistent rounding to prevent + flaky behavior caused by floating-point precision differences. """ - basket_item = BasketItemFactory.create() - discount = UnlimitedUseDiscountFactory.create() + # Create product with deterministic price that avoids precision edge cases + product = ProductFactory.create(price=Decimal("100.00")) + + # Create discount with deterministic percentage that results in clean calculation + # 20% discount on $100.00 = $20.00 discount, final price $80.00 + discount = UnlimitedUseDiscountFactory.create( + discount_type=DISCOUNT_TYPE_PERCENT_OFF, amount=20 + ) + + basket_item = BasketItemFactory.create(product=product) user = UserFactory.create() basket_discount = BasketDiscount( @@ -230,13 +241,20 @@ def test_basket_with_product_serializer(): serialized_basket = BasketWithProductSerializer(basket_item.basket).data + # Use the same rounding method that the serializer uses for consistent comparison logic = DiscountType.for_discount(discount) - discount_price = logic.get_discounted_price([discount], basket_item.product) + raw_discount_price = logic.get_discounted_price([discount], basket_item.product) + # Apply quantize to match the serializer's rounding method + expected_discount_price = raw_discount_price.quantize(Decimal("0.01")) assert serialized_basket["total_price"] == basket_item.product.price - assert serialized_basket["discounted_price"] == discount_price + assert serialized_basket["discounted_price"] == expected_discount_price assert len(serialized_basket["discounts"]) == 1 + # Additional verification: ensure the discount calculation is correct + # 20% discount on $100.00 should result in $80.00 + assert expected_discount_price == Decimal("80.00") + def test_basket_with_program_product_serializer(): """ From b757d544e155a03b007c5d572ec9d817fbd29629 Mon Sep 17 00:00:00 2001 From: Doof Date: Thu, 23 Apr 2026 14:35:00 +0000 Subject: [PATCH 2/2] Release 1.147.5 --- RELEASE.rst | 5 +++++ main/settings.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/RELEASE.rst b/RELEASE.rst index 222d9e61ba..c3bdaea805 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -1,6 +1,11 @@ Release Notes ============= +Version 1.147.5 +--------------- + +- Make basket serializer test deterministic (#3517) + Version 1.147.4 (Released April 22, 2026) --------------- diff --git a/main/settings.py b/main/settings.py index f38451f41e..2e8fa11633 100644 --- a/main/settings.py +++ b/main/settings.py @@ -37,7 +37,7 @@ from main.sentry import init_sentry from openapi.settings_spectacular import open_spectacular_settings -VERSION = "1.147.4" +VERSION = "1.147.5" log = logging.getLogger()