Skip to content

Commit 96a5433

Browse files
author
Mauko Quiroga
committed
Fix reform tests
1 parent cb51aa9 commit 96a5433

3 files changed

Lines changed: 48 additions & 57 deletions

File tree

tests/core/test_countries.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,26 @@ def test_input_with_wrong_period(simulation_builder, tax_benefit_system):
9191
simulation_builder.build_from_variables(tax_benefit_system, variables)
9292

9393

94-
def test_variable_with_reference(simulation_builder, isolated_tax_benefit_system):
94+
def test_variable_with_reference(make_simulation, isolated_tax_benefit_system):
9595
variables = {"salary": 4000}
96-
simulation_builder.set_default_period(PERIOD)
97-
98-
simulation = \
99-
simulation_builder \
100-
.build_from_variables(isolated_tax_benefit_system, variables)
96+
simulation = make_simulation(isolated_tax_benefit_system, variables, PERIOD)
10197

102-
disposable_income = simulation.calculate("disposable_income", PERIOD)
98+
result = simulation.calculate("disposable_income", PERIOD)
10399

104-
assert disposable_income > 0
100+
assert result > 0
105101

106102
class disposable_income(Variable):
107-
108103
definition_period = periods.MONTH
109104

110105
def formula(household, period):
111106
return household.empty_array()
112107

113108
isolated_tax_benefit_system.update_variable(disposable_income)
109+
simulation = make_simulation(isolated_tax_benefit_system, variables, PERIOD)
114110

115-
simulation = \
116-
simulation_builder \
117-
.build_from_variables(isolated_tax_benefit_system, variables)
118-
119-
disposable_income = simulation.calculate("disposable_income", PERIOD)
111+
result = simulation.calculate("disposable_income", PERIOD)
120112

121-
assert disposable_income == 0
113+
assert result == 0
122114

123115

124116
def test_variable_name_conflict(tax_benefit_system):

tests/core/test_reforms.py

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
# -*- coding: utf-8 -*-
2-
31
import warnings
42

5-
from pytest import fixture, raises
3+
import pytest
64

75
from openfisca_core import periods
86
from openfisca_core.periods import Instant
9-
from openfisca_core.simulation_builder import SimulationBuilder
107
from openfisca_core.tools import assert_near
118
from openfisca_core.parameters import ValuesHistory, ParameterNode
129
from openfisca_country_template.entities import Household, Person
1310
from openfisca_core.model_api import * # noqa analysis:ignore
14-
from openfisca_country_template import CountryTaxBenefitSystem
15-
tax_benefit_system = CountryTaxBenefitSystem()
16-
17-
18-
@fixture
19-
def make_simulation():
20-
def _make_simulation(tbs, period, data):
21-
builder = SimulationBuilder()
22-
builder.default_period = period
23-
return builder.build_from_variables(tbs, data)
24-
return _make_simulation
2511

2612

2713
class goes_to_school(Variable):
@@ -32,27 +18,29 @@ class goes_to_school(Variable):
3218
definition_period = MONTH
3319

3420

35-
tax_benefit_system.add_variable(goes_to_school)
36-
37-
3821
class WithBasicIncomeNeutralized(Reform):
3922
def apply(self):
4023
self.neutralize_variable('basic_income')
4124

4225

43-
def test_formula_neutralization(make_simulation):
26+
@pytest.fixture(scope = "module", autouse = True)
27+
def add_variables_to_tax_benefit_system(tax_benefit_system):
28+
tax_benefit_system.add_variables(goes_to_school)
29+
30+
31+
def test_formula_neutralization(make_simulation, tax_benefit_system):
4432
reform = WithBasicIncomeNeutralized(tax_benefit_system)
4533

4634
period = '2017-01'
47-
simulation = make_simulation(reform.base_tax_benefit_system, period, {})
35+
simulation = make_simulation(reform.base_tax_benefit_system, {}, period)
4836
simulation.debug = True
4937

5038
basic_income = simulation.calculate('basic_income', period = period)
5139
assert_near(basic_income, 600)
5240
disposable_income = simulation.calculate('disposable_income', period = period)
5341
assert disposable_income > 0
5442

55-
reform_simulation = make_simulation(reform, period, {})
43+
reform_simulation = make_simulation(reform, {}, period)
5644
reform_simulation.debug = True
5745

