|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | + |
| 3 | + |
| 4 | +from odoo.exceptions import ValidationError |
| 5 | +from odoo.tests import tagged |
| 6 | +from odoo.tests.common import TransactionCase |
| 7 | + |
| 8 | + |
| 9 | +@tagged("post_install", "-at_install") |
| 10 | +class TestGISReportProgramsModel(TransactionCase): |
| 11 | + """Test GIS Report extension for program context filtering.""" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + super().setUpClass() |
| 16 | + cls.Report = cls.env["spp.gis.report"] |
| 17 | + cls.Program = cls.env["spp.program"] |
| 18 | + |
| 19 | + cls.program = cls.Program.create( |
| 20 | + { |
| 21 | + "name": "Test Program for GIS", |
| 22 | + "target_type": "group", |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + # Get source model for res.partner |
| 27 | + cls.partner_model = cls.env["ir.model"].search([("model", "=", "res.partner")], limit=1) |
| 28 | + |
| 29 | + def _make_report_vals(self, **overrides): |
| 30 | + """Build minimal vals dict for a spp.gis.report record.""" |
| 31 | + vals = { |
| 32 | + "name": "Test Report", |
| 33 | + "code": "TEST_RPT_001", |
| 34 | + "source_model_id": self.partner_model.id, |
| 35 | + "area_field_path": "area_id", |
| 36 | + "filter_mode": "domain", |
| 37 | + "aggregation_method": "count", |
| 38 | + "base_area_level": 2, |
| 39 | + "normalization_method": "raw", |
| 40 | + "threshold_mode": "auto_quartile", |
| 41 | + "refresh_mode": "manual", |
| 42 | + "geometry_type": "polygon", |
| 43 | + } |
| 44 | + vals.update(overrides) |
| 45 | + return vals |
| 46 | + |
| 47 | + def test_program_id_field_exists(self): |
| 48 | + """Test that program_id field is added to spp.gis.report.""" |
| 49 | + field_info = self.Report.fields_get(["program_id"]) |
| 50 | + self.assertIn("program_id", field_info) |
| 51 | + self.assertEqual(field_info["program_id"]["relation"], "spp.program") |
| 52 | + |
| 53 | + def test_report_create_with_program(self): |
| 54 | + """Test creating a report with program_id set.""" |
| 55 | + report = self.Report.create(self._make_report_vals(program_id=self.program.id)) |
| 56 | + self.assertEqual(report.program_id, self.program) |
| 57 | + |
| 58 | + def test_report_create_without_program(self): |
| 59 | + """Test creating a report without program_id.""" |
| 60 | + report = self.Report.create(self._make_report_vals()) |
| 61 | + self.assertFalse(report.program_id) |
| 62 | + |
| 63 | + def test_apply_context_filters_with_program(self): |
| 64 | + """Test _apply_context_filters adds program enrollment domain.""" |
| 65 | + report = self.Report.new( |
| 66 | + { |
| 67 | + "program_id": self.program.id, |
| 68 | + "source_model": "res.partner", |
| 69 | + } |
| 70 | + ) |
| 71 | + domain = [("is_registrant", "=", True)] |
| 72 | + result = report._apply_context_filters(domain) |
| 73 | + |
| 74 | + # Should contain the original domain + program membership filter |
| 75 | + flat = str(result) |
| 76 | + self.assertIn("program_membership_ids.program_id", flat) |
| 77 | + |
| 78 | + def test_apply_context_filters_without_program(self): |
| 79 | + """Test _apply_context_filters is a no-op without program_id.""" |
| 80 | + report = self.Report.new( |
| 81 | + { |
| 82 | + "source_model": "res.partner", |
| 83 | + } |
| 84 | + ) |
| 85 | + domain = [("is_registrant", "=", True)] |
| 86 | + result = report._apply_context_filters(domain) |
| 87 | + self.assertEqual(result, domain) |
| 88 | + |
| 89 | + def test_apply_context_filters_non_partner_model(self): |
| 90 | + """Test _apply_context_filters skips filter for non-partner models.""" |
| 91 | + report = self.Report.new( |
| 92 | + { |
| 93 | + "program_id": self.program.id, |
| 94 | + "source_model": "spp.area", |
| 95 | + } |
| 96 | + ) |
| 97 | + domain = [("active", "=", True)] |
| 98 | + result = report._apply_context_filters(domain) |
| 99 | + # Should NOT add program filter since source_model is not res.partner |
| 100 | + self.assertEqual(result, domain) |
| 101 | + |
| 102 | + |
| 103 | +@tagged("post_install", "-at_install") |
| 104 | +class TestGISReportWizardPrograms(TransactionCase): |
| 105 | + """Test GIS Report Wizard extension for program context.""" |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def setUpClass(cls): |
| 109 | + super().setUpClass() |
| 110 | + cls.Wizard = cls.env["spp.gis.report.wizard"] |
| 111 | + cls.Program = cls.env["spp.program"] |
| 112 | + |
| 113 | + cls.program = cls.Program.create( |
| 114 | + { |
| 115 | + "name": "Wizard Test Program", |
| 116 | + "target_type": "group", |
| 117 | + } |
| 118 | + ) |
| 119 | + |
| 120 | + def test_wizard_program_id_field(self): |
| 121 | + """Test that program_id field exists on the wizard.""" |
| 122 | + field_info = self.Wizard.fields_get(["program_id"]) |
| 123 | + self.assertIn("program_id", field_info) |
| 124 | + self.assertEqual(field_info["program_id"]["relation"], "spp.program") |
| 125 | + |
| 126 | + def test_validate_context_requirements_program_required(self): |
| 127 | + """Test validation when template requires program but none set.""" |
| 128 | + wizard = self.Wizard.new( |
| 129 | + { |
| 130 | + "template_requires_program": True, |
| 131 | + "program_id": False, |
| 132 | + } |
| 133 | + ) |
| 134 | + with self.assertRaises(ValidationError): |
| 135 | + wizard._validate_context_requirements() |
| 136 | + |
| 137 | + def test_validate_context_requirements_program_provided(self): |
| 138 | + """Test validation passes when required program is provided.""" |
| 139 | + wizard = self.Wizard.new( |
| 140 | + { |
| 141 | + "template_requires_program": True, |
| 142 | + "program_id": self.program.id, |
| 143 | + } |
| 144 | + ) |
| 145 | + # Should not raise |
| 146 | + wizard._validate_context_requirements() |
| 147 | + |
| 148 | + def test_validate_context_requirements_not_required(self): |
| 149 | + """Test validation passes when program is not required.""" |
| 150 | + wizard = self.Wizard.new( |
| 151 | + { |
| 152 | + "template_requires_program": False, |
| 153 | + "program_id": False, |
| 154 | + } |
| 155 | + ) |
| 156 | + # Should not raise |
| 157 | + wizard._validate_context_requirements() |
| 158 | + |
| 159 | + def test_get_context_filter_vals_with_program(self): |
| 160 | + """Test _get_context_filter_vals includes program_id.""" |
| 161 | + wizard = self.Wizard.new( |
| 162 | + { |
| 163 | + "program_id": self.program.id, |
| 164 | + } |
| 165 | + ) |
| 166 | + vals = wizard._get_context_filter_vals() |
| 167 | + self.assertEqual(vals.get("program_id"), self.program.id) |
| 168 | + |
| 169 | + def test_get_context_filter_vals_without_program(self): |
| 170 | + """Test _get_context_filter_vals without program.""" |
| 171 | + wizard = self.Wizard.new( |
| 172 | + { |
| 173 | + "program_id": False, |
| 174 | + } |
| 175 | + ) |
| 176 | + vals = wizard._get_context_filter_vals() |
| 177 | + self.assertNotIn("program_id", vals) |
| 178 | + |
| 179 | + def test_get_context_code_suffix_with_program(self): |
| 180 | + """Test _get_context_code_suffix includes program ID.""" |
| 181 | + wizard = self.Wizard.new( |
| 182 | + { |
| 183 | + "program_id": self.program.id, |
| 184 | + } |
| 185 | + ) |
| 186 | + suffix = wizard._get_context_code_suffix() |
| 187 | + self.assertIn(str(self.program.id), suffix) |
| 188 | + |
| 189 | + def test_get_context_code_suffix_without_program(self): |
| 190 | + """Test _get_context_code_suffix returns empty without program.""" |
| 191 | + wizard = self.Wizard.new( |
| 192 | + { |
| 193 | + "program_id": False, |
| 194 | + } |
| 195 | + ) |
| 196 | + suffix = wizard._get_context_code_suffix() |
| 197 | + self.assertEqual(suffix, "") |
0 commit comments