Skip to content

Commit

Permalink
feat: fail with axes and missing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Dec 8, 2023
1 parent 9bb296f commit 82ddcc1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
46 changes: 22 additions & 24 deletions openfisca_core/simulations/simulation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,21 @@ def build_from_dict(
:return: A :any:`Simulation`
"""

if is_impl_spec(
params := input_dict, tax_benefit_system.entities_by_singular()
):
expl = self.explicit_singular_entities(tax_benefit_system, params)
return self.build_from_entities(tax_benefit_system, expl)
variables = tax_benefit_system.variables.keys()

if is_expl_spec(input_dict, tax_benefit_system.entities_plural()):
return self.build_from_entities(tax_benefit_system, input_dict)
singular = tax_benefit_system.entities_by_singular()

if is_axes_spec(input_dict):
raise ValueError("#TODO")
plural = tax_benefit_system.entities_plural()

if is_abbr_spec(params := input_dict, tax_benefit_system.variables.keys()):
return self.build_from_variables(tax_benefit_system, params)
if is_impl_spec(input_dict, singular):
expl = self.explicit_singular_entities(tax_benefit_system, input_dict)
return self.build_from_entities(tax_benefit_system, expl)

raise ValueError("#TODO")
if is_expl_spec(input_dict, plural):
return self.build_from_entities(tax_benefit_system, input_dict)

if is_abbr_spec(input_dict, variables):
return self.build_from_variables(tax_benefit_system, input_dict)

def build_from_entities(
self,
Expand Down Expand Up @@ -178,9 +177,6 @@ def build_from_entities(

person_entity: SingleEntity = tax_benefit_system.person_entity

if person_entity.plural is None:
raise ValueError("#TODO")

persons_json = params.get(person_entity.plural, None)

if not persons_json:
Expand All @@ -200,6 +196,17 @@ def build_from_entities(
self.add_group_entity(
self.persons_plural, persons_ids, entity_class, instances_json
)

elif axes:
raise errors.SituationParsingError(
[entity_class.plural],
f"We could not find any specified {entity_class.plural}. "
"In order to expand over axes, all group entities and roles "
"must be fully specified. For further support, please do "
"not hesitate to take a look at the official documentation: "
"https://openfisca.org/doc/simulate/replicate-simulation-inputs.html.",
)

else:
self.add_default_group_entity(persons_ids, entity_class)

Expand Down Expand Up @@ -354,9 +361,6 @@ def add_person_entity(self, entity, instances_json):
"""
Add the simulation's instances of the persons entity as described in ``instances_json``.
"""
if entity.plural is None:
raise ValueError("#TODO")

helpers.check_type(instances_json, dict, [entity.plural])
entity_ids = list(map(str, instances_json.keys()))
self.persons_plural = entity.plural
Expand All @@ -372,9 +376,6 @@ def add_person_entity(self, entity, instances_json):
def add_default_group_entity(
self, persons_ids: list[str], entity: GroupEntity
) -> None:
if entity.plural is None:
raise ValueError("#TODO")

persons_count = len(persons_ids)
roles = list(entity.flattened_roles)
self.entity_ids[entity.plural] = persons_ids
Expand All @@ -394,9 +395,6 @@ def add_group_entity(
"""
Add all instances of one of the model's entities as described in ``instances_json``.
"""
if entity.plural is None:
raise ValueError("#TODO")

helpers.check_type(instances_json, dict, [entity.plural])
entity_ids = list(map(str, instances_json.keys()))

Expand Down
15 changes: 6 additions & 9 deletions tests/core/test_axes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from openfisca_core import errors
from openfisca_core.simulations import SimulationBuilder
from openfisca_core.tools import test_runner

Expand Down Expand Up @@ -322,7 +323,7 @@ def test_add_perpendicular_axis_on_an_existing_variable_with_input(persons):
)


# Integration test
# Integration tests


def test_simulation_with_axes(tax_benefit_system):
Expand Down Expand Up @@ -370,11 +371,7 @@ def test_simulation_with_axes_missing_entities(tax_benefit_system):
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]
)
with pytest.raises(errors.SituationParsingError) as error:
SimulationBuilder().build_from_dict(tax_benefit_system, data)
assert "In order to expand over axes" in error.value()
assert "all group entities and roles must be fully specified" in error.value()

0 comments on commit 82ddcc1

Please sign in to comment.