|
| 1 | +"""Tests for the new economic impact output modules.""" |
| 2 | + |
| 3 | +from dataclasses import FrozenInstanceError |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pandas as pd |
| 8 | +import pytest |
| 9 | +from microdf import MicroDataFrame |
| 10 | + |
| 11 | +from policyengine.outputs.change_aggregate import ChangeAggregate, ChangeAggregateType |
| 12 | +from policyengine.outputs.country_config import UK_CONFIG, US_CONFIG |
| 13 | +from policyengine.outputs.decile_impact import DecileImpact, compute_decile_impacts |
| 14 | + |
| 15 | +# --------------------------------------------------------------------------- |
| 16 | +# Helpers (same pattern as test_intra_decile_impact.py) |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | + |
| 19 | + |
| 20 | +def _make_variable_mock(name: str, entity: str) -> MagicMock: |
| 21 | + """Create a mock Variable with name and entity attributes.""" |
| 22 | + var = MagicMock() |
| 23 | + var.name = name |
| 24 | + var.entity = entity |
| 25 | + return var |
| 26 | + |
| 27 | + |
| 28 | +def _make_sim(household_data: dict, variables: list | None = None) -> MagicMock: |
| 29 | + """Create a mock Simulation with household-level data.""" |
| 30 | + hh_df = MicroDataFrame( |
| 31 | + pd.DataFrame(household_data), |
| 32 | + weights="household_weight", |
| 33 | + ) |
| 34 | + sim = MagicMock() |
| 35 | + sim.output_dataset.data.household = hh_df |
| 36 | + sim.id = "test-sim" |
| 37 | + if variables is not None: |
| 38 | + sim.tax_benefit_model_version.variables = variables |
| 39 | + return sim |
| 40 | + |
| 41 | + |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | +# CountryConfig tests |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | + |
| 46 | + |
| 47 | +def test_us_config_is_frozen(): |
| 48 | + """US_CONFIG should be immutable.""" |
| 49 | + with pytest.raises(FrozenInstanceError): |
| 50 | + US_CONFIG.country_id = "uk" |
| 51 | + |
| 52 | + |
| 53 | +def test_uk_config_is_frozen(): |
| 54 | + """UK_CONFIG should be immutable.""" |
| 55 | + with pytest.raises(FrozenInstanceError): |
| 56 | + UK_CONFIG.country_id = "us" |
| 57 | + |
| 58 | + |
| 59 | +def test_us_config_has_correct_country_id(): |
| 60 | + assert US_CONFIG.country_id == "us" |
| 61 | + |
| 62 | + |
| 63 | +def test_uk_config_has_correct_country_id(): |
| 64 | + assert UK_CONFIG.country_id == "uk" |
| 65 | + |
| 66 | + |
| 67 | +def test_us_config_programs(): |
| 68 | + """US_CONFIG should contain expected program keys.""" |
| 69 | + expected = { |
| 70 | + "income_tax", |
| 71 | + "employee_payroll_tax", |
| 72 | + "snap", |
| 73 | + "tanf", |
| 74 | + "ssi", |
| 75 | + "social_security", |
| 76 | + } |
| 77 | + assert set(US_CONFIG.programs.keys()) == expected |
| 78 | + |
| 79 | + |
| 80 | +def test_uk_config_programs(): |
| 81 | + """UK_CONFIG should contain expected programme keys.""" |
| 82 | + expected = { |
| 83 | + "income_tax", |
| 84 | + "national_insurance", |
| 85 | + "vat", |
| 86 | + "council_tax", |
| 87 | + "universal_credit", |
| 88 | + "child_benefit", |
| 89 | + "pension_credit", |
| 90 | + "income_support", |
| 91 | + "working_tax_credit", |
| 92 | + "child_tax_credit", |
| 93 | + } |
| 94 | + assert set(UK_CONFIG.programs.keys()) == expected |
| 95 | + |
| 96 | + |
| 97 | +def test_country_config_program_structure(): |
| 98 | + """Each program entry should have 'entity' and 'is_tax' keys.""" |
| 99 | + for name, info in US_CONFIG.programs.items(): |
| 100 | + assert "entity" in info, f"US program {name} missing 'entity'" |
| 101 | + assert "is_tax" in info, f"US program {name} missing 'is_tax'" |
| 102 | + for name, info in UK_CONFIG.programs.items(): |
| 103 | + assert "entity" in info, f"UK programme {name} missing 'entity'" |
| 104 | + assert "is_tax" in info, f"UK programme {name} missing 'is_tax'" |
| 105 | + |
| 106 | + |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | +# DecileImpact tests |
| 109 | +# --------------------------------------------------------------------------- |
| 110 | + |
| 111 | + |
| 112 | +def test_decile_impact_variable_not_found(): |
| 113 | + """DecileImpact.run() should raise ValueError for a nonexistent variable.""" |
| 114 | + variables = [_make_variable_mock("household_net_income", "household")] |
| 115 | + sim = _make_sim( |
| 116 | + {"household_net_income": [50000.0], "household_weight": [1.0]}, |
| 117 | + variables=variables, |
| 118 | + ) |
| 119 | + |
| 120 | + di = DecileImpact.model_construct( |
| 121 | + baseline_simulation=sim, |
| 122 | + reform_simulation=sim, |
| 123 | + income_variable="nonexistent_variable", |
| 124 | + entity="household", |
| 125 | + decile=1, |
| 126 | + ) |
| 127 | + with pytest.raises(ValueError, match="not found in model"): |
| 128 | + di.run() |
| 129 | + |
| 130 | + |
| 131 | +def test_compute_decile_impacts_returns_10(): |
| 132 | + """compute_decile_impacts should return 10 DecileImpact objects by default.""" |
| 133 | + n = 100 |
| 134 | + incomes = np.linspace(10000, 100000, n) |
| 135 | + reform_incomes = incomes + 500 |
| 136 | + variables = [_make_variable_mock("household_net_income", "household")] |
| 137 | + |
| 138 | + baseline = _make_sim( |
| 139 | + {"household_net_income": incomes, "household_weight": np.ones(n)}, |
| 140 | + variables=variables, |
| 141 | + ) |
| 142 | + reform = _make_sim( |
| 143 | + {"household_net_income": reform_incomes, "household_weight": np.ones(n)}, |
| 144 | + variables=variables, |
| 145 | + ) |
| 146 | + |
| 147 | + result = compute_decile_impacts( |
| 148 | + baseline, reform, income_variable="household_net_income", entity="household" |
| 149 | + ) |
| 150 | + |
| 151 | + assert len(result.outputs) == 10 |
| 152 | + assert len(result.dataframe) == 10 |
| 153 | + |
| 154 | + # Each decile should have absolute_change ~500 |
| 155 | + for di in result.outputs: |
| 156 | + assert abs(di.absolute_change - 500.0) < 1e-6 |
| 157 | + |
| 158 | + |
| 159 | +def test_compute_decile_impacts_custom_quantiles(): |
| 160 | + """compute_decile_impacts with quantiles=5 should return 5 outputs.""" |
| 161 | + n = 100 |
| 162 | + incomes = np.linspace(10000, 100000, n) |
| 163 | + variables = [_make_variable_mock("household_net_income", "household")] |
| 164 | + |
| 165 | + sim = _make_sim( |
| 166 | + {"household_net_income": incomes, "household_weight": np.ones(n)}, |
| 167 | + variables=variables, |
| 168 | + ) |
| 169 | + |
| 170 | + result = compute_decile_impacts( |
| 171 | + sim, |
| 172 | + sim, |
| 173 | + income_variable="household_net_income", |
| 174 | + entity="household", |
| 175 | + quantiles=5, |
| 176 | + ) |
| 177 | + |
| 178 | + assert len(result.outputs) == 5 |
| 179 | + |
| 180 | + |
| 181 | +# --------------------------------------------------------------------------- |
| 182 | +# ChangeAggregate error test |
| 183 | +# --------------------------------------------------------------------------- |
| 184 | + |
| 185 | + |
| 186 | +def test_change_aggregate_variable_not_found(): |
| 187 | + """ChangeAggregate should raise ValueError for a nonexistent variable.""" |
| 188 | + variables = [_make_variable_mock("employment_income", "person")] |
| 189 | + sim = _make_sim( |
| 190 | + {"household_net_income": [50000.0], "household_weight": [1.0]}, |
| 191 | + variables=variables, |
| 192 | + ) |
| 193 | + |
| 194 | + ca = ChangeAggregate.model_construct( |
| 195 | + baseline_simulation=sim, |
| 196 | + reform_simulation=sim, |
| 197 | + variable="nonexistent_variable", |
| 198 | + aggregate_type=ChangeAggregateType.COUNT, |
| 199 | + ) |
| 200 | + with pytest.raises(ValueError, match="not found in model"): |
| 201 | + ca.run() |
0 commit comments