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/firm-entity-is-person.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Describe only a country's person entity as a person in `/{country}/metadata`, so the UK `firm` entity, which policyengine-core also flags `is_person`, is described as a non-person entity.
15 changes: 12 additions & 3 deletions policyengine_api/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,23 @@ def build_parameters(self) -> dict:
return parameter_data

def build_entities(self) -> dict:
"""Describe every entity by key, with its roles (empty when it has none)."""
"""Describe every entity by key, with its roles (empty when it has none).

Only the system's person entity is described with ``is_person`` true.
policyengine-core also flags a root population with no members (such
as the UK ``firm`` entity) as ``is_person``, and takes the first such
entity as the person entity. Keys are compared because a cloned system
copies its person entity.
"""
entities = self.tax_benefit_system.entities
person_key = next((entity.key for entity in entities if entity.is_person), None)
data = {}
for entity in self.tax_benefit_system.entities:
for entity in entities:
entity_data = {
"plural": entity.plural,
"label": entity.label,
"doc": entity.doc,
"is_person": entity.is_person,
"is_person": entity.key == person_key,
"key": entity.key,
}
if hasattr(entity, "roles"):
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ def test__json_safe_serializes_dates(self):
}


class TestCountryEntities:
def test__build_entities_describes_only_the_person_entity_as_a_person(self):
# policyengine-core flags a root population with no members, such as
# the UK firm entity, as is_person. Only the first one is the person.
def core_entity(key, plural, *, is_person):
return SimpleNamespace(
key=key, plural=plural, label=key, doc="", is_person=is_person
)

country = PolicyEngineCountry.__new__(PolicyEngineCountry)
country.tax_benefit_system = SimpleNamespace(
entities=[
core_entity("person", "people", is_person=True),
core_entity("household", "households", is_person=False),
core_entity("firm", "firms", is_person=True),
]
)

entities = country.build_entities()

assert {key: entity["is_person"] for key, entity in entities.items()} == {
"person": True,
"household": False,
"firm": False,
}
assert entities["firm"]["roles"] == {}


class TestHouseholdCalculation:
def test__calculate_does_not_enable_or_read_simulation_tracing(self, monkeypatch):
class SimulationWithoutTracing:
Expand Down
Loading