Skip to content
Closed
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/4164.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1 households that leave out some group collections, or leave a person out of a collection's groups, now copy to v2. The copy creates the same groups that policyengine-core creates when it calculates the v1 household: one group of everyone for an omitted collection, and a group of their own for each person left out.
51 changes: 48 additions & 3 deletions policyengine_api/services/v2/households/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def translate_legacy_household(
"""Translate a v1 household document into normalized v2 household content.

Named v1 people and groups get stable ids in name order, and group members
become person memberships. The default year is derived from the value periods.
become person memberships. People a v1 collection leaves out belong to the
unnamed groups policyengine-core would create for them. The default year is
derived from the value periods.
Raise LegacyHouseholdTranslationError when the v1 document cannot be
translated exactly.
"""
Expand Down Expand Up @@ -269,9 +271,24 @@ def _translate_legacy_groups(
collection: str,
translation: _LegacyTranslation,
) -> list[dict[str, Any]]:
"""Translate one v1 collection's named groups into v2 records, in name order."""
"""Translate one v1 collection's groups into v2 records.

Named groups come first, in name order. People the collection leaves out get
the unnamed groups policyengine-core gives them when it builds a simulation
from the v1 household: one group of everyone when the collection is omitted
or null, otherwise a group of their own each, in name order.
"""
source_name = LEGACY_COLLECTION_NAMES[collection]
source_groups = source.get(source_name, {})
source_groups = source.get(source_name)
if source_groups is None:
return [
_unnamed_legacy_group(
collection,
group_number=1,
members=list(translation.person_ids),
translation=translation,
)
]
if type(source_groups) is not dict:
msg = f"legacy {source_name} must be an object"
raise LegacyHouseholdTranslationError(msg)
Expand All @@ -295,9 +312,37 @@ def _translate_legacy_groups(
"values": values,
}
)
left_out = [
person_name
for person_name in sorted(translation.person_ids)
if collection not in translation.person_memberships[person_name]
]
for group_number, person_name in enumerate(left_out, start=len(group_records) + 1):
group_records.append(
_unnamed_legacy_group(
collection,
group_number=group_number,
members=[person_name],
translation=translation,
)
)
return group_records


def _unnamed_legacy_group(
collection: str,
*,
group_number: int,
members: list[str],
translation: _LegacyTranslation,
) -> dict[str, Any]:
"""Return a v2 record, with no source name or values, for a group v1 implies."""
group_id = f"{collection}-{group_number}"
for person_name in members:
translation.person_memberships[person_name][collection] = group_id
return {"id": group_id, "values": {}}


def _translate_legacy_group(
group: dict[str, Any],
*,
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/test_v1_household_dual_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ def test_translation_failure_retains_one_pending_event_until_explicit_replay(
try:
creation = v1_service.create_household(
"us",
{"people": {"adult": {"age": {"2026": 40}}}},
"Structurally incomplete household",
{
"people": {"adult": {"age": {"2026": 40}}},
"unsupported_entities": {},
},
"Household with a collection policyengine-core rejects",
record_mirror_event=True,
)
with pytest.raises(HouseholdMirrorUnavailableError):
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/v2/test_household_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,87 @@ def test_array_variable_that_contains_person_names_is_not_a_relationship() -> No
]


ADULT_AND_CHILD = {"adult": {"age": {"2026": 40}}, "child": {"age": {"2026": 10}}}


@pytest.mark.parametrize(
("country_id", "household_json", "omitted_collections"),
[
(
"us",
{
"people": ADULT_AND_CHILD,
"households": {"home": {"members": ["adult", "child"]}},
"tax_units": {"tax unit": {"members": ["adult", "child"]}},
},
["family", "spm_unit", "marital_unit"],
),
(
"us",
{"people": ADULT_AND_CHILD, "households": None},
["household", "family", "tax_unit", "spm_unit", "marital_unit"],
),
(
"uk",
{
"people": ADULT_AND_CHILD,
"households": {"home": {"members": ["adult", "child"]}},
},
["benunit"],
),
],
)
def test_omitted_collection_becomes_one_unnamed_group_of_everyone(
country_id: str,
household_json: dict[str, object],
omitted_collections: list[str],
) -> None:
# policyengine-core fills an omitted or null collection with one group of
# every person, so v1 calculates these households.
translated = translate_legacy_household(
_snapshot(country_id, household_json=household_json)
)
people = translated.household_data["people"]

for collection in omitted_collections:
assert translated.household_data[collection] == [
{"id": f"{collection}-1", "values": {}}
]
assert [person["memberships"][collection] for person in people] == [
f"{collection}-1",
f"{collection}-1",
]


def test_person_left_out_of_a_collection_gets_an_unnamed_group_of_their_own() -> None:
# policyengine-core gives each person that a collection's groups leave out
# a group of their own, so v1 calculates these households.
source = _snapshot().household_json
assert isinstance(source, dict)
source["tax_units"] = {"tax unit": {"members": ["adult"]}}
source["marital_units"] = {}

translated = translate_legacy_household(_snapshot(household_json=source))
people = translated.household_data["people"]

assert translated.household_data["tax_unit"] == [
{"id": "tax_unit-1", "source_name": "tax unit", "values": {}},
{"id": "tax_unit-2", "values": {}},
]
assert translated.household_data["marital_unit"] == [
{"id": "marital_unit-1", "values": {}},
{"id": "marital_unit-2", "values": {}},
]
assert [person["memberships"]["tax_unit"] for person in people] == [
"tax_unit-1",
"tax_unit-2",
]
assert [person["memberships"]["marital_unit"] for person in people] == [
"marital_unit-1",
"marital_unit-2",
]


def test_one_unambiguous_year_supplies_scalar_period_context() -> None:
source = _snapshot().household_json
assert isinstance(source, dict)
Expand Down
Loading