|
| 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 TestBatchEntitlementCreation(TransactionCase): |
| 10 | + """Test that cash entitlement manager creates entitlements in a single |
| 11 | + batch call instead of one-by-one.""" |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + super().setUp() |
| 15 | + self.program = self.env["spp.program"].create({"name": f"Test Program {uuid.uuid4().hex[:8]}"}) |
| 16 | + self.journal = self.env["account.journal"].create( |
| 17 | + { |
| 18 | + "name": "Test Journal", |
| 19 | + "type": "bank", |
| 20 | + "code": f"TJ{uuid.uuid4().hex[:4].upper()}", |
| 21 | + } |
| 22 | + ) |
| 23 | + self.program.journal_id = self.journal.id |
| 24 | + |
| 25 | + self.cycle = self.env["spp.cycle"].create( |
| 26 | + { |
| 27 | + "name": "Test Cycle", |
| 28 | + "program_id": self.program.id, |
| 29 | + "start_date": fields.Date.today(), |
| 30 | + "end_date": fields.Date.today(), |
| 31 | + } |
| 32 | + ) |
| 33 | + self.manager = self.env["spp.program.entitlement.manager.cash"].create( |
| 34 | + { |
| 35 | + "name": "Test Cash Manager", |
| 36 | + "program_id": self.program.id, |
| 37 | + } |
| 38 | + ) |
| 39 | + self.env["spp.program.entitlement.manager.cash.item"].create( |
| 40 | + { |
| 41 | + "entitlement_id": self.manager.id, |
| 42 | + "amount": 100.0, |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | + # Create beneficiaries with cycle memberships |
| 47 | + self.registrants = self.env["res.partner"] |
| 48 | + self.memberships = self.env["spp.cycle.membership"] |
| 49 | + for i in range(5): |
| 50 | + reg = self.env["res.partner"].create({"name": f"Registrant {i}", "is_registrant": True}) |
| 51 | + self.registrants |= reg |
| 52 | + self.memberships |= self.env["spp.cycle.membership"].create( |
| 53 | + { |
| 54 | + "partner_id": reg.id, |
| 55 | + "cycle_id": self.cycle.id, |
| 56 | + "state": "enrolled", |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + def test_cash_manager_batch_creates_entitlements(self): |
| 61 | + """Cash entitlement manager must create entitlements for all beneficiaries |
| 62 | + using a single batch vals_list passed to create().""" |
| 63 | + self.manager.prepare_entitlements(self.cycle, self.memberships) |
| 64 | + |
| 65 | + entitlements = self.env["spp.entitlement"].search([("cycle_id", "=", self.cycle.id)]) |
| 66 | + self.assertEqual( |
| 67 | + len(entitlements), |
| 68 | + 5, |
| 69 | + f"Expected 5 entitlements, got {len(entitlements)}", |
| 70 | + ) |
| 71 | + # Verify each registrant got an entitlement |
| 72 | + entitled_partners = entitlements.mapped("partner_id") |
| 73 | + for reg in self.registrants: |
| 74 | + self.assertIn(reg, entitled_partners) |
| 75 | + |
| 76 | + |
| 77 | +class TestBatchPaymentCreation(TransactionCase): |
| 78 | + """Test that payment manager creates payments in a single batch call |
| 79 | + instead of one-by-one.""" |
| 80 | + |
| 81 | + def setUp(self): |
| 82 | + super().setUp() |
| 83 | + self.program = self.env["spp.program"].create({"name": f"Test Program {uuid.uuid4().hex[:8]}"}) |
| 84 | + self.journal = self.env["account.journal"].create( |
| 85 | + { |
| 86 | + "name": "Test Journal", |
| 87 | + "type": "bank", |
| 88 | + "code": f"TJ{uuid.uuid4().hex[:4].upper()}", |
| 89 | + } |
| 90 | + ) |
| 91 | + self.program.journal_id = self.journal.id |
| 92 | + |
| 93 | + self.cycle = self.env["spp.cycle"].create( |
| 94 | + { |
| 95 | + "name": "Test Cycle", |
| 96 | + "program_id": self.program.id, |
| 97 | + "start_date": fields.Date.today(), |
| 98 | + "end_date": fields.Date.today(), |
| 99 | + } |
| 100 | + ) |
| 101 | + self.payment_manager = self.env["spp.program.payment.manager.default"].create( |
| 102 | + { |
| 103 | + "name": "Test Payment Manager", |
| 104 | + "program_id": self.program.id, |
| 105 | + "create_batch": False, |
| 106 | + } |
| 107 | + ) |
| 108 | + |
| 109 | + # Create approved entitlements |
| 110 | + self.entitlements = self.env["spp.entitlement"] |
| 111 | + for i in range(5): |
| 112 | + reg = self.env["res.partner"].create({"name": f"Registrant {i}", "is_registrant": True}) |
| 113 | + self.entitlements |= self.env["spp.entitlement"].create( |
| 114 | + { |
| 115 | + "partner_id": reg.id, |
| 116 | + "cycle_id": self.cycle.id, |
| 117 | + "initial_amount": 100.0, |
| 118 | + "state": "approved", |
| 119 | + "is_cash_entitlement": True, |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + def test_payment_manager_batch_creates_payments(self): |
| 124 | + """Payment manager must call create() at most once per batch tag |
| 125 | + (batch), not once per entitlement.""" |
| 126 | + original_create = type(self.env["spp.payment"]).create |
| 127 | + |
| 128 | + call_count = 0 |
| 129 | + |
| 130 | + def counting_create(self_model, vals_list): |
| 131 | + nonlocal call_count |
| 132 | + call_count += 1 |
| 133 | + return original_create(self_model, vals_list) |
| 134 | + |
| 135 | + # Add a batch tag so we enter the loop |
| 136 | + batch_tag = self.env["spp.payment.batch.tag"].create( |
| 137 | + { |
| 138 | + "name": "Test Tag", |
| 139 | + "order": 1, |
| 140 | + "domain": "[]", |
| 141 | + "max_batch_size": 500, |
| 142 | + } |
| 143 | + ) |
| 144 | + self.payment_manager.batch_tag_ids = [(4, batch_tag.id)] |
| 145 | + |
| 146 | + with patch.object( |
| 147 | + type(self.env["spp.payment"]), |
| 148 | + "create", |
| 149 | + counting_create, |
| 150 | + ): |
| 151 | + self.payment_manager._prepare_payments(self.cycle, self.entitlements) |
| 152 | + |
| 153 | + self.assertEqual( |
| 154 | + call_count, |
| 155 | + 1, |
| 156 | + f"create() should be called once (batch), was called {call_count} times", |
| 157 | + ) |
0 commit comments