5846
basic_income_reform = reform_simulation.calculate('basic_income', period = '2013-01')
@@ -61,25 +49,25 @@ def test_formula_neutralization(make_simulation):
6149
assert_near(disposable_income_reform, 0)
6250

6351

64-
def test_neutralization_variable_with_default_value(make_simulation):
52+
def test_neutralization_variable_with_default_value(make_simulation, tax_benefit_system):
6553
class test_goes_to_school_neutralization(Reform):
6654
def apply(self):
6755
self.neutralize_variable('goes_to_school')
6856

6957
reform = test_goes_to_school_neutralization(tax_benefit_system)
7058

7159
period = "2017-01"
72-
simulation = make_simulation(reform.base_tax_benefit_system, period, {})
60+
simulation = make_simulation(reform.base_tax_benefit_system, {}, period)
7361

7462
goes_to_school = simulation.calculate('goes_to_school', period)
7563
assert_near(goes_to_school, [True], absolute_error_margin = 0)
7664

7765

78-
def test_neutralization_optimization(make_simulation):
66+
def test_neutralization_optimization(make_simulation, tax_benefit_system):
7967
reform = WithBasicIncomeNeutralized(tax_benefit_system)
8068

8169
period = '2017-01'
82-
simulation = make_simulation(reform, period, {})
70+
simulation = make_simulation(reform, {}, period)
8371
simulation.debug = True
8472

8573
simulation.calculate('basic_income', period = '2013-01')
@@ -90,7 +78,7 @@ def test_neutralization_optimization(make_simulation):
9078
assert basic_income_holder.get_known_periods() == []
9179

9280

93-
def test_input_variable_neutralization(make_simulation):
81+
def test_input_variable_neutralization(make_simulation, tax_benefit_system):
9482

9583
class test_salary_neutralization(Reform):
9684
def apply(self):
@@ -103,15 +91,15 @@ def apply(self):
10391
reform = test_salary_neutralization(tax_benefit_system)
10492

10593
with warnings.catch_warnings(record=True) as raised_warnings:
106-
reform_simulation = make_simulation(reform, period, {'salary': [1200, 1000]})
94+
reform_simulation = make_simulation(reform, {'salary': [1200, 1000]}, period)
10795
assert 'You cannot set a value for the variable' in raised_warnings[0].message.args[0]
10896
salary = reform_simulation.calculate('salary', period)
10997
assert_near(salary, [0, 0],)
11098
disposable_income_reform = reform_simulation.calculate('disposable_income', period = period)
11199
assert_near(disposable_income_reform, [600, 600])
112100

113101

114-
def test_permanent_variable_neutralization(make_simulation):
102+
def test_permanent_variable_neutralization(make_simulation, tax_benefit_system):
115103

116104
class test_date_naissance_neutralization(Reform):
117105
def apply(self):
@@ -120,9 +108,9 @@ def apply(self):
120108
reform = test_date_naissance_neutralization(tax_benefit_system)
121109

122110
period = '2017-01'
123-
simulation = make_simulation(reform.base_tax_benefit_system, period, {'birth': '1980-01-01'})
111+
simulation = make_simulation(reform.base_tax_benefit_system, {'birth': '1980-01-01'}, period)
124112
with warnings.catch_warnings(record=True) as raised_warnings:
125-
reform_simulation = make_simulation(reform, period, {'birth': '1980-01-01'})
113+
reform_simulation = make_simulation(reform, {'birth': '1980-01-01'}, period)
126114
assert 'You cannot set a value for the variable' in raised_warnings[0].message.args[0]
127115
assert str(simulation.calculate('birth', None)[0]) == '1980-01-01'
128116
assert str(reform_simulation.calculate('birth', None)[0]) == '1970-01-01'
@@ -223,7 +211,7 @@ def check_update_items(description, value_history, start_instant, stop_instant,
223211
)
224212

225213

226-
def test_add_variable(make_simulation):
214+
def test_add_variable(make_simulation, tax_benefit_system):
227215
class new_variable(Variable):
228216
value_type = int
229217
label = "Nouvelle variable introduite par la réforme"
@@ -241,13 +229,13 @@ def apply(self):
241229
reform = test_add_variable(tax_benefit_system)
242230

243231
assert tax_benefit_system.get_variable('new_variable') is None
244-
reform_simulation = make_simulation(reform, 2013, {})
232+
reform_simulation = make_simulation(reform, {}, 2013)
245233
reform_simulation.debug = True
246234
new_variable1 = reform_simulation.calculate('new_variable', period = '2013-01')
247235
assert_near(new_variable1, 10, absolute_error_margin = 0)
248236

249237

250-
def test_add_dated_variable(make_simulation):
238+
def test_add_dated_variable(make_simulation, tax_benefit_system):
251239
class new_dated_variable(Variable):
252240
value_type = int
253241
label = "Nouvelle variable introduite par la réforme"
@@ -266,13 +254,13 @@ def apply(self):
266254

267255
reform = test_add_variable(tax_benefit_system)
268256

269-
reform_simulation = make_simulation(reform, period, {})
257+
reform_simulation = make_simulation(reform, {}, '2013-01')
270258
reform_simulation.debug = True
271259
new_dated_variable1 = reform_simulation.calculate('new_dated_variable', period = '2013-01')
272260
assert_near(new_dated_variable1, 15, absolute_error_margin = 0)
273261

274262

275-
def test_update_variable(make_simulation):
263+
def test_update_variable(make_simulation, tax_benefit_system):
276264

277265
class disposable_income(Variable):
278266
definition_period = MONTH
@@ -294,7 +282,7 @@ def apply(self):
294282
assert disposable_income_reform.name == disposable_income_baseline.name
295283
assert disposable_income_reform.label == disposable_income_baseline.label
296284

297-
reform_simulation = make_simulation(reform, 2018, {})
285+
reform_simulation = make_simulation(reform, {}, 2018)
298286
disposable_income1 = reform_simulation.calculate('disposable_income', period = '2018-01')
299287
assert_near(disposable_income1, 10, absolute_error_margin = 0)
300288

@@ -303,7 +291,7 @@ def apply(self):
303291
assert(disposable_income2 > 100)
304292

305293

306-
def test_replace_variable():
294+
def test_replace_variable(tax_benefit_system):
307295

308296
class disposable_income(Variable):
309297
definition_period = MONTH
@@ -324,16 +312,16 @@ def apply(self):
324312
assert disposable_income_reform.get_formula('2017') is None
325313

326314

327-
def test_wrong_reform():
315+
def test_wrong_reform(tax_benefit_system):
328316
class wrong_reform(Reform):
329317
# A Reform must implement an `apply` method
330318
pass
331319

332-
with raises(Exception):
320+
with pytest.raises(Exception):
333321
wrong_reform(tax_benefit_system)
334322

335323

336-
def test_modify_parameters():
324+
def test_modify_parameters(tax_benefit_system):
337325

338326
def modify_parameters(reference_parameters):
339327
reform_parameters_subtree = ParameterNode(
@@ -361,7 +349,7 @@ def apply(self):
361349
assert parameters_at_instant.new_node.new_param is True
362350

363351

364-
def test_attributes_conservation():
352+
def test_attributes_conservation(tax_benefit_system):
365353

366354
class some_variable(Variable):
367355
value_type = int
@@ -391,7 +379,7 @@ def apply(self):
391379
assert reform_variable.calculate_output == baseline_variable.calculate_output
392380

393381

394-
def test_formulas_removal():
382+
def test_formulas_removal(tax_benefit_system):
395383
class reform(Reform):
396384
def apply(self):
397385

tests/fixtures/simulations.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import functools
2+
13
import pytest
24

35
from openfisca_core.simulation_builder import SimulationBuilder
@@ -11,6 +13,15 @@ def simulation_builder():
1113
@pytest.fixture
1214
def simulation(simulation_builder, tax_benefit_system, request):
1315
variables, period = request.param
16+
return _simulation(simulation_builder, tax_benefit_system, variables, period)
17+
18+
19+
@pytest.fixture
20+
def make_simulation(simulation_builder):
21+
return functools.partial(_simulation, simulation_builder)
22+
23+
24+
def _simulation(simulation_builder, tax_benefit_system, variables, period):
1425
simulation_builder.set_default_period(period)
1526
simulation = \
1627
simulation_builder \

0 commit comments

Comments
 (0)