|
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,21 @@ 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 | + persons_json = {'Alicia': {'salary': {'2018': 6000}}, 'Javier': {'salary': {'2018': 6000, '2018-01': 500}}} |
| 280 | + simulation_builder.add_person_entity(persons, persons_json) |
| 281 | + population = Population(persons) |
| 282 | + |
| 283 | + salary = population.entity.get_variable('salary') |
| 284 | + salary.definition_period = MONTH |
| 285 | + salary.set_input = set_input_divide_by_period |
| 286 | + |
| 287 | + simulation_builder.finalize_variables_init(population) |
| 288 | + assert_near(population.get_holder('salary').get_array('2018-01'), [500, 500]) |
| 289 | + assert population.count == 2 |
| 290 | + assert population.ids == ['Alicia', 'Javier'] |
| 291 | + |
| 292 | + |
270 | 293 | def test_canonicalize_period_keys(simulation_builder, persons):
|
271 | 294 | persons_json = {'Alicia': {'salary': {'year:2018-01': 100}}}
|
272 | 295 | simulation_builder.add_person_entity(persons, persons_json)
|
|
0 commit comments