|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +import uuid |
| 3 | + |
| 4 | +from odoo import fields |
| 5 | +from odoo.tests import TransactionCase |
| 6 | + |
| 7 | + |
| 8 | +class TestCycleComputedFields(TransactionCase): |
| 9 | + """Test that SQL-optimized cycle computed fields return correct results. |
| 10 | +
|
| 11 | + These fields were migrated from Python iteration over recordsets to |
| 12 | + SQL aggregation for O(1) instead of O(N) per cycle. |
| 13 | + """ |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + super().setUp() |
| 17 | + self.program = self.env["spp.program"].create({"name": f"Test Program {uuid.uuid4().hex[:8]}"}) |
| 18 | + self.cycle = self.env["spp.cycle"].create( |
| 19 | + { |
| 20 | + "name": "Test Cycle", |
| 21 | + "program_id": self.program.id, |
| 22 | + "start_date": fields.Date.today(), |
| 23 | + "end_date": fields.Date.today(), |
| 24 | + } |
| 25 | + ) |
| 26 | + self.registrant1 = self.env["res.partner"].create({"name": "Registrant 1", "is_registrant": True}) |
| 27 | + self.registrant2 = self.env["res.partner"].create({"name": "Registrant 2", "is_registrant": True}) |
| 28 | + self.registrant3 = self.env["res.partner"].create({"name": "Registrant 3", "is_registrant": True}) |
| 29 | + |
| 30 | + def _create_entitlement(self, partner, amount, state="draft"): |
| 31 | + ent = self.env["spp.entitlement"].create( |
| 32 | + { |
| 33 | + "partner_id": partner.id, |
| 34 | + "cycle_id": self.cycle.id, |
| 35 | + "initial_amount": amount, |
| 36 | + } |
| 37 | + ) |
| 38 | + if state != "draft": |
| 39 | + ent.write({"state": state}) |
| 40 | + return ent |
| 41 | + |
| 42 | + # -- total_amount -- |
| 43 | + |
| 44 | + def test_total_amount_empty(self): |
| 45 | + """Cycle with no entitlements has total_amount = 0.""" |
| 46 | + self.cycle.invalidate_recordset(["total_amount"]) |
| 47 | + self.assertEqual(self.cycle.total_amount, 0) |
| 48 | + |
| 49 | + def test_total_amount_sums_initial_amounts(self): |
| 50 | + """total_amount must equal sum of all entitlement initial_amounts.""" |
| 51 | + self._create_entitlement(self.registrant1, 100.0) |
| 52 | + self._create_entitlement(self.registrant2, 250.50) |
| 53 | + self._create_entitlement(self.registrant3, 49.50) |
| 54 | + self.cycle.invalidate_recordset(["total_amount"]) |
| 55 | + self.assertAlmostEqual(self.cycle.total_amount, 400.0) |
| 56 | + |
| 57 | + # -- total_entitlements_count -- |
| 58 | + |
| 59 | + def test_total_entitlements_count_empty(self): |
| 60 | + """Cycle with no entitlements has count = 0.""" |
| 61 | + self.cycle.invalidate_recordset(["total_entitlements_count"]) |
| 62 | + self.assertEqual(self.cycle.total_entitlements_count, 0) |
| 63 | + |
| 64 | + def test_total_entitlements_count_correct(self): |
| 65 | + """Count must include all cash entitlements.""" |
| 66 | + self._create_entitlement(self.registrant1, 100.0) |
| 67 | + self._create_entitlement(self.registrant2, 200.0) |
| 68 | + self.cycle.invalidate_recordset(["total_entitlements_count"]) |
| 69 | + self.assertEqual(self.cycle.total_entitlements_count, 2) |
| 70 | + |
| 71 | + # -- show_approve_entitlements_button -- |
| 72 | + |
| 73 | + def test_show_approve_no_entitlements(self): |
| 74 | + """Button hidden when no entitlements exist.""" |
| 75 | + self.cycle.invalidate_recordset(["show_approve_entitlements_button"]) |
| 76 | + self.assertFalse(self.cycle.show_approve_entitlements_button) |
| 77 | + |
| 78 | + def test_show_approve_with_pending(self): |
| 79 | + """Button shown when pending_validation entitlements exist.""" |
| 80 | + self._create_entitlement(self.registrant1, 100.0, state="pending_validation") |
| 81 | + self.cycle.invalidate_recordset(["show_approve_entitlements_button"]) |
| 82 | + self.assertTrue(self.cycle.show_approve_entitlements_button) |
| 83 | + |
| 84 | + def test_show_approve_only_draft(self): |
| 85 | + """Button hidden when all entitlements are draft.""" |
| 86 | + self._create_entitlement(self.registrant1, 100.0, state="draft") |
| 87 | + self.cycle.invalidate_recordset(["show_approve_entitlements_button"]) |
| 88 | + self.assertFalse(self.cycle.show_approve_entitlements_button) |
| 89 | + |
| 90 | + def test_show_approve_only_approved(self): |
| 91 | + """Button hidden when all entitlements are approved.""" |
| 92 | + self._create_entitlement(self.registrant1, 100.0, state="approved") |
| 93 | + self.cycle.invalidate_recordset(["show_approve_entitlements_button"]) |
| 94 | + self.assertFalse(self.cycle.show_approve_entitlements_button) |
| 95 | + |
| 96 | + # -- all_entitlements_approved -- |
| 97 | + |
| 98 | + def test_all_approved_empty(self): |
| 99 | + """No entitlements => not all approved (nothing to approve).""" |
| 100 | + self.cycle.invalidate_recordset(["all_entitlements_approved"]) |
| 101 | + self.assertFalse(self.cycle.all_entitlements_approved) |
| 102 | + |
| 103 | + def test_all_approved_when_all_approved(self): |
| 104 | + """True when every entitlement has state=approved.""" |
| 105 | + self._create_entitlement(self.registrant1, 100.0, state="approved") |
| 106 | + self._create_entitlement(self.registrant2, 200.0, state="approved") |
| 107 | + self.cycle.invalidate_recordset(["all_entitlements_approved"]) |
| 108 | + self.assertTrue(self.cycle.all_entitlements_approved) |
| 109 | + |
| 110 | + def test_all_approved_mixed_states(self): |
| 111 | + """False when some entitlements are not approved.""" |
| 112 | + self._create_entitlement(self.registrant1, 100.0, state="approved") |
| 113 | + self._create_entitlement(self.registrant2, 200.0, state="draft") |
| 114 | + self.cycle.invalidate_recordset(["all_entitlements_approved"]) |
| 115 | + self.assertFalse(self.cycle.all_entitlements_approved) |
| 116 | + |
| 117 | + # -- multi-cycle batching -- |
| 118 | + |
| 119 | + def test_computed_fields_multi_cycle(self): |
| 120 | + """SQL queries must handle multiple cycles in a single batch.""" |
| 121 | + cycle2 = self.env["spp.cycle"].create( |
| 122 | + { |
| 123 | + "name": "Test Cycle 2", |
| 124 | + "program_id": self.program.id, |
| 125 | + "start_date": fields.Date.today(), |
| 126 | + "end_date": fields.Date.today(), |
| 127 | + } |
| 128 | + ) |
| 129 | + self._create_entitlement(self.registrant1, 100.0) |
| 130 | + self.env["spp.entitlement"].create( |
| 131 | + { |
| 132 | + "partner_id": self.registrant2.id, |
| 133 | + "cycle_id": cycle2.id, |
| 134 | + "initial_amount": 300.0, |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + cycles = self.cycle | cycle2 |
| 139 | + cycles.invalidate_recordset(["total_amount", "total_entitlements_count"]) |
| 140 | + |
| 141 | + self.assertAlmostEqual(self.cycle.total_amount, 100.0) |
| 142 | + self.assertAlmostEqual(cycle2.total_amount, 300.0) |
| 143 | + self.assertEqual(self.cycle.total_entitlements_count, 1) |
| 144 | + self.assertEqual(cycle2.total_entitlements_count, 1) |
0 commit comments