|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +from odoo.tests import TransactionCase |
| 3 | + |
| 4 | + |
| 5 | +class TestCompositeIndexes(TransactionCase): |
| 6 | + """Test that composite indexes exist for frequent query patterns.""" |
| 7 | + |
| 8 | + def test_entitlement_cycle_partner_index_exists(self): |
| 9 | + """Composite index on spp_entitlement(cycle_id, partner_id) must exist. |
| 10 | +
|
| 11 | + The prepare_entitlements duplicate check searches entitlements by |
| 12 | + (cycle_id, partner_id). Without this index, each batch does a |
| 13 | + sequential scan. |
| 14 | + """ |
| 15 | + self.env.cr.execute( |
| 16 | + """ |
| 17 | + SELECT 1 FROM pg_indexes |
| 18 | + WHERE tablename = 'spp_entitlement' |
| 19 | + AND indexdef LIKE '%%cycle_id%%' |
| 20 | + AND indexdef LIKE '%%partner_id%%' |
| 21 | + """ |
| 22 | + ) |
| 23 | + self.assertTrue( |
| 24 | + self.env.cr.fetchone(), |
| 25 | + "Composite index on (cycle_id, partner_id) must exist on spp_entitlement", |
| 26 | + ) |
| 27 | + |
| 28 | + def test_entitlement_cycle_state_index_exists(self): |
| 29 | + """Composite index on spp_entitlement(cycle_id, state) must exist. |
| 30 | +
|
| 31 | + Cycle computed fields (total_amount, show_approve_button, |
| 32 | + entitlements_count) filter entitlements by cycle_id and state. |
| 33 | + """ |
| 34 | + self.env.cr.execute( |
| 35 | + """ |
| 36 | + SELECT 1 FROM pg_indexes |
| 37 | + WHERE tablename = 'spp_entitlement' |
| 38 | + AND indexdef LIKE '%%cycle_id%%' |
| 39 | + AND indexdef LIKE '%%state%%' |
| 40 | + """ |
| 41 | + ) |
| 42 | + self.assertTrue( |
| 43 | + self.env.cr.fetchone(), |
| 44 | + "Composite index on (cycle_id, state) must exist on spp_entitlement", |
| 45 | + ) |
| 46 | + |
| 47 | + def test_program_membership_program_state_index_exists(self): |
| 48 | + """Composite index on spp_program_membership(program_id, state) must exist. |
| 49 | +
|
| 50 | + get_beneficiaries() and count_beneficiaries() filter by |
| 51 | + (program_id, state) on every async batch dispatch. |
| 52 | + """ |
| 53 | + self.env.cr.execute( |
| 54 | + """ |
| 55 | + SELECT 1 FROM pg_indexes |
| 56 | + WHERE tablename = 'spp_program_membership' |
| 57 | + AND indexdef LIKE '%%program_id%%' |
| 58 | + AND indexdef LIKE '%%state%%' |
| 59 | + """ |
| 60 | + ) |
| 61 | + self.assertTrue( |
| 62 | + self.env.cr.fetchone(), |
| 63 | + "Composite index on (program_id, state) must exist on spp_program_membership", |
| 64 | + ) |
0 commit comments