188 lines
6.4 KiB
Python
188 lines
6.4 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from decimal import Decimal
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from app.db.enums import BillingPeriod, PlanType
|
|
from app.features.access.service import LIFETIME_PLAN_CODES
|
|
from app.features.admin.schema import PlanPricingUpdateRequest
|
|
from app.features.admin.services.plan_service import AdminPlanService
|
|
from app.features.plans import get_public_plans
|
|
|
|
|
|
def _exec_result(*, scalar=None, scalars_list=None, rows=None):
|
|
res = MagicMock()
|
|
res.scalar_one_or_none.return_value = scalar
|
|
res.scalars.return_value.first.return_value = scalar
|
|
res.scalars.return_value.all.return_value = scalars_list or []
|
|
res.all.return_value = rows or []
|
|
return res
|
|
|
|
|
|
def test_lifetime_plan_codes_include_eur():
|
|
assert {"LIFETIME_ALL", "LIFETIME_ALL_USD", "LIFETIME_ALL_EUR"}.issubset(LIFETIME_PLAN_CODES)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ensure_pending_pricing_plan_creates_inactive_three_tier_plans():
|
|
mod = SimpleNamespace(
|
|
id=uuid.uuid4(),
|
|
code="m0044",
|
|
name="Pediatric Disorders",
|
|
description="TCM pediatrics",
|
|
)
|
|
session = AsyncMock()
|
|
session.add = MagicMock()
|
|
session.execute.side_effect = [
|
|
_exec_result(scalar=mod), # Module lookup
|
|
_exec_result(scalars_list=[]), # Existing ONE_TIME plan links
|
|
_exec_result(scalar=None), # Existing MOD_M0044 code check
|
|
]
|
|
|
|
svc = AdminPlanService(session)
|
|
code = await svc.ensure_pending_pricing_plan_for_module("m0044")
|
|
assert code == "MOD_M0044"
|
|
# 3 AccessPlan + 3 AccessPlanModule added
|
|
assert session.add.call_count == 6
|
|
added_plans = [c.args[0] for c in session.add.call_args_list if hasattr(c.args[0], "currency_code")]
|
|
assert [p.code for p in added_plans] == ["MOD_M0044", "MOD_M0044_USD", "MOD_M0044_EUR"]
|
|
assert all(p.is_active is False for p in added_plans)
|
|
assert all(p.metadata_json == {"pricing_status": "pending_pricing"} for p in added_plans)
|
|
session.commit.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_toggle_status_blocks_pending_pricing_and_update_pricing_configures():
|
|
pending_plan = SimpleNamespace(
|
|
id=uuid.uuid4(),
|
|
code="MOD_M0044",
|
|
name="Pediatric Disorders",
|
|
description="",
|
|
plan_type=PlanType.ONE_TIME,
|
|
billing_period=BillingPeriod.YEARLY,
|
|
module_fee=Decimal(0),
|
|
currency_code="INR",
|
|
is_active=False,
|
|
metadata_json={"pricing_status": "pending_pricing"},
|
|
)
|
|
session = AsyncMock()
|
|
session.execute.return_value = _exec_result(scalars_list=[pending_plan])
|
|
|
|
svc = AdminPlanService(session)
|
|
with pytest.raises(ValueError, match="Cannot activate plan before pricing is configured"):
|
|
await svc.toggle_status("MOD_M0044", True)
|
|
|
|
# Now update pricing -> clears pending_pricing to configured
|
|
session.execute.return_value = _exec_result(scalars_list=[pending_plan])
|
|
await svc.update_pricing(
|
|
"MOD_M0044",
|
|
PlanPricingUpdateRequest(price_inr=600, price_usd=15, price_eur=15),
|
|
)
|
|
assert pending_plan.module_fee == Decimal("600")
|
|
assert pending_plan.metadata_json["pricing_status"] == "configured"
|
|
|
|
# Now activation succeeds
|
|
session.execute.return_value = _exec_result(scalars_list=[pending_plan])
|
|
res = await svc.toggle_status("MOD_M0044", True)
|
|
assert res["is_active"] is True
|
|
assert pending_plan.is_active is True
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_public_plans_enriches_module_metadata_and_sort_order():
|
|
pid = uuid.uuid4()
|
|
plan = SimpleNamespace(
|
|
id=pid,
|
|
code="MOD_M0044",
|
|
name="Pediatric Disorders",
|
|
description="",
|
|
plan_type=PlanType.ONE_TIME,
|
|
billing_period=BillingPeriod.YEARLY,
|
|
module_fee=Decimal("600"),
|
|
total_fee=None,
|
|
currency_code="INR",
|
|
is_active=True,
|
|
metadata_json={"pricing_status": "configured", "color": "from-teal-500 to-cyan-500"},
|
|
)
|
|
session = AsyncMock()
|
|
session.execute.side_effect = [
|
|
_exec_result(scalars_list=[plan]),
|
|
_exec_result(
|
|
rows=[
|
|
(pid, "m0044", "pediatric-disorders", "Pediatric Disorders", "TCM pediatrics", {"kind": "therapeutic", "sort_order": 40}),
|
|
]
|
|
),
|
|
]
|
|
|
|
result = await get_public_plans(session=session)
|
|
assert len(result) == 1
|
|
assert result[0]["code"] == "MOD_M0044"
|
|
assert result[0]["color"] == "from-teal-500 to-cyan-500"
|
|
assert result[0]["modules"][0] == {
|
|
"code": "m0044",
|
|
"slug": "pediatric-disorders",
|
|
"name": "Pediatric Disorders",
|
|
"description": "TCM pediatrics",
|
|
"category": "Therapeutics",
|
|
"sort_order": 40,
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_pricing_syncs_bundle_module_ids():
|
|
inr_id, usd_id, eur_id = uuid.uuid4(), uuid.uuid4(), uuid.uuid4()
|
|
m1, m2 = uuid.uuid4(), uuid.uuid4()
|
|
plans = [
|
|
SimpleNamespace(
|
|
id=inr_id,
|
|
code="ALL_IN_ONE",
|
|
name="All-in-One",
|
|
description="",
|
|
module_fee=Decimal("4000"),
|
|
currency_code="INR",
|
|
is_active=True,
|
|
metadata_json={},
|
|
),
|
|
SimpleNamespace(
|
|
id=usd_id,
|
|
code="ALL_IN_ONE_USD",
|
|
name="All-in-One (USD)",
|
|
description="",
|
|
module_fee=Decimal("100"),
|
|
currency_code="USD",
|
|
is_active=True,
|
|
metadata_json={},
|
|
),
|
|
SimpleNamespace(
|
|
id=eur_id,
|
|
code="ALL_IN_ONE_EUR",
|
|
name="All-in-One (EUR)",
|
|
description="",
|
|
module_fee=Decimal("100"),
|
|
currency_code="EUR",
|
|
is_active=True,
|
|
metadata_json={},
|
|
),
|
|
]
|
|
session = AsyncMock()
|
|
session.add = MagicMock()
|
|
session.execute.side_effect = [
|
|
_exec_result(scalars_list=plans),
|
|
_exec_result(),
|
|
]
|
|
|
|
svc = AdminPlanService(session)
|
|
res = await svc.update_pricing(
|
|
"ALL_IN_ONE",
|
|
PlanPricingUpdateRequest(module_ids=[str(m1), str(m2), str(m1), "invalid-uuid"]),
|
|
)
|
|
assert res["status"] == "success"
|
|
added_links = [(c.args[0].plan_id, c.args[0].module_id) for c in session.add.call_args_list]
|
|
assert len(added_links) == 6
|
|
assert (inr_id, m1) in added_links and (eur_id, m2) in added_links
|
|
session.commit.assert_awaited_once()
|