Skip to content

Commit ef591ff

Browse files
committed
feat: Add API version to economy comparisons
1 parent 317eac4 commit ef591ff

File tree

9 files changed

+138
-1
lines changed

9 files changed

+138
-1
lines changed

policyengine/constants.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
from policyengine_core.data import Dataset
44
from policyengine.utils.data_download import download
55

6+
SUPPORTED_COUNTRY_IDS = [
7+
"us",
8+
"uk",
9+
]
10+
11+
UNSUPPORTED_COUNTRY_IDS = [
12+
"ca",
13+
"il",
14+
"ng",
15+
]
16+
17+
SUPPORTED_COUNTRY_PACKAGES = [
18+
f"policyengine_{country}" for country in SUPPORTED_COUNTRY_IDS
19+
]
20+
UNSUPPORTED_COUNTRY_PACKAGES = [
21+
f"policyengine_{country}" if country != "ca" else "policyengine_canada"
22+
for country in UNSUPPORTED_COUNTRY_IDS
23+
]
24+
25+
ALL_COUNTRY_PACKAGES = (
26+
SUPPORTED_COUNTRY_PACKAGES + UNSUPPORTED_COUNTRY_PACKAGES
27+
)
28+
629
# Datasets
730

831
ENHANCED_FRS = "hf://policyengine/policyengine-uk-data/enhanced_frs_2022_23.h5"

policyengine/outputs/macro/comparison/calculate_economy_comparison.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from policyengine.outputs.macro.single.calculate_single_economy import (
1111
SingleEconomy,
1212
)
13+
from policyengine.utils.versioning import get_country_package_version
1314
from typing import List, Dict
1415

1516

@@ -775,6 +776,7 @@ def uk_constituency_breakdown(
775776

776777

777778
class EconomyComparison(BaseModel):
779+
country_packge_version: str
778780
budget: BudgetaryImpact
779781
detailed_budget: DetailedBudgetaryImpact
780782
decile: DecileImpact
@@ -823,6 +825,7 @@ def calculate_economy_comparison(
823825
)
824826

825827
return EconomyComparison(
828+
country_package_version=get_country_package_version(country_id),
826829
budget=budgetary_impact_data,
827830
detailed_budget=detailed_budgetary_impact_data,
828831
decile=decile_impact_data,

policyengine/simulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _initialise_simulation(
181181
time_period: TimePeriodType,
182182
region: RegionType,
183183
subsample: SubsampleType,
184-
):
184+
) -> CountrySimulation:
185185
macro = scope == "macro"
186186
_simulation_type: Type[CountrySimulation] = {
187187
"uk": {

policyengine/utils/versioning.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from importlib.metadata import version
2+
from policyengine.constants import (
3+
SUPPORTED_COUNTRY_IDS,
4+
UNSUPPORTED_COUNTRY_IDS,
5+
)
6+
7+
8+
def get_version(package: str) -> str:
9+
try:
10+
return version(package)
11+
except Exception as e:
12+
raise RuntimeError(
13+
f"Could not get version for package {package}: {e}"
14+
) from e
15+
16+
17+
def get_country_package_version(country_id: str) -> str:
18+
19+
if country_id in UNSUPPORTED_COUNTRY_IDS:
20+
raise ValueError(f"Country ID {country_id} is not supported.")
21+
22+
if country_id not in SUPPORTED_COUNTRY_IDS:
23+
raise ValueError(f"Country ID {country_id} is not recognized.")
24+
25+
try:
26+
return version(f"policyengine_{country_id}")
27+
except Exception as e:
28+
raise RuntimeError(
29+
f"Could not get version for package policyengine_{country_id}: {e}"
30+
) from e

tests/__init__.py

Whitespace-only changes.

tests/fixtures/utils/__init__.py

Whitespace-only changes.

tests/fixtures/utils/versioning.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from policyengine.constants import SUPPORTED_COUNTRY_PACKAGES
2+
from importlib.metadata import PackageNotFoundError
3+
import pytest
4+
from unittest.mock import patch
5+
6+
7+
@pytest.fixture
8+
def patch_importlib_version():
9+
def mock_version(package_name):
10+
if package_name in SUPPORTED_COUNTRY_PACKAGES:
11+
return "1.0.0"
12+
else:
13+
raise Exception(f"Package {package_name} not found")
14+
15+
with patch(
16+
"policyengine.utils.versioning.version", side_effect=mock_version
17+
) as mock:
18+
yield mock

tests/utils/__init__.py

Whitespace-only changes.

tests/utils/test_versioning.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
from policyengine.utils.versioning import (
3+
get_version,
4+
get_country_package_version,
5+
)
6+
from tests.fixtures.utils.versioning import patch_importlib_version
7+
from importlib.metadata import PackageNotFoundError
8+
9+
10+
class TestGetVersion:
11+
def test__given_package_exist__then_return_version(
12+
self, patch_importlib_version
13+
):
14+
test_package = "policyengine_us"
15+
16+
test_version = get_version(test_package)
17+
18+
# Version number defined by mock
19+
assert test_version == "1.0.0"
20+
21+
def test__given_package_does_not_exist__then_raise_exception(
22+
self, patch_importlib_version
23+
):
24+
test_package = "non_existent_package"
25+
26+
with pytest.raises(
27+
RuntimeError,
28+
match=f"Could not get version for package {test_package}",
29+
):
30+
get_version(test_package)
31+
32+
33+
class TestGetCountryPackageVersion:
34+
def test__given_country_package_exists__then_return_version(
35+
self, patch_importlib_version
36+
):
37+
test_country = "us"
38+
39+
test_version = get_country_package_version(test_country)
40+
41+
# Version number defined by mock
42+
assert test_version == "1.0.0"
43+
44+
def test__given_country_package_does_not_exist__then_raise_exception(
45+
self, patch_importlib_version
46+
):
47+
test_country = "non_existent_country"
48+
49+
with pytest.raises(
50+
ValueError, match=f"Country ID {test_country} is not recognized."
51+
):
52+
get_country_package_version(test_country)
53+
54+
def test__given_country_not_supported__then_raise_error(
55+
self, patch_importlib_version
56+
):
57+
unsupported_country = "ca"
58+
59+
with pytest.raises(
60+
ValueError,
61+
match=f"Country ID {unsupported_country} is not supported.",
62+
):
63+
get_country_package_version(unsupported_country)

0 commit comments

Comments
 (0)