from decimal import Decimal from django.test import TestCase from apps.pricing.engine import PricingEngine from apps.pricing.models import PricingRule from apps.workflows.models import Workflow, WorkflowStep, WorkflowVersion class PricingEngineTests(TestCase): def setUp(self): wf = Workflow.objects.create(name="P", slug="p") self.version = WorkflowVersion.objects.create(workflow=wf, version=1) self.washing = WorkflowStep.objects.create(version=self.version, key="washing", name="Washing") self.drying = WorkflowStep.objects.create(version=self.version, key="drying", name="Drying") def test_per_kg(self): PricingRule.objects.create(step=self.washing, name="Wash", rule_type="per_kg", amount=Decimal("5000")) result = PricingEngine().calculate(850, [self.washing]) self.assertEqual(result["total"], "4250000.00") def test_tiered(self): PricingRule.objects.create(step=self.washing, name="Tiered", rule_type="tiered", config={ "tiers": [{"from": 0, "price": "5000"}, {"from": 100, "price": "4000"}, {"from": 500, "price": "3000"}], }) result = PricingEngine().calculate(600, [self.washing]) self.assertEqual(result["total"], "1800000.00") def test_discount_and_tax(self): PricingRule.objects.create(step=self.washing, name="Wash", rule_type="per_kg", amount=Decimal("1000")) PricingRule.objects.create(step=self.washing, name="Disc", rule_type="discount", amount=Decimal("10"), config={"percentage": True}) PricingRule.objects.create(step=self.washing, name="Tax", rule_type="tax", amount=Decimal("9")) result = PricingEngine().calculate(100, [self.washing]) # subtotal 100000, discount 10% => 10000, taxable 90000, tax 9% => 8100, total 98100 self.assertEqual(result["total"], "98100.00") def test_multiple_steps(self): PricingRule.objects.create(step=self.washing, name="Wash", rule_type="per_kg", amount=Decimal("5000")) PricingRule.objects.create(step=self.drying, name="Dry", rule_type="per_kg", amount=Decimal("8000")) result = PricingEngine().calculate(100, [self.washing, self.drying]) self.assertEqual(result["total"], "1300000.00")