|
| 1 | +"""Regression tests for `policyengine_uk_data.datasets.spi`. |
| 2 | +
|
| 3 | +Covers the three bugs flagged in the bug hunt: |
| 4 | +
|
| 5 | +- The ``__main__`` block called ``create_spi`` with two positional args but |
| 6 | + the signature required three. This test asserts the function is callable |
| 7 | + with two positional args (``spi_data_file_path`` and ``fiscal_year``) and |
| 8 | + that the optional ``output_file_path`` kwarg is accepted. |
| 9 | +- Age imputation was non-deterministic (unseeded ``np.random.rand``). This |
| 10 | + test asserts two runs with the same seed produce identical ``age`` |
| 11 | + columns. |
| 12 | +- Unknown GORCODE values were silently mapped to ``SOUTH_EAST``. This test |
| 13 | + asserts the default fallback label is now ``UNKNOWN``. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import importlib.util |
| 19 | +import inspect |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import pandas as pd |
| 23 | +import pytest |
| 24 | + |
| 25 | +if importlib.util.find_spec("policyengine_uk") is None: |
| 26 | + pytest.skip( |
| 27 | + "policyengine_uk not available in test environment", |
| 28 | + allow_module_level=True, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +SPI_COLUMNS = [ |
| 33 | + "SREF", |
| 34 | + "FACT", |
| 35 | + "DIVIDENDS", |
| 36 | + "GIFTAID", |
| 37 | + "GORCODE", |
| 38 | + "INCBBS", |
| 39 | + "INCPROP", |
| 40 | + "PAY", |
| 41 | + "EPB", |
| 42 | + "EXPS", |
| 43 | + "PENSION", |
| 44 | + "PSAV_XS", |
| 45 | + "PENSRLF", |
| 46 | + "PROFITS", |
| 47 | + "CAPALL", |
| 48 | + "LOSSBF", |
| 49 | + "AGERANGE", |
| 50 | + "SRP", |
| 51 | + "TAX_CRED", |
| 52 | + "MOTHINC", |
| 53 | + "INCPBEN", |
| 54 | + "OSSBEN", |
| 55 | + "TAXTERM", |
| 56 | + "UBISJA", |
| 57 | + "OTHERINC", |
| 58 | + "GIFTINV", |
| 59 | + "OTHERINV", |
| 60 | + "COVNTS", |
| 61 | + "MOTHDED", |
| 62 | + "DEFICIEN", |
| 63 | + "MCAS", |
| 64 | + "BPADUE", |
| 65 | + "MAIND", |
| 66 | +] |
| 67 | + |
| 68 | + |
| 69 | +def _write_fake_spi(path, gor_values=(1, 2, 3), maind_values=(1, 0, 1)): |
| 70 | + """Write a minimal SPI-shaped tab file for tests. |
| 71 | +
|
| 72 | + The real SPI file has dozens of columns; the test only needs them to |
| 73 | + exist with sensible types so ``create_spi`` can build dataframes. |
| 74 | + """ |
| 75 | + n = len(gor_values) |
| 76 | + data = {col: np.zeros(n, dtype=float) for col in SPI_COLUMNS} |
| 77 | + data["SREF"] = np.arange(1, n + 1) |
| 78 | + data["FACT"] = np.ones(n) |
| 79 | + data["GORCODE"] = list(gor_values) |
| 80 | + data["MAIND"] = list(maind_values) |
| 81 | + data["AGERANGE"] = [1] * n # bucket (16, 25) |
| 82 | + df = pd.DataFrame(data) |
| 83 | + df.to_csv(path, sep="\t", index=False) |
| 84 | + |
| 85 | + |
| 86 | +def test_create_spi_accepts_two_positional_args(tmp_path): |
| 87 | + """The ``__main__`` crash bug: ``create_spi(path, year)`` must work.""" |
| 88 | + from policyengine_uk_data.datasets.spi import create_spi |
| 89 | + |
| 90 | + sig = inspect.signature(create_spi) |
| 91 | + params = list(sig.parameters.values()) |
| 92 | + # First two params are required positional; remaining params are optional |
| 93 | + # so two-arg calls succeed. |
| 94 | + assert params[0].default is inspect.Parameter.empty |
| 95 | + assert params[1].default is inspect.Parameter.empty |
| 96 | + for p in params[2:]: |
| 97 | + assert p.default is not inspect.Parameter.empty, ( |
| 98 | + f"Parameter {p.name!r} must have a default so create_spi(path, " |
| 99 | + f"year) stays callable without breaking the __main__ block." |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def test_create_spi_age_imputation_is_deterministic(tmp_path): |
| 104 | + """Same seed → identical age column. Was unseeded in the buggy version.""" |
| 105 | + from policyengine_uk_data.datasets.spi import create_spi |
| 106 | + |
| 107 | + tab = tmp_path / "spi.tab" |
| 108 | + _write_fake_spi(tab, gor_values=(1, 2, 3, 4, 5), maind_values=(0, 0, 0, 0, 0)) |
| 109 | + |
| 110 | + ds_a = create_spi(tab, 2020, seed=42) |
| 111 | + ds_b = create_spi(tab, 2020, seed=42) |
| 112 | + ds_c = create_spi(tab, 2020, seed=123) |
| 113 | + |
| 114 | + assert (ds_a.person["age"].to_numpy() == ds_b.person["age"].to_numpy()).all() |
| 115 | + # Different seeds should give some variation for the (16, 25) bucket. |
| 116 | + assert not (ds_a.person["age"].to_numpy() == ds_c.person["age"].to_numpy()).all() |
| 117 | + |
| 118 | + |
| 119 | +def test_create_spi_unknown_gorcode_does_not_silently_become_south_east( |
| 120 | + tmp_path, |
| 121 | +): |
| 122 | + """Unmapped GORCODE rows now get UNKNOWN, not SOUTH_EAST, by default.""" |
| 123 | + from policyengine_uk_data.datasets.spi import create_spi |
| 124 | + |
| 125 | + tab = tmp_path / "spi.tab" |
| 126 | + _write_fake_spi( |
| 127 | + tab, |
| 128 | + gor_values=(99, 7, 99), # 99 is undocumented → should be UNKNOWN |
| 129 | + maind_values=(0, 0, 0), |
| 130 | + ) |
| 131 | + |
| 132 | + ds = create_spi(tab, 2020, seed=0) |
| 133 | + regions = ds.household["region"].tolist() |
| 134 | + assert regions[0] == "UNKNOWN" |
| 135 | + assert regions[1] == "LONDON" # GORCODE 7 maps to LONDON |
| 136 | + assert regions[2] == "UNKNOWN" |
| 137 | + # Legacy behaviour is still accessible via the kwarg for callers that |
| 138 | + # relied on it. |
| 139 | + ds_legacy = create_spi(tab, 2020, seed=0, unknown_region="SOUTH_EAST") |
| 140 | + assert ds_legacy.household["region"].tolist()[0] == "SOUTH_EAST" |
| 141 | + |
| 142 | + |
| 143 | +def test_create_spi_marriage_allowance_uses_fiscal_year_parameters(tmp_path): |
| 144 | + """MA cap should follow the fiscal year's 10% × Personal Allowance rule. |
| 145 | +
|
| 146 | + 2020-21 PA = £12,500 so MA cap = £1,250 (the historical hardcoded value). |
| 147 | + 2021-22 onwards PA = £12,570 so MA cap = £1,257, rounded down to |
| 148 | + increments per the rounding_increment parameter (HMRC publishes £1,260 |
| 149 | + for 2025-26). |
| 150 | + """ |
| 151 | + from policyengine_uk_data.datasets.spi import create_spi |
| 152 | + |
| 153 | + tab = tmp_path / "spi.tab" |
| 154 | + _write_fake_spi(tab, gor_values=(1, 2, 3), maind_values=(1, 0, 1)) |
| 155 | + |
| 156 | + ds_2020 = create_spi(tab, 2020, seed=0) |
| 157 | + marriage_2020 = ds_2020.person["marriage_allowance"].to_numpy() |
| 158 | + # Expect eligible rows (MAIND == 1) to receive £1,250 and ineligible 0. |
| 159 | + assert (marriage_2020[[0, 2]] == 1_250).all() |
| 160 | + assert marriage_2020[1] == 0 |
| 161 | + |
| 162 | + ds_2025 = create_spi(tab, 2025, seed=0) |
| 163 | + marriage_2025 = ds_2025.person["marriage_allowance"].to_numpy() |
| 164 | + # Post-2020, PA is £12,570 so the cap is £1,257 before rounding; the |
| 165 | + # published HMRC value is £1,260 (rounding to nearest £10). Accept |
| 166 | + # either, but require it's NOT the stale 2020-21 £1,250 figure. |
| 167 | + assert marriage_2025[0] != 1_250 |
| 168 | + assert marriage_2025[0] >= 1_250 # PA has only risen since 2020 |
0 commit comments