Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scorecard_db/ingest_uk_ab2026.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,9 @@ def _drop(acct: dict, fam: dict, reason_key: str) -> None:
fam["drops"][reason_key] = fam["drops"].get(reason_key, 0) + 1


_PRINTED_AMOUNT = re.compile(r"£(\d+|\d{1,3}(?:,\d{3})+)(?:\.(\d+))? (million|billion)")
_PRINTED_AMOUNT = re.compile(
r"£(0|[1-9]\d*|[1-9]\d{0,2}(?:,\d{3})+)(?:\.(\d+))? (million|billion)"
)
_MULTIPLIERS = {"million": 1e6, "billion": 1e9}


Expand Down
90 changes: 86 additions & 4 deletions tests/test_uk_ab2026_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,32 @@ def _staged_unit(row):
STORED_UNITS = {("uk_ifs_ab2026", "slide 4", "£80"): "gbp_per_household"}


# The conditions `_score`, `_verdict_conditions` and `with_baseline_condition`
# write beside the staged ones.
INGEST_CONDITIONS = {
"action_link",
"baseline_policy",
"benchmark_class",
"counterfactual",
"country",
"estimate_kind",
"measure_key",
"pe_expressibility",
"pe_missing",
"reform_hint",
"scoring_method",
"staged_metric",
"value_verbatim",
}


def _keeps_conditions(row, score):
"""Require each staged condition on the claim as staged. For a condition the
ingest canonicalises, require the registered name and the staged text beside
any name that differs. For a program we haven't registered, require the staged
text alone."""
for key, value in (row.get("conditions") or {}).items():
text alone. Refuse any condition neither the row nor the ingest supplies."""
staged = row.get("conditions") or {}
for key, value in staged.items():
stored = score.conditions.get(key)
verbatim = score.conditions.get(f"{key}_verbatim")
if key == "program" and value not in known(row["source"], key):
Expand All @@ -731,7 +751,12 @@ def _keeps_conditions(row, score):
kept = stored == value
if not kept:
return False
return True
verbatim = {
f"{key}_verbatim"
for key in staged
if key == "program" or key in I.AB25._CANONICALISED
}
return set(score.conditions) <= set(staged) | verbatim | INGEST_CONDITIONS


_CHILDCARE = "free childcare entitlement (claimed and received some)"
Expand Down Expand Up @@ -776,9 +801,21 @@ def _keeps_conditions(row, score):
{"source": "obr", "conditions": {"geography": "NI"}},
{"geography": "Northern Ireland"},
),
(
{"source": "centax", "conditions": {"basis": "forecast"}},
{"basis": "forecast", "housing_costs": "AHC"},
),
(
{"source": "centax", "conditions": {"basis": "forecast"}},
{"basis": "forecast", "basis_verbatim": "forecast"},
),
(
{"source": "centax", "conditions": {"basis": "forecast"}},
{"basis": "forecast", "geography_verbatim": "England"},
),
],
)
def test_preservation_rejects_a_lost_condition(row, stored):
def test_preservation_rejects_a_lost_or_invented_condition(row, stored):
assert not _keeps_conditions(row, SimpleNamespace(conditions=stored))


Expand Down Expand Up @@ -808,6 +845,10 @@ def test_preservation_rejects_a_lost_condition(row, stored):
{"source": "obr", "conditions": {"geography": "NI"}},
{"geography": "Northern Ireland", "geography_verbatim": "NI"},
),
(
{"source": "centax", "conditions": {"basis": "forecast"}},
{"basis": "forecast", "country": "UK", "value_verbatim": "£1"},
),
],
)
def test_preservation_accepts_what_the_ingest_keeps(row, stored):
Expand Down Expand Up @@ -836,6 +877,22 @@ def canonical(source, axis, value):
assert score.conditions.get("program_verbatim") == verbatim


def test_a_static_exchequer_row_gains_its_scoring_method():
"""We staged no row under `exchequer_impact_static`, so we set it on an
HM Treasury costings row with no scoring method."""
row = copy.deepcopy(
next(
r
for r in RAW["uk_hmt_costings"]
if "scoring_method" not in (r.get("conditions") or {})
)
)
row["proposed_metric"] = "exchequer_impact_static"
score = I._score(row, "uk_hmt_costings", row["source"], Metric.REVENUE_CHANGE)
assert score.conditions["scoring_method"] == "static"
assert _keeps_conditions(row, score)


def test_every_claim_preserves_its_staged_row(staged):
"""Read each family's claims file directly, find the row behind each
claim by its printed place and printed value, and require the claim to
Expand Down Expand Up @@ -1039,6 +1096,9 @@ def test_a_ukraine_row_keeps_its_table_cell_for_the_year_exception(monkeypatch):
"£26,00 million",
"£2600, million",
"£1,2345 million",
"£0,123 million",
"£01 million",
"£1234,567 million",
],
)
def test_a_reviewed_twin_printed_in_another_form_raises(raw):
Expand All @@ -1048,6 +1108,28 @@ def test_a_reviewed_twin_printed_in_another_form_raises(raw):
I._check_row_calls("uk_hmt_redbook", rows)


@given(st.integers(0, 10**12), st.sampled_from(["million", "billion"]))
def test_a_printed_amount_reads_with_or_without_thousands_commas(number, unit):
for digits in (str(number), f"{number:,}"):
printed = I._PRINTED_AMOUNT.fullmatch(f"£{digits} {unit}")
assert printed and int(printed[1].replace(",", "")) == number


@given(st.integers(1, 10**12), st.integers(0, 13))
def test_a_printed_amount_takes_commas_only_at_thousands(number, at):
digits = str(number)
comma = f"{digits[:at]},{digits[at:]}"
assert (I._PRINTED_AMOUNT.fullmatch(f"£{comma} million") is not None) == (
comma == f"{number:,}"
)


@given(st.integers(1, 10**12))
def test_a_printed_amount_takes_no_leading_zero(number):
assert I._PRINTED_AMOUNT.fullmatch(f"£0{number} million") is None
assert I._PRINTED_AMOUNT.fullmatch(f"£0{number:,} million") is None


def test_a_reviewed_twin_whose_value_leaves_its_print_raises():
"""We cannot widen the ingest's tolerance by storing a value that differs
from the printed amount."""
Expand Down
Loading