Skip to content

Commit cb51aa9

Browse files
author
Mauko Quiroga
committed
Fix opt out cache tests
1 parent 3d13905 commit cb51aa9

3 files changed

Lines changed: 33 additions & 55 deletions

File tree

tests/core/test_countries.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@
77
PERIOD = periods.period("2016-01")
88

99

10-
@pytest.fixture
11-
def simulation(simulation_builder, tax_benefit_system, request):
12-
variables, period = request.param
13-
simulation_builder.set_default_period(period)
14-
simulation = \
15-
simulation_builder \
16-
.build_from_variables(tax_benefit_system, variables)
17-
18-
return simulation
19-
20-
2110
@pytest.mark.parametrize("simulation", [({"salary": 2000}, PERIOD)], indirect = True)
2211
def test_input_variable(simulation):
2312
result = simulation.calculate("salary", PERIOD)

tests/core/test_opt_out_cache.py

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
1-
# -*- coding: utf-8 -*-
1+
import pytest
22

3-
from pytest import fixture
4-
5-
from openfisca_core.simulation_builder import SimulationBuilder
6-
from openfisca_country_template import CountryTaxBenefitSystem
73
from openfisca_country_template.entities import Person
8-
from openfisca_core.variables import Variable
9-
from openfisca_core.periods import MONTH
10-
114

12-
@fixture
13-
def month():
14-
return '2016-05'
5+
from openfisca_core import periods
6+
from openfisca_core.periods import MONTH
7+
from openfisca_core.variables import Variable
158

169

17-
@fixture
18-
def make_isolated_simulation(month):
19-
def _make_simulation(tbs, data):
20-
builder = SimulationBuilder()
21-
builder.default_period = month
22-
return builder.build_from_variables(tbs, data)
23-
return _make_simulation
10+
PERIOD = periods.period("2016-01")
2411

2512

2613
class input(Variable):
@@ -50,43 +37,34 @@ def formula(person, period):
5037
return person('intermediate', period)
5138

5239

53-
def get_filled_tbs():
54-
tax_benefit_system = CountryTaxBenefitSystem()
40+
@pytest.fixture(scope = "module", autouse = True)
41+
def add_variables_to_tax_benefit_system(tax_benefit_system):
5542
tax_benefit_system.add_variables(input, intermediate, output)
5643

57-
return tax_benefit_system
58-
5944

60-
# TaxBenefitSystem instance declared after formulas
45+
@pytest.fixture(scope = "module", autouse = True)
46+
def add_variables_to_cache_blakclist(tax_benefit_system):
47+
tax_benefit_system.cache_blacklist = set(['intermediate'])
6148

6249

63-
tax_benefit_system = get_filled_tbs()
64-
65-
66-
tax_benefit_system.cache_blacklist = set(['intermediate'])
67-
68-
69-
def test_without_cache_opt_out(make_isolated_simulation, month):
70-
simulation = make_isolated_simulation(tax_benefit_system, {'input': 1})
71-
simulation.calculate('output', period = month)
50+
@pytest.mark.parametrize("simulation", [({'input': 1}, PERIOD)], indirect = True)
51+
def test_without_cache_opt_out(simulation):
52+
simulation.calculate('output', period = PERIOD)
7253
intermediate_cache = simulation.persons.get_holder('intermediate')
73-
assert(intermediate_cache.get_array(month) is not None)
54+
assert(intermediate_cache.get_array(PERIOD) is not None)
7455

7556

76-
def test_with_cache_opt_out(make_isolated_simulation, month):
77-
simulation = make_isolated_simulation(tax_benefit_system, {'input': 1})
57+
@pytest.mark.parametrize("simulation", [({'input': 1}, PERIOD)], indirect = True)
58+
def test_with_cache_opt_out(simulation):
7859
simulation.debug = True
7960
simulation.opt_out_cache = True
80-
simulation.calculate('output', period = month)
61+
simulation.calculate('output', period = PERIOD)
8162
intermediate_cache = simulation.persons.get_holder('intermediate')
82-
assert(intermediate_cache.get_array(month) is None)
83-
84-
85-
tax_benefit_system2 = get_filled_tbs()
63+
assert(intermediate_cache.get_array(PERIOD) is None)
8664

8765

88-
def test_with_no_blacklist(make_isolated_simulation, month):
89-
simulation = make_isolated_simulation(tax_benefit_system2, {'input': 1})
90-
simulation.calculate('output', period = month)
66+
@pytest.mark.parametrize("simulation", [({'input': 1}, PERIOD)], indirect = True)
67+
def test_with_no_blacklist(simulation):
68+
simulation.calculate('output', period = PERIOD)
9169
intermediate_cache = simulation.persons.get_holder('intermediate')
92-
assert(intermediate_cache.get_array(month) is not None)
70+
assert(intermediate_cache.get_array(PERIOD) is not None)

tests/fixtures/simulations.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,14 @@
66
@pytest.fixture
77
def simulation_builder():
88
return SimulationBuilder()
9+
10+
11+
@pytest.fixture
12+
def simulation(simulation_builder, tax_benefit_system, request):
13+
variables, period = request.param
14+
simulation_builder.set_default_period(period)
15+
simulation = \
16+
simulation_builder \
17+
.build_from_variables(tax_benefit_system, variables)
18+
19+
return simulation

0 commit comments

Comments
 (0)