acupedia-api/tests/unit/test_cart_pricing.py

265 lines
9.8 KiB
Python

from __future__ import annotations
from decimal import Decimal
import pytest
from app.features.payments.pricing import PricingEngine, PricingItem, PricingCoupon
@pytest.fixture
def engine() -> PricingEngine:
return PricingEngine()
class TestPricingEngineBundleRules:
def test_empty_cart(self, engine: PricingEngine) -> None:
result = engine.calculate(
items=[],
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=True,
)
assert result["base_subtotal"] == 0.0
assert result["grand_total"] == 0.0
assert result["bundle_discount_amount"] == 0.0
assert result["discount_policy"] == "None"
def test_first_time_buy_single_unit_no_discount(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_ACU", name="Acupuncture", category="acu", price=Decimal("1000.00")),
]
result = engine.calculate(
items=items,
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=True,
)
assert result["base_subtotal"] == 1000.0
assert result["bundle_discount_amount"] == 0.0
assert result["grand_total"] == 1000.0
assert result["discount_policy"] == "None"
def test_first_time_buy_two_units_ten_percent(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_ACU", name="Acupuncture", category="acu", price=Decimal("1000.00")),
PricingItem(code="MOD_MOXA", name="Moxibustion", category="moxa", price=Decimal("1000.00")),
]
result = engine.calculate(
items=items,
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=True,
)
assert result["base_subtotal"] == 2000.0
assert result["bundle_discount_amount"] == 200.0 # 10% of 2000
assert result["grand_total"] == 1800.0
assert "10% Off" in result["discount_policy"]
def test_first_time_buy_three_units_twenty_percent(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")),
PricingItem(code="MOD_2", name="Mod 2", category="cat2", price=Decimal("1000.00")),
PricingItem(code="MOD_3", name="Mod 3", category="cat3", price=Decimal("1000.00")),
]
result = engine.calculate(
items=items,
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=True,
)
assert result["base_subtotal"] == 3000.0
assert result["bundle_discount_amount"] == 600.0 # 20% of 3000
assert result["grand_total"] == 2400.0
assert "20% Off" in result["discount_policy"]
def test_first_time_buy_four_or_more_units_thirty_percent(self, engine: PricingEngine) -> None:
items = [
PricingItem(code=f"MOD_{i}", name=f"Mod {i}", category=f"cat{i}", price=Decimal("1000.00"))
for i in range(1, 5)
]
result = engine.calculate(
items=items,
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=True,
)
assert result["base_subtotal"] == 4000.0
assert result["bundle_discount_amount"] == 1200.0 # 30% of 4000
assert result["grand_total"] == 2800.0
assert "30% Off" in result["discount_policy"]
def test_lifetime_package_overrides_discounts(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="LIFETIME_ALL", name="Lifetime Access", category="lifetime", price=Decimal("15000.00")),
]
result = engine.calculate(
items=items,
owned_categories=["cat1", "cat2"],
coupon=None,
currency="INR",
is_first_purchase=False,
)
assert result["discount_policy"] == "Lifetime Bulk Package"
assert result["bundle_discount_amount"] == 0.0
assert result["grand_total"] == 15000.0
class TestPricingEngineLoyaltyRules:
def test_loyalty_discount_scaling(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")),
]
# 1 owned category -> 5%
res1 = engine.calculate(
items=items,
owned_categories=["cat_old1"],
coupon=None,
currency="INR",
is_first_purchase=False,
)
assert res1["bundle_discount_amount"] == 50.0 # 5%
assert "5% Off" in res1["discount_policy"]
# 3 owned categories -> 15%
res3 = engine.calculate(
items=items,
owned_categories=["cat_old1", "cat_old2", "cat_old3"],
coupon=None,
currency="INR",
is_first_purchase=False,
)
assert res3["bundle_discount_amount"] == 150.0 # 15%
assert "15% Off" in res3["discount_policy"]
# 6 owned categories -> capped at 25%
res6 = engine.calculate(
items=items,
owned_categories=[f"cat_old_{i}" for i in range(6)],
coupon=None,
currency="INR",
is_first_purchase=False,
)
assert res6["bundle_discount_amount"] == 250.0 # max 25%
assert "25% Off" in res6["discount_policy"]
class TestPricingEngineCouponRules:
def test_flat_coupon_discount(self, engine: PricingEngine) -> None:
items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00"))]
coupon = PricingCoupon(
code="FLAT200",
category="general",
discount_type="flat",
value=Decimal("200.00"),
description="Flat Rs 200 off",
)
result = engine.calculate(
items=items,
owned_categories=[],
coupon=coupon,
currency="INR",
is_first_purchase=True,
)
assert result["coupon_discount_amount"] == 200.0
assert result["grand_total"] == 800.0
assert "FLAT200" in result["coupon_applied"]
def test_percentage_coupon_discount(self, engine: PricingEngine) -> None:
items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00"))]
coupon = PricingCoupon(
code="SAVE15",
category="general",
discount_type="percentage",
value=Decimal("15.00"),
)
result = engine.calculate(
items=items,
owned_categories=[],
coupon=coupon,
currency="INR",
is_first_purchase=True,
)
assert result["coupon_discount_amount"] == 150.0 # 15% of 1000
assert result["grand_total"] == 850.0
def test_coupon_max_discount_cap(self, engine: PricingEngine) -> None:
items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("5000.00"))]
coupon = PricingCoupon(
code="BIG50",
category="general",
discount_type="percentage",
value=Decimal("50.00"), # 50% would be 2500
max_discount_limit=Decimal("1000.00"), # capped at 1000
)
result = engine.calculate(
items=items,
owned_categories=[],
coupon=coupon,
currency="INR",
is_first_purchase=True,
)
assert result["coupon_discount_amount"] == 1000.0
assert result["grand_total"] == 4000.0
def test_coupon_min_purchase_amount_not_met(self, engine: PricingEngine) -> None:
items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("500.00"))]
coupon = PricingCoupon(
code="MIN1000",
category="general",
discount_type="flat",
value=Decimal("100.00"),
min_purchase_amount=Decimal("1000.00"),
)
result = engine.calculate(
items=items,
owned_categories=[],
coupon=coupon,
currency="INR",
is_first_purchase=True,
)
assert result["coupon_discount_amount"] == 0.0
assert result["grand_total"] == 500.0
assert result["coupon_error"] is not None
assert "Minimum cart spend" in result["coupon_error"]
def test_introductory_flat_coupon_disables_unit_bundle_discount(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")),
PricingItem(code="MOD_2", name="Mod 2", category="cat2", price=Decimal("1000.00")),
]
coupon = PricingCoupon(
code="INTRO100",
category="introductory",
discount_type="flat",
value=Decimal("100.00"),
)
result = engine.calculate(
items=items,
owned_categories=[],
coupon=coupon,
currency="INR",
is_first_purchase=True,
)
assert result["discount_policy"] == "None (Overridden by New User Event Coupon)"
assert result["bundle_discount_amount"] == 0.0
assert result["coupon_discount_amount"] == 100.0
assert result["grand_total"] == 1900.0
def test_not_first_purchase_without_owned_categories(self, engine: PricingEngine) -> None:
items = [
PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")),
]
result = engine.calculate(
items=items,
owned_categories=[],
coupon=None,
currency="INR",
is_first_purchase=False,
)
assert result["bundle_discount_amount"] == 0.0
assert result["discount_policy"] == "None"