Skip to content

Commit

Permalink
Avoid confusing error when using axes and missing entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Morendil committed Nov 12, 2023
1 parent 6a6e470 commit 1f2008b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions openfisca_core/simulations/simulation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ def add_default_group_entity(self, persons_ids, entity):
persons_count = len(persons_ids)
self.entity_ids[entity.plural] = persons_ids
self.entity_counts[entity.plural] = persons_count
self.memberships[entity.plural] = numpy.arange(
self.memberships[entity.plural] = list(numpy.arange(
0, persons_count, dtype=numpy.int32
)
self.roles[entity.plural] = numpy.repeat(
))
self.roles[entity.plural] = list(numpy.repeat(
entity.flattened_roles[0], persons_count
)
))

def add_group_entity(self, persons_plural, persons_ids, entity, instances_json):
"""
Expand Down
24 changes: 24 additions & 0 deletions tests/core/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,27 @@ def test_simulation_with_axes(tax_benefit_system):
[0, 0, 0, 0, 0, 0]
)
assert simulation.get_array("rent", "2018-11") == pytest.approx([0, 0, 3000, 0])

# Test for missing group entities with build_from_entities()

def test_simulation_with_axes_missing_entities(tax_benefit_system):
input_yaml = """
persons:
Alicia: {salary: {2018-11: 0}}
Javier: {}
Tom: {}
axes:
-
- count: 2
name: rent
min: 0
max: 3000
period: 2018-11
"""
data = test_runner.yaml.safe_load(input_yaml)
simulation = SimulationBuilder().build_from_entities(tax_benefit_system, data)
assert simulation.get_array("salary", "2018-11") == pytest.approx(
[0, 0, 0, 0, 0, 0]
)
# Since a household is synthesized for each person, we have six:
assert simulation.get_array("rent", "2018-11") == pytest.approx([0, 0, 0, 3000, 0, 0])

0 comments on commit 1f2008b

Please sign in to comment.