|
| 1 | +"""Regression test for OBR NIC target parsing. |
| 2 | +
|
| 3 | +Bug-hunt finding U8: `_parse_nics` only covered Class 1 employee and |
| 4 | +employer NICs, omitting Classes 2 (self-employed flat-rate), 3 (voluntary) |
| 5 | +and 4 (self-employed profit-based). Calibration had no target for those |
| 6 | +receipts and pushed self-employment income downward to compensate. |
| 7 | +
|
| 8 | +This test drives the parser with a minimal in-memory openpyxl workbook |
| 9 | +that mimics the OBR EFO Table 3.4 layout, and asserts that targets for |
| 10 | +all five NIC variables are produced. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +from unittest.mock import patch |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +if importlib.util.find_spec("openpyxl") is None: |
| 21 | + pytest.skip("openpyxl not installed", allow_module_level=True) |
| 22 | + |
| 23 | +import openpyxl # noqa: E402 |
| 24 | + |
| 25 | + |
| 26 | +def _build_fake_obr_workbook() -> openpyxl.Workbook: |
| 27 | + """Create a stand-in for OBR EFO receipts with a minimal Table 3.4.""" |
| 28 | + wb = openpyxl.Workbook() |
| 29 | + ws = wb.active |
| 30 | + ws.title = "3.4" |
| 31 | + |
| 32 | + # Column B holds row labels; columns C-I hold FY values in £bn. |
| 33 | + rows = [ |
| 34 | + ("Class 1 Employee NICs", [120.0] * 7), |
| 35 | + ("Class 1 Employer NICs", [140.0] * 7), |
| 36 | + ("Class 2 NICs", [0.3] * 7), |
| 37 | + ("Class 3 NICs", [0.05] * 7), |
| 38 | + ("Class 4 NICs", [4.5] * 7), |
| 39 | + ] |
| 40 | + for row_idx, (label, values) in enumerate(rows, start=2): |
| 41 | + ws.cell(row=row_idx, column=2, value=label) # col B |
| 42 | + for col_idx, value in enumerate(values, start=3): # cols C-I |
| 43 | + ws.cell(row=row_idx, column=col_idx, value=value) |
| 44 | + |
| 45 | + return wb |
| 46 | + |
| 47 | + |
| 48 | +def test_parse_nics_covers_all_five_classes(): |
| 49 | + from policyengine_uk_data.targets.sources import obr as obr_module |
| 50 | + |
| 51 | + wb = _build_fake_obr_workbook() |
| 52 | + |
| 53 | + fake_config = { |
| 54 | + "obr": { |
| 55 | + "vintage": "test", |
| 56 | + "efo_receipts": "https://example.invalid/receipts", |
| 57 | + "efo_expenditure": "https://example.invalid/expenditure", |
| 58 | + } |
| 59 | + } |
| 60 | + with patch.object(obr_module, "load_config", return_value=fake_config): |
| 61 | + targets = obr_module._parse_nics(wb) |
| 62 | + |
| 63 | + names = {t.name for t in targets} |
| 64 | + assert names == { |
| 65 | + "obr/ni_employee", |
| 66 | + "obr/ni_employer", |
| 67 | + "obr/ni_class_2", |
| 68 | + "obr/ni_class_3", |
| 69 | + "obr/ni_class_4", |
| 70 | + } |
| 71 | + |
| 72 | + variables = {t.variable for t in targets} |
| 73 | + # Variable names must match the policyengine-uk variable identifiers so |
| 74 | + # calibration can map them to simulated totals. |
| 75 | + assert variables == { |
| 76 | + "ni_employee", |
| 77 | + "ni_employer", |
| 78 | + "ni_class_2", |
| 79 | + "ni_class_3", |
| 80 | + "ni_class_4", |
| 81 | + } |
| 82 | + |
| 83 | + # Values are scaled by 1e9 (£bn → £) inside _read_row_values. |
| 84 | + class_4_target = next(t for t in targets if t.variable == "ni_class_4") |
| 85 | + assert class_4_target.values[2024] == pytest.approx(4.5e9) |
| 86 | + |
| 87 | + |
| 88 | +def test_parse_nics_tolerates_alt_label_wording(): |
| 89 | + """The parser should accept common wording variants for self-employed rows.""" |
| 90 | + from policyengine_uk_data.targets.sources import obr as obr_module |
| 91 | + |
| 92 | + wb = openpyxl.Workbook() |
| 93 | + ws = wb.active |
| 94 | + ws.title = "3.4" |
| 95 | + |
| 96 | + # Use alternative labels that the parser should still find. |
| 97 | + rows = [ |
| 98 | + ("Class 1 Employee NICs", [100.0] * 7), |
| 99 | + ("Class 1 Employer NICs", [110.0] * 7), |
| 100 | + ("Class 2 Self-Employed NICs", [0.2] * 7), |
| 101 | + ("Class 3 Voluntary NICs", [0.04] * 7), |
| 102 | + ("Class 4 Self-Employed NICs", [4.0] * 7), |
| 103 | + ] |
| 104 | + for row_idx, (label, values) in enumerate(rows, start=2): |
| 105 | + ws.cell(row=row_idx, column=2, value=label) |
| 106 | + for col_idx, value in enumerate(values, start=3): |
| 107 | + ws.cell(row=row_idx, column=col_idx, value=value) |
| 108 | + |
| 109 | + fake_config = { |
| 110 | + "obr": { |
| 111 | + "vintage": "test", |
| 112 | + "efo_receipts": "https://example.invalid/receipts", |
| 113 | + "efo_expenditure": "https://example.invalid/expenditure", |
| 114 | + } |
| 115 | + } |
| 116 | + with patch.object(obr_module, "load_config", return_value=fake_config): |
| 117 | + targets = obr_module._parse_nics(wb) |
| 118 | + |
| 119 | + assert {t.variable for t in targets} == { |
| 120 | + "ni_employee", |
| 121 | + "ni_employer", |
| 122 | + "ni_class_2", |
| 123 | + "ni_class_3", |
| 124 | + "ni_class_4", |
| 125 | + } |
0 commit comments