11# -*- coding: utf-8 -*-
22
3- from enum import Enum
4- from datetime import date
53from pytest import fixture
64
75from openfisca_country_template import CountryTaxBenefitSystem
1210from openfisca_core .variables import Variable
1311from openfisca_core .periods import ETERNITY
1412from openfisca_core .entities import Entity , GroupEntity
15- from openfisca_core .indexed_enums import Enum as OFEnum
1613from openfisca_core .model_api import * # noqa analysis:ignore
1714
1815
19- class TestVariable (Variable ):
20- definition_period = ETERNITY
21- value_type = float
22-
23- def __init__ (self , entity ):
24- self .__class__ .entity = entity
25- super ().__init__ ()
26-
27-
28- @fixture
29- def tax_benefit_system ():
30- return CountryTaxBenefitSystem ()
31-
32-
33- @fixture
34- def simulation_builder ():
35- return SimulationBuilder ()
36-
37-
38- @fixture
39- def simulation (tax_benefit_system , simulation_builder ):
40- return SimulationBuilder ().build_default_simulation (tax_benefit_system )
41-
16+ # PERIODS
4217
4318@fixture
4419def period ():
4520 return "2016-01"
4621
4722
48- @fixture
49- def year_period ():
50- return "2016"
23+ # ENTITIES
5124
5225
53- @fixture
54- def make_simulation (tax_benefit_system , simulation_builder , period ):
55- def _make_simulation (data ):
56- simulation_builder .default_period = period
57- return simulation_builder .build_from_variables (tax_benefit_system , data )
58- return _make_simulation
59-
60-
61- @fixture
62- def make_isolated_simulation (simulation_builder , period ):
63- def _make_simulation (tbs , data ):
64- simulation_builder .default_period = period
65- return simulation_builder .build_from_variables (tbs , data )
66- return _make_simulation
67-
26+ class TestVariable (Variable ):
27+ definition_period = ETERNITY
28+ value_type = float
6829
69- @fixture
70- def make_simulation_from_yaml (tax_benefit_system , simulation_builder ):
71- def _make_simulation_from_yaml (simulation_yaml ):
72- return simulation_builder .build_from_dict (tax_benefit_system , yaml .safe_load (simulation_yaml ))
73- return _make_simulation_from_yaml
30+ def __init__ (self , entity ):
31+ self .__class__ .entity = entity
32+ super ().__init__ ()
7433
7534
7635@fixture
@@ -87,53 +46,6 @@ def check_variable_defined_for_entity(self, variable_name):
8746 return TestPersonEntity ("person" , "persons" , "" , "" )
8847
8948
90- @fixture
91- def int_variable (persons ):
92-
93- class intvar (Variable ):
94- definition_period = ETERNITY
95- value_type = int
96- entity = persons
97-
98- def __init__ (self ):
99- super ().__init__ ()
100-
101- return intvar ()
102-
103-
104- @fixture
105- def date_variable (persons ):
106-
107- class datevar (Variable ):
108- definition_period = ETERNITY
109- value_type = date
110- entity = persons
111-
112- def __init__ (self ):
113- super ().__init__ ()
114-
115- return datevar ()
116-
117-
118- @fixture
119- def enum_variable ():
120-
121- class TestEnum (Variable ):
122- definition_period = ETERNITY
123- value_type = OFEnum
124- dtype = 'O'
125- default_value = '0'
126- is_neutralized = False
127- set_input = None
128- possible_values = Enum ('foo' , 'bar' )
129- name = "enum"
130-
131- def __init__ (self ):
132- pass
133-
134- return TestEnum ()
135-
136-
13749@fixture
13850def group_entity ():
13951 class Household (GroupEntity ):
@@ -218,132 +130,68 @@ def couple():
218130 }
219131 }
220132
221-
222- @fixture
223- def simulation_single (simulation_builder , tax_benefit_system , single ):
224- return simulation_builder .build_from_entities (tax_benefit_system , single )
133+ # TAX BENEFIt SYSTEMS
225134
226135
227136@fixture
228- def simulation_couple (simulation_builder , tax_benefit_system , couple ):
229- return simulation_builder .build_from_entities (tax_benefit_system , couple )
230-
231-
232- class simple_variable (Variable ):
233- entity = Person
234- definition_period = MONTH
235- value_type = int
236-
237-
238- class variable_with_calculate_output_add (Variable ):
239- entity = Person
240- definition_period = MONTH
241- value_type = int
242- calculate_output = calculate_output_add
137+ def tax_benefit_system ():
138+ return CountryTaxBenefitSystem ()
243139
244140
245- class variable_with_calculate_output_divide (Variable ):
246- entity = Person
247- definition_period = YEAR
248- value_type = int
249- calculate_output = calculate_output_divide
141+ # SIMULATIONS
250142
251143
252144@fixture
253- def simulation_single_with_variables (simulation_builder , tax_benefit_system , single ):
254- tax_benefit_system .add_variables (
255- simple_variable ,
256- variable_with_calculate_output_add ,
257- variable_with_calculate_output_divide
258- )
259- return simulation_builder .build_from_entities (tax_benefit_system , single )
260-
261-
262- # 1 <--> 2 with same period
263- class variable1 (Variable ):
264- value_type = int
265- entity = Person
266- definition_period = MONTH
267-
268- def formula (person , period ):
269- return person ('variable2' , period )
270-
271-
272- class variable2 (Variable ):
273- value_type = int
274- entity = Person
275- definition_period = MONTH
276-
277- def formula (person , period ):
278- return person ('variable1' , period )
279-
280-
281- # 3 <--> 4 with a period offset
282- class variable3 (Variable ):
283- value_type = int
284- entity = Person
285- definition_period = MONTH
286-
287- def formula (person , period ):
288- return person ('variable4' , period .last_month )
289-
145+ def simulation_builder ():
146+ return SimulationBuilder ()
290147
291- class variable4 (Variable ):
292- value_type = int
293- entity = Person
294- definition_period = MONTH
295148
296- def formula (person , period ):
297- return person ('variable3' , period )
149+ @fixture
150+ def simulation (tax_benefit_system , simulation_builder ):
151+ return SimulationBuilder ().build_default_simulation (tax_benefit_system )
298152
299153
300- # 5 -f-> 6 with a period offset
301- # <---
302- class variable5 (Variable ):
303- value_type = int
304- entity = Person
305- definition_period = MONTH
154+ @fixture
155+ def make_simulation_from_entities (tax_benefit_system , simulation_builder ):
156+ def _make_simulation_from_entities (entities ):
157+ return simulation_builder .build_from_entities (tax_benefit_system , entities )
158+ return _make_simulation_from_entities
306159
307- def formula (person , period ):
308- variable6 = person ('variable6' , period .last_month )
309- return 5 + variable6
310160
161+ @fixture
162+ def make_simulation_from_tbs_and_variables (simulation_builder , period ):
163+ def _make_simulation_from_variables_and_tbs (tbs , variables ):
164+ simulation_builder .default_period = period
165+ return simulation_builder .build_from_variables (tbs , variables )
166+ return _make_simulation_from_variables_and_tbs
311167
312- class variable6 (Variable ):
313- value_type = int
314- entity = Person
315- definition_period = MONTH
316168
317- def formula (person , period ):
318- variable5 = person ('variable5' , period )
319- return 6 + variable5
169+ @fixture
170+ def make_simulation_from_yaml (tax_benefit_system , simulation_builder ):
171+ def _make_simulation_from_yaml (simulation_yaml ):
172+ return simulation_builder .build_from_dict (tax_benefit_system , yaml .safe_load (simulation_yaml ))
173+ return _make_simulation_from_yaml
320174
321175
322- class variable7 (Variable ):
323- value_type = int
324- entity = Person
325- definition_period = MONTH
176+ @fixture
177+ def simulation_single (make_simulation_from_entities , single ):
178+ return make_simulation_from_entities (single )
326179
327- def formula (person , period ):
328- variable5 = person ('variable5' , period )
329- return 7 + variable5
330180
181+ @fixture
182+ def simulation_couple (make_simulation_from_entities , couple ):
183+ return make_simulation_from_entities (couple )
331184
332- # december cotisation depending on november value
333- class cotisation (Variable ):
334- value_type = int
335- entity = Person
336- definition_period = MONTH
337185
338- def formula (person , period ):
339- if period .start .month == 12 :
340- return 2 * person ('cotisation' , period .last_month )
341- else :
342- return person .empty_array () + 1
186+ # VARIABLES
343187
344188
345189@fixture
346- def simulation_with_variables (tax_benefit_system , simulation_builder ):
347- tax_benefit_system .add_variables (variable1 , variable2 , variable3 , variable4 ,
348- variable5 , variable6 , variable7 , cotisation )
349- return SimulationBuilder ().build_default_simulation (tax_benefit_system )
190+ def make_variable ():
191+ def _make_variable (name , ** new_methods_and_attrs ):
192+ methods_and_attrs = {}
193+ default = dict (value_type = int , entity = Person , definition_period = MONTH )
194+ methods_and_attrs .update (default )
195+ methods_and_attrs .update (new_methods_and_attrs )
196+ return type (name , (Variable , ), methods_and_attrs )
197+ return _make_variable
0 commit comments