diff --git a/changelog.d/uk-budget-headline-cache-versions.fixed.md b/changelog.d/uk-budget-headline-cache-versions.fixed.md new file mode 100644 index 000000000..6536de638 --- /dev/null +++ b/changelog.d/uk-budget-headline-cache-versions.fixed.md @@ -0,0 +1 @@ +Stop serving economy results and saved reports from before the UK budget headline counted employer National Insurance (PolicyEngine/policyengine-sim-api#1171). Each one is calculated again the next time it is requested, US ones included. diff --git a/policyengine_api/constants.py b/policyengine_api/constants.py index cfa040317..c8bc4ce7a 100644 --- a/policyengine_api/constants.py +++ b/policyengine_api/constants.py @@ -196,8 +196,15 @@ def get_bundle_default_dataset_option(country_id: str) -> dict: # keys do not change when only the simulation API's code changes, so this # version was raised to stop serving those results. The budget-window # cache key uses the same version. - "economy_impact": 4, - "report_output": 3, + # 5: UK economy results cached under 4 can hold a budget headline that + # leaves out employer National Insurance, from the simulation API + # (PolicyEngine/policyengine-sim-api#1170), which counts it since + # PolicyEngine/policyengine-sim-api#1171. + "economy_impact": 5, + # 4: UK report outputs saved under 3 can hold the same headline. Saved + # outputs are not read back from the economy cache, so they need a version + # of their own to be calculated again. + "report_output": 4, } # Runtime cache versions are stored in legacy VARCHAR(10) columns, and keep at # least this many digest characters after their scope prefix and schema version. diff --git a/tests/fixtures/services/report_output_fixtures.py b/tests/fixtures/services/report_output_fixtures.py index bd3bf921b..d466ea130 100644 --- a/tests/fixtures/services/report_output_fixtures.py +++ b/tests/fixtures/services/report_output_fixtures.py @@ -9,14 +9,14 @@ def insert_outdated_report_output( """Save a report output under the previous report-output cache version. The cache version is computed with the ``report_output`` schema version set - to 2, and the schema version is set back to 3 before the record is saved, + to 3, and the schema version is set back to 4 before the record is saved, so the record reads as outdated against the current cache version. ``fields`` are the record's other columns, e.g. its simulation IDs, year and output; its status is "complete" unless given. """ - monkeypatch.setitem(constants.RUNTIME_CACHE_SCHEMA_VERSIONS, "report_output", 2) - outdated_cache_version = get_report_output_cache_version(country_id) monkeypatch.setitem(constants.RUNTIME_CACHE_SCHEMA_VERSIONS, "report_output", 3) + outdated_cache_version = get_report_output_cache_version(country_id) + monkeypatch.setitem(constants.RUNTIME_CACHE_SCHEMA_VERSIONS, "report_output", 4) assert outdated_cache_version != get_report_output_cache_version(country_id) with orm_session_factory.begin() as session: outdated = ReportOutput( diff --git a/tests/unit/services/test_reform_impacts_service.py b/tests/unit/services/test_reform_impacts_service.py index e11b4d078..40ad90b58 100644 --- a/tests/unit/services/test_reform_impacts_service.py +++ b/tests/unit/services/test_reform_impacts_service.py @@ -226,32 +226,41 @@ def test_a_failed_records_seconds_left_count_down_to_its_expiry() -> None: assert service.get_reform_impact_seconds_left(stored_failure) == 90 -def test_impact_cached_under_previous_economy_schema_version_is_not_matched( - service, +@pytest.mark.parametrize( + "earlier_schema_version", + [ + # Results cached under 3 can hold decile relative changes as percentages + # (x100) from the simulation API (PolicyEngine/policyengine-sim-api#827). + 3, + # Results cached under 4 can hold a UK budget headline that leaves out + # employer National Insurance (PolicyEngine/policyengine-sim-api#1170). + 4, + ], +) +def test_impact_cached_under_an_earlier_economy_schema_version_is_not_matched( + service, earlier_schema_version ): - # Economy results cached under schema version 3 can hold decile relative - # changes as percentages (x100) from the simulation API - # (PolicyEngine/policyengine-sim-api#827). A lookup built from the - # current cache version must not find them. + # A lookup built from the current cache version must not find a result + # cached under an earlier one. current_cache_version = get_economy_impact_cache_version(SCOPE.country_id) with pytest.MonkeyPatch.context() as schema_version_patch: schema_version_patch.setitem( - constants.RUNTIME_CACHE_SCHEMA_VERSIONS, "economy_impact", 3 - ) - percentage_decile_cache_version = get_economy_impact_cache_version( - SCOPE.country_id + constants.RUNTIME_CACHE_SCHEMA_VERSIONS, + "economy_impact", + earlier_schema_version, ) + earlier_cache_version = get_economy_impact_cache_version(SCOPE.country_id) - options_hash = "decile-hash" + options_hash = "earlier-hash" stale_identity = ReformImpactIdentity( scope=SCOPE, - api_version=percentage_decile_cache_version, + api_version=earlier_cache_version, options_hash=options_hash, ) service.set_reform_impact( stale_identity, ReformImpactExecution( - execution_id="percentage-decile-job", + execution_id="earlier-version-job", start_time=datetime(2026, 7, 1), ), options={"hash": options_hash}, @@ -259,8 +268,8 @@ def test_impact_cached_under_previous_economy_schema_version_is_not_matched( reform_impact_json={}, ) service.set_complete_reform_impact( - "percentage-decile-job", - reform_impact_json={"decile": {"relative": {"1": 1.5}}}, + "earlier-version-job", + reform_impact_json={"budget": {"budgetary_impact": 1.5}}, ) current_identity = ReformImpactIdentity( @@ -271,7 +280,7 @@ def test_impact_cached_under_previous_economy_schema_version_is_not_matched( assert [ impact.execution_id for impact in service.get_all_reform_impacts(stale_identity) - ] == ["percentage-decile-job"] + ] == ["earlier-version-job"] assert service.get_all_reform_impacts(current_identity) == [] assert ( service.get_all_reform_impacts_by_options_hash_prefix( diff --git a/tests/unit/test_constants.py b/tests/unit/test_constants.py index aefef36f3..7f74cbd0e 100644 --- a/tests/unit/test_constants.py +++ b/tests/unit/test_constants.py @@ -21,6 +21,7 @@ _resolve_distribution_version, get_economy_impact_cache_version, get_py_manifest, + get_report_output_cache_version, ) # Prepended to a subprocess script: any import of .py (the ``policyengine`` @@ -147,15 +148,34 @@ class TestEconomyImpactCacheVersion: # fractions (PolicyEngine/policyengine-sim-api#827). Economy results # cached under schema version 3 can hold those values, and the cache key # does not change when the simulation API's code does, so version 4 makes - # every economy cache lookup miss them and recompute. - def test__economy_impact_schema_version_is_4(self): - assert RUNTIME_CACHE_SCHEMA_VERSIONS["economy_impact"] == 4 + # every economy cache lookup miss them and recompute. Version 5 does the + # same for UK results whose budget headline leaves out employer National + # Insurance (PolicyEngine/policyengine-sim-api#1170). + def test__economy_impact_schema_version_is_5(self): + assert RUNTIME_CACHE_SCHEMA_VERSIONS["economy_impact"] == 5 @pytest.mark.parametrize("country_id", ["us", "uk"]) - def test__economy_impact_cache_version_carries_schema_version_4(self, country_id): + def test__economy_impact_cache_version_carries_schema_version_5(self, country_id): cache_version = get_economy_impact_cache_version(country_id) - assert cache_version.startswith("e4") + assert cache_version.startswith("e5") + assert len(cache_version) == RUNTIME_CACHE_VERSION_LENGTH + + +class TestReportOutputCacheVersion: + # A report output is saved with the result the app computed, and is not read + # back from the economy cache, so a UK report saved with a budget headline + # that leaves out employer National Insurance + # (PolicyEngine/policyengine-sim-api#1170) keeps it until its own version + # changes. + def test__report_output_schema_version_is_4(self): + assert RUNTIME_CACHE_SCHEMA_VERSIONS["report_output"] == 4 + + @pytest.mark.parametrize("country_id", ["us", "uk"]) + def test__report_output_cache_version_carries_schema_version_4(self, country_id): + cache_version = get_report_output_cache_version(country_id) + + assert cache_version.startswith("r4") assert len(cache_version) == RUNTIME_CACHE_VERSION_LENGTH