Skip to content
Draft
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
1 change: 1 addition & 0 deletions changelog.d/metadata-variable-quantity-type.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include each variable's quantity type ("stock" or "flow") in the `/{country_id}/metadata` variable descriptions as `quantityType`.
3 changes: 3 additions & 0 deletions policyengine_api/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ def build_variables(self) -> dict:
"entity": variable.entity.key,
"valueType": variable.value_type.__name__,
"definitionPeriod": variable.definition_period,
# "stock" for a balance or value at one point in time, "flow"
# for an amount over a period.
"quantityType": variable.quantity_type,
"name": variable_name,
"label": variable.label,
"category": variable.category,
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ def get_population(self, entity_plural):
)


class TestVariableQuantityType:
"""Each variable's metadata says whether it is a stock or a flow."""

@pytest.mark.parametrize(
("country_id", "variable_name", "quantity_type"),
[
# Money over a period: core's default for a float variable.
("us", "employment_income", "flow"),
("uk", "employment_income", "flow"),
# A value at one point in time, declared in the model.
("us", "household_vehicles_value", "stock"),
("uk", "mortgage_debt", "stock"),
# Core's default for a yes-or-no variable.
("us", "is_blind", "stock"),
],
)
def test__variable_metadata_includes_the_models_quantity_type(
self, country_id, variable_name, quantity_type
):
variable_metadata = COUNTRIES[country_id].metadata["variables"][variable_name]

assert variable_metadata["quantityType"] == quantity_type

def test__every_variable_has_a_stock_or_flow_quantity_type(self):
for country_id, country in COUNTRIES.items():
quantity_types = {
variable_metadata["quantityType"]
for variable_metadata in country.metadata["variables"].values()
}

assert quantity_types <= {"stock", "flow"}, country_id


class TestUKCountryMetadata:
"""Tests for UK country metadata, specifically local authority loading."""

Expand Down
Loading