acupedia-api/tests/test_cart_currency.py

108 lines
4.5 KiB
Python

import asyncio
import unittest
from decimal import Decimal
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
from sqlalchemy.pool import NullPool
from app.core.config import settings
from app.features.cart.repository import CartRepository
from app.features.cart.service import CartService
from app.features.cart.schema import CartCalculationRequest
class TestCartCurrencyResolution(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.engine = create_async_engine(settings.database_url, poolclass=NullPool)
self.session_factory = async_sessionmaker(bind=self.engine, class_=AsyncSession, expire_on_commit=False)
self.session = self.session_factory()
self.repo = CartRepository(self.session)
self.service = CartService(self.repo)
async def asyncTearDown(self):
await self.session.close()
await self.engine.dispose()
async def test_resolve_points_acu_usd(self):
items = await self.repo.get_items(["POINTS_ACU"], "USD")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].code, "POINTS_ACU")
self.assertEqual(items[0].price, Decimal("5.00"))
async def test_resolve_points_acu_inr(self):
items = await self.repo.get_items(["POINTS_ACU"], "INR")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].code, "POINTS_ACU")
self.assertEqual(items[0].price, Decimal("300.00"))
async def test_resolve_suffixed_code_usd(self):
items = await self.repo.get_items(["POINTS_ACU_USD"], "USD")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].code, "POINTS_ACU_USD")
self.assertEqual(items[0].price, Decimal("5.00"))
async def test_resolve_lifetime_all_usd(self):
items = await self.repo.get_items(["LIFETIME_ALL"], "USD")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].code, "LIFETIME_ALL")
self.assertEqual(items[0].price, Decimal("350.00"))
async def test_resolve_lifetime_all_inr(self):
items = await self.repo.get_items(["LIFETIME_ALL"], "INR")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].code, "LIFETIME_ALL")
self.assertEqual(items[0].price, Decimal("15000.00"))
async def test_calculate_usd_cart(self):
calc = await self.service.calculate(CartCalculationRequest(
cart_item_codes=["POINTS_ACU"],
currency="USD"
))
self.assertEqual(calc.currency, "USD")
self.assertEqual(calc.base_subtotal, 5.0)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.grand_total, 5.0)
self.assertEqual(len(calc.cart_items), 1)
self.assertEqual(calc.cart_items[0].code, "POINTS_ACU")
async def test_calculate_inr_cart(self):
calc = await self.service.calculate(CartCalculationRequest(
cart_item_codes=["POINTS_ACU"],
currency="INR"
))
self.assertEqual(calc.currency, "INR")
self.assertEqual(calc.base_subtotal, 300.0)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.gst_amount, 0.0)
self.assertEqual(calc.grand_total, 300.0)
async def test_multi_item_usd_cart(self):
calc = await self.service.calculate(CartCalculationRequest(
cart_item_codes=["POINTS_ACU", "POINTS_EAR"],
currency="USD"
))
self.assertEqual(calc.currency, "USD")
self.assertEqual(calc.base_subtotal, 8.0)
self.assertEqual(calc.convenience_fee, 0.0)
self.assertEqual(calc.grand_total, 8.0)
self.assertEqual(len(calc.cart_items), 2)
async def test_loyalty_discount_with_two_owned_categories(self):
calc = await self.service.calculate(CartCalculationRequest(
cart_item_codes=["POINTS_ACU"],
owned_category_names=["Pattern", "Nutrition"],
currency="INR"
))
self.assertEqual(calc.currency, "INR")
self.assertEqual(calc.base_subtotal, 300.0)
self.assertEqual(calc.bundle_discount_amount, 30.0)
self.assertEqual(calc.discount_policy, "Loyalty Discount (10% Off)")
self.assertEqual(calc.grand_total, 270.0)
async def test_unknown_plan_raises_error(self):
with self.assertRaises(ValueError):
await self.service.calculate(CartCalculationRequest(
cart_item_codes=["NON_EXISTENT_PLAN_XYZ"],
currency="USD"
))
if __name__ == "__main__":
unittest.main()