|
11 | 11 | from openfisca_core.tools import assert_near |
12 | 12 | from openfisca_core.tools.test_runner import yaml |
13 | 13 | from openfisca_core.entities import Entity, GroupEntity |
| 14 | +from openfisca_core.holders import set_input_divide_by_period |
14 | 15 | from openfisca_core.populations import Population |
15 | 16 | from openfisca_core.variables import Variable |
16 | 17 | from openfisca_country_template.entities import Household |
17 | 18 | from openfisca_country_template.situation_examples import couple |
18 | 19 | from openfisca_core.errors import SituationParsingError |
19 | | -from openfisca_core.periods import ETERNITY |
| 20 | +from openfisca_core.periods import ETERNITY, MONTH |
20 | 21 | from openfisca_core.indexed_enums import Enum as OFEnum |
21 | 22 |
|
22 | 23 |
|
@@ -87,9 +88,16 @@ def __init__(self): |
87 | 88 | @fixture |
88 | 89 | def persons(): |
89 | 90 | class TestPersonEntity(Entity): |
| 91 | + def __init__(self, key, plural, label, doc): |
| 92 | + super().__init__(key, plural, label, doc) |
| 93 | + self.variables = {} |
| 94 | + |
90 | 95 | def get_variable(self, variable_name): |
91 | | - result = TestVariable(self) |
92 | | - result.name = variable_name |
| 96 | + result = self.variables.get(variable_name) |
| 97 | + if result is None: |
| 98 | + result = TestVariable(self) |
| 99 | + result.name = variable_name |
| 100 | + self.variables[variable_name] = result |
93 | 101 | return result |
94 | 102 |
|
95 | 103 | def check_variable_defined_for_entity(self, variable_name): |
@@ -267,6 +275,23 @@ def test_finalize_person_entity(simulation_builder, persons): |
267 | 275 | assert population.ids == ['Alicia', 'Javier'] |
268 | 276 |
|
269 | 277 |
|
| 278 | +def test_heterogenous_periods(simulation_builder, persons): |
| 279 | + months = {month: 500 for month in ["2018-{:02d}".format(x) for x in range(1, 13)]} |
| 280 | + year = {'2018': 6000} |
| 281 | + persons_json = {'Alicia': {'salary': year}, 'Javier': {'salary': months}} |
| 282 | + simulation_builder.add_person_entity(persons, persons_json) |
| 283 | + population = Population(persons) |
| 284 | + |
| 285 | + salary = population.entity.get_variable('salary') |
| 286 | + salary.definition_period = MONTH |
| 287 | + salary.set_input = set_input_divide_by_period |
| 288 | + |
| 289 | + simulation_builder.finalize_variables_init(population) |
| 290 | + assert_near(population.get_holder('salary').get_array('2018-01'), [1000, 500]) |
| 291 | + assert population.count == 2 |
| 292 | + assert population.ids == ['Alicia', 'Javier'] |
| 293 | + |
| 294 | + |
270 | 295 | def test_canonicalize_period_keys(simulation_builder, persons): |
271 | 296 | persons_json = {'Alicia': {'salary': {'year:2018-01': 100}}} |
272 | 297 | simulation_builder.add_person_entity(persons, persons_json) |
|
0 commit comments