|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +import uuid |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from odoo import fields |
| 6 | +from odoo.tests import TransactionCase |
| 7 | + |
| 8 | + |
| 9 | +class TestFundBalanceOptimization(TransactionCase): |
| 10 | + """Test that fund balance is fetched once per approval batch, not per entitlement. |
| 11 | +
|
| 12 | + The approve_entitlements methods previously called check_fund_balance() |
| 13 | + (2 SQL queries) inside the per-entitlement loop. Now the balance is |
| 14 | + fetched once and tracked via a running total in Python. |
| 15 | + """ |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + super().setUp() |
| 19 | + self.program = self.env["spp.program"].create({"name": f"Test Program {uuid.uuid4().hex[:8]}"}) |
| 20 | + # Create a journal for the program |
| 21 | + self.journal = self.env["account.journal"].create( |
| 22 | + { |
| 23 | + "name": "Test Journal", |
| 24 | + "type": "bank", |
| 25 | + "code": f"TJ{uuid.uuid4().hex[:4].upper()}", |
| 26 | + } |
| 27 | + ) |
| 28 | + self.program.journal_id = self.journal.id |
| 29 | + |
| 30 | + self.cycle = self.env["spp.cycle"].create( |
| 31 | + { |
| 32 | + "name": "Test Cycle", |
| 33 | + "program_id": self.program.id, |
| 34 | + "start_date": fields.Date.today(), |
| 35 | + "end_date": fields.Date.today(), |
| 36 | + } |
| 37 | + ) |
| 38 | + |
| 39 | + self.registrants = self.env["res.partner"] |
| 40 | + for i in range(5): |
| 41 | + self.registrants |= self.env["res.partner"].create({"name": f"Registrant {i}", "is_registrant": True}) |
| 42 | + |
| 43 | + def _create_default_manager(self): |
| 44 | + return self.env["spp.program.entitlement.manager.default"].create( |
| 45 | + { |
| 46 | + "name": "Test Default Manager", |
| 47 | + "program_id": self.program.id, |
| 48 | + "amount_per_cycle": 100.0, |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + def _create_cash_manager(self): |
| 53 | + manager = self.env["spp.program.entitlement.manager.cash"].create( |
| 54 | + { |
| 55 | + "name": "Test Cash Manager", |
| 56 | + "program_id": self.program.id, |
| 57 | + } |
| 58 | + ) |
| 59 | + self.env["spp.program.entitlement.manager.cash.item"].create( |
| 60 | + { |
| 61 | + "entitlement_id": manager.id, |
| 62 | + "amount": 100.0, |
| 63 | + } |
| 64 | + ) |
| 65 | + return manager |
| 66 | + |
| 67 | + def _create_entitlements(self, count=5, amount=100.0): |
| 68 | + """Create multiple entitlements in pending_validation state.""" |
| 69 | + entitlements = self.env["spp.entitlement"] |
| 70 | + for i in range(count): |
| 71 | + entitlements |= self.env["spp.entitlement"].create( |
| 72 | + { |
| 73 | + "partner_id": self.registrants[i].id, |
| 74 | + "cycle_id": self.cycle.id, |
| 75 | + "initial_amount": amount, |
| 76 | + "state": "pending_validation", |
| 77 | + "is_cash_entitlement": True, |
| 78 | + } |
| 79 | + ) |
| 80 | + return entitlements |
| 81 | + |
| 82 | + def test_default_manager_calls_check_fund_balance_once(self): |
| 83 | + """DefaultCashEntitlementManager.approve_entitlements must call |
| 84 | + check_fund_balance at most once per batch.""" |
| 85 | + manager = self._create_default_manager() |
| 86 | + entitlements = self._create_entitlements(count=3) |
| 87 | + |
| 88 | + with patch.object( |
| 89 | + type(manager), |
| 90 | + "check_fund_balance", |
| 91 | + wraps=manager.check_fund_balance, |
| 92 | + ) as mock_check: |
| 93 | + # Set fund balance high enough to approve all |
| 94 | + mock_check.return_value = 10000.0 |
| 95 | + manager.approve_entitlements(entitlements) |
| 96 | + self.assertEqual( |
| 97 | + mock_check.call_count, |
| 98 | + 1, |
| 99 | + f"check_fund_balance should be called exactly once, was called {mock_check.call_count} times", |
| 100 | + ) |
| 101 | + |
| 102 | + def test_cash_manager_calls_check_fund_balance_once(self): |
| 103 | + """SppCashEntitlementManager.approve_entitlements must call |
| 104 | + check_fund_balance at most once per batch.""" |
| 105 | + manager = self._create_cash_manager() |
| 106 | + entitlements = self._create_entitlements(count=3) |
| 107 | + |
| 108 | + with patch.object( |
| 109 | + type(manager), |
| 110 | + "check_fund_balance", |
| 111 | + wraps=manager.check_fund_balance, |
| 112 | + ) as mock_check: |
| 113 | + mock_check.return_value = 10000.0 |
| 114 | + manager.approve_entitlements(entitlements) |
| 115 | + self.assertEqual( |
| 116 | + mock_check.call_count, |
| 117 | + 1, |
| 118 | + f"check_fund_balance should be called exactly once, was called {mock_check.call_count} times", |
| 119 | + ) |
| 120 | + |
| 121 | + def test_fund_balance_insufficient_stops_early(self): |
| 122 | + """When fund runs out mid-batch, remaining entitlements are not approved.""" |
| 123 | + manager = self._create_default_manager() |
| 124 | + entitlements = self._create_entitlements(count=3, amount=100.0) |
| 125 | + |
| 126 | + with patch.object( |
| 127 | + type(manager), |
| 128 | + "check_fund_balance", |
| 129 | + return_value=250.0, |
| 130 | + ): |
| 131 | + state_err, message = manager.approve_entitlements(entitlements) |
| 132 | + self.assertEqual(state_err, 1) |
| 133 | + self.assertIn("insufficient", message) |
| 134 | + |
| 135 | + def test_fund_balance_running_total_correct(self): |
| 136 | + """Running total must correctly track cumulative approved amounts.""" |
| 137 | + manager = self._create_default_manager() |
| 138 | + entitlements = self._create_entitlements(count=3, amount=100.0) |
| 139 | + |
| 140 | + with patch.object( |
| 141 | + type(manager), |
| 142 | + "check_fund_balance", |
| 143 | + return_value=300.0, |
| 144 | + ): |
| 145 | + state_err, _message = manager.approve_entitlements(entitlements) |
| 146 | + self.assertEqual(state_err, 0) |
| 147 | + # All 3 should be approved (300 fund, 3x100 = 300) |
| 148 | + for ent in entitlements: |
| 149 | + ent.invalidate_recordset(["state"]) |
| 150 | + self.assertEqual(ent.state, "approved") |
0 commit comments