|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +"""Tests for aggregation API schemas and endpoint logic.""" |
| 3 | + |
| 4 | +from odoo.tests import tagged |
| 5 | + |
| 6 | +from .common import SimulationApiTestCase |
| 7 | + |
| 8 | + |
| 9 | +@tagged("-at_install", "post_install") |
| 10 | +class TestAggregationApi(SimulationApiTestCase): |
| 11 | + """Test aggregation endpoint schemas and logic.""" |
| 12 | + |
| 13 | + def test_aggregation_scope_request_defaults(self): |
| 14 | + """Test AggregationScopeRequest schema defaults.""" |
| 15 | + from ..schemas.aggregation import AggregationScopeRequest |
| 16 | + |
| 17 | + scope = AggregationScopeRequest() |
| 18 | + self.assertEqual(scope.target_type, "group") |
| 19 | + self.assertIsNone(scope.cel_expression) |
| 20 | + self.assertIsNone(scope.area_id) |
| 21 | + |
| 22 | + def test_aggregation_scope_request_with_values(self): |
| 23 | + """Test AggregationScopeRequest with explicit values.""" |
| 24 | + from ..schemas.aggregation import AggregationScopeRequest |
| 25 | + |
| 26 | + scope = AggregationScopeRequest( |
| 27 | + target_type="individual", |
| 28 | + cel_expression="r.age >= 18", |
| 29 | + area_id=42, |
| 30 | + ) |
| 31 | + self.assertEqual(scope.target_type, "individual") |
| 32 | + self.assertEqual(scope.cel_expression, "r.age >= 18") |
| 33 | + self.assertEqual(scope.area_id, 42) |
| 34 | + |
| 35 | + def test_compute_aggregation_request_minimal(self): |
| 36 | + """Test ComputeAggregationRequest with minimal fields.""" |
| 37 | + from ..schemas.aggregation import ( |
| 38 | + AggregationScopeRequest, |
| 39 | + ComputeAggregationRequest, |
| 40 | + ) |
| 41 | + |
| 42 | + request = ComputeAggregationRequest( |
| 43 | + scope=AggregationScopeRequest(), |
| 44 | + ) |
| 45 | + self.assertEqual(request.scope.target_type, "group") |
| 46 | + self.assertIsNone(request.statistics) |
| 47 | + self.assertIsNone(request.group_by) |
| 48 | + |
| 49 | + def test_compute_aggregation_request_full(self): |
| 50 | + """Test ComputeAggregationRequest with all fields.""" |
| 51 | + from ..schemas.aggregation import ( |
| 52 | + AggregationScopeRequest, |
| 53 | + ComputeAggregationRequest, |
| 54 | + ) |
| 55 | + |
| 56 | + request = ComputeAggregationRequest( |
| 57 | + scope=AggregationScopeRequest( |
| 58 | + target_type="individual", |
| 59 | + cel_expression="r.age >= 60", |
| 60 | + ), |
| 61 | + statistics=["count", "average_age"], |
| 62 | + group_by=["gender", "area"], |
| 63 | + ) |
| 64 | + self.assertEqual(request.scope.target_type, "individual") |
| 65 | + self.assertEqual(len(request.statistics), 2) |
| 66 | + self.assertEqual(len(request.group_by), 2) |
| 67 | + |
| 68 | + def test_aggregation_response_schema(self): |
| 69 | + """Test AggregationResponse schema.""" |
| 70 | + from ..schemas.aggregation import AggregationResponse |
| 71 | + |
| 72 | + response = AggregationResponse( |
| 73 | + total_count=150, |
| 74 | + statistics={"average_age": 45.2, "count": 150}, |
| 75 | + breakdown={"gender": {"male": 70, "female": 80}}, |
| 76 | + from_cache=False, |
| 77 | + computed_at="2024-01-01T12:00:00Z", |
| 78 | + access_level="aggregate", |
| 79 | + ) |
| 80 | + self.assertEqual(response.total_count, 150) |
| 81 | + self.assertEqual(response.statistics["average_age"], 45.2) |
| 82 | + self.assertIn("gender", response.breakdown) |
| 83 | + self.assertFalse(response.from_cache) |
| 84 | + |
| 85 | + def test_aggregation_response_no_breakdown(self): |
| 86 | + """Test AggregationResponse without breakdown.""" |
| 87 | + from ..schemas.aggregation import AggregationResponse |
| 88 | + |
| 89 | + response = AggregationResponse( |
| 90 | + total_count=100, |
| 91 | + statistics={}, |
| 92 | + from_cache=True, |
| 93 | + computed_at="2024-01-01T12:00:00Z", |
| 94 | + access_level="aggregate", |
| 95 | + ) |
| 96 | + self.assertIsNone(response.breakdown) |
| 97 | + self.assertTrue(response.from_cache) |
| 98 | + |
| 99 | + def test_dimension_info_schema(self): |
| 100 | + """Test DimensionInfo schema.""" |
| 101 | + from ..schemas.aggregation import DimensionInfo |
| 102 | + |
| 103 | + dim = DimensionInfo( |
| 104 | + name="gender", |
| 105 | + label="Gender", |
| 106 | + description="Biological sex", |
| 107 | + dimension_type="field", |
| 108 | + applies_to="all", |
| 109 | + value_labels={"male": "Male", "female": "Female"}, |
| 110 | + ) |
| 111 | + self.assertEqual(dim.name, "gender") |
| 112 | + self.assertEqual(dim.label, "Gender") |
| 113 | + self.assertEqual(dim.dimension_type, "field") |
| 114 | + self.assertEqual(dim.applies_to, "all") |
| 115 | + self.assertIn("male", dim.value_labels) |
| 116 | + |
| 117 | + def test_dimension_info_minimal(self): |
| 118 | + """Test DimensionInfo with no optional fields.""" |
| 119 | + from ..schemas.aggregation import DimensionInfo |
| 120 | + |
| 121 | + dim = DimensionInfo( |
| 122 | + name="custom_dim", |
| 123 | + label="Custom", |
| 124 | + dimension_type="expression", |
| 125 | + applies_to="individuals", |
| 126 | + ) |
| 127 | + self.assertIsNone(dim.description) |
| 128 | + self.assertIsNone(dim.value_labels) |
| 129 | + |
| 130 | + def test_dimensions_list_response_schema(self): |
| 131 | + """Test DimensionsListResponse schema.""" |
| 132 | + from ..schemas.aggregation import DimensionInfo, DimensionsListResponse |
| 133 | + |
| 134 | + dims = [ |
| 135 | + DimensionInfo( |
| 136 | + name="gender", |
| 137 | + label="Gender", |
| 138 | + dimension_type="field", |
| 139 | + applies_to="all", |
| 140 | + ), |
| 141 | + DimensionInfo( |
| 142 | + name="age_group", |
| 143 | + label="Age Group", |
| 144 | + dimension_type="expression", |
| 145 | + applies_to="individuals", |
| 146 | + ), |
| 147 | + ] |
| 148 | + response = DimensionsListResponse(dimensions=dims) |
| 149 | + self.assertEqual(len(response.dimensions), 2) |
| 150 | + self.assertEqual(response.dimensions[0].name, "gender") |
| 151 | + self.assertEqual(response.dimensions[1].name, "age_group") |
| 152 | + |
| 153 | + def test_scope_model_dump(self): |
| 154 | + """Test that scope model_dump produces the expected dict.""" |
| 155 | + from ..schemas.aggregation import AggregationScopeRequest |
| 156 | + |
| 157 | + scope = AggregationScopeRequest( |
| 158 | + target_type="group", |
| 159 | + cel_expression="r.area_id != false", |
| 160 | + area_id=5, |
| 161 | + ) |
| 162 | + dumped = scope.model_dump() |
| 163 | + self.assertEqual(dumped["target_type"], "group") |
| 164 | + self.assertEqual(dumped["cel_expression"], "r.area_id != false") |
| 165 | + self.assertEqual(dumped["area_id"], 5) |
0 commit comments