Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release Notes
=============

Version 1.147.5
---------------

- Make basket serializer test deterministic (#3517)

Version 1.147.4 (Released April 22, 2026)
---------------

Expand Down
26 changes: 22 additions & 4 deletions ecommerce/serializers/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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():
"""
Expand Down
2 changes: 1 addition & 1 deletion main/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading