|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
| 6 | +from policyengine_api import constants |
| 7 | +from policyengine_api.constants import get_economy_impact_cache_version |
6 | 8 | from policyengine_api.runtime_cache.core import CacheNamespace |
7 | 9 | from policyengine_api.runtime_cache.fake import InMemoryCacheBackend |
8 | 10 | from policyengine_api.runtime_cache.reform_impacts import ( |
@@ -226,3 +228,58 @@ def test_a_briefly_kept_error_expires_and_leaves_other_records() -> None: |
226 | 228 | assert [(impact.execution_id, impact.status) for impact in after_expiry] == [ |
227 | 229 | ("next-job", "computing") |
228 | 230 | ] |
| 231 | + |
| 232 | + |
| 233 | +def test_impact_cached_under_previous_economy_schema_version_is_not_matched( |
| 234 | + service, |
| 235 | +): |
| 236 | + # Economy results cached under schema version 3 can hold decile relative |
| 237 | + # changes as percentages (x100) from the simulation API |
| 238 | + # (PolicyEngine/policyengine-sim-api#827). A lookup built from the |
| 239 | + # current cache version must not find them. |
| 240 | + current_cache_version = get_economy_impact_cache_version(SCOPE.country_id) |
| 241 | + with pytest.MonkeyPatch.context() as schema_version_patch: |
| 242 | + schema_version_patch.setitem( |
| 243 | + constants.RUNTIME_CACHE_SCHEMA_VERSIONS, "economy_impact", 3 |
| 244 | + ) |
| 245 | + percentage_decile_cache_version = get_economy_impact_cache_version( |
| 246 | + SCOPE.country_id |
| 247 | + ) |
| 248 | + |
| 249 | + options_hash = "decile-hash" |
| 250 | + stale_identity = ReformImpactIdentity( |
| 251 | + scope=SCOPE, |
| 252 | + api_version=percentage_decile_cache_version, |
| 253 | + options_hash=options_hash, |
| 254 | + ) |
| 255 | + service.set_reform_impact( |
| 256 | + stale_identity, |
| 257 | + ReformImpactExecution( |
| 258 | + execution_id="percentage-decile-job", |
| 259 | + start_time=datetime(2026, 7, 1), |
| 260 | + ), |
| 261 | + options={"hash": options_hash}, |
| 262 | + status="computing", |
| 263 | + reform_impact_json={}, |
| 264 | + ) |
| 265 | + service.set_complete_reform_impact( |
| 266 | + "percentage-decile-job", |
| 267 | + reform_impact_json={"decile": {"relative": {"1": 1.5}}}, |
| 268 | + ) |
| 269 | + |
| 270 | + current_identity = ReformImpactIdentity( |
| 271 | + scope=SCOPE, |
| 272 | + api_version=current_cache_version, |
| 273 | + options_hash=options_hash, |
| 274 | + ) |
| 275 | + |
| 276 | + assert [ |
| 277 | + impact.execution_id for impact in service.get_all_reform_impacts(stale_identity) |
| 278 | + ] == ["percentage-decile-job"] |
| 279 | + assert service.get_all_reform_impacts(current_identity) == [] |
| 280 | + assert ( |
| 281 | + service.get_all_reform_impacts_by_options_hash_prefix( |
| 282 | + current_identity, f"{options_hash}%" |
| 283 | + ) |
| 284 | + == [] |
| 285 | + ) |
0 commit comments