Skip to content

Commit 19da1aa

Browse files
committed
Read SPI projection metadata without write access
1 parent 2dda55e commit 19da1aa

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

policyengine_uk_data/tests/test_spi_build.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,36 @@ def test_income_projection_uses_current_spi_release():
220220
assert "savings_interest_income" in incomes_projection.ALL_INCOME_VARIABLES
221221

222222

223+
def test_income_projection_reads_spi_dataset_year_read_only(monkeypatch):
224+
from policyengine_uk_data.utils import incomes_projection
225+
226+
calls = {}
227+
228+
class FakeStore:
229+
def __enter__(self):
230+
return self
231+
232+
def __exit__(self, exc_type, exc_value, traceback):
233+
return None
234+
235+
def __getitem__(self, key):
236+
assert key == "time_period"
237+
return pd.Series([2022])
238+
239+
def fake_hdf_store(path, mode=None):
240+
calls["path"] = path
241+
calls["mode"] = mode
242+
return FakeStore()
243+
244+
monkeypatch.setattr(incomes_projection.pd, "HDFStore", fake_hdf_store)
245+
246+
assert incomes_projection._read_spi_dataset_year("/readonly/spi_2022_23.h5") == 2022
247+
assert calls == {
248+
"path": "/readonly/spi_2022_23.h5",
249+
"mode": "r",
250+
}
251+
252+
223253
def test_income_projection_builds_current_spi_dataset_when_missing(
224254
tmp_path,
225255
monkeypatch,

policyengine_uk_data/utils/incomes_projection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
def _read_spi_dataset_year(dataset_path) -> int:
23-
with pd.HDFStore(dataset_path) as store:
23+
with pd.HDFStore(dataset_path, mode="r") as store:
2424
return int(store["time_period"].iloc[0])
2525

2626

0 commit comments

Comments
 (0)