-
Notifications
You must be signed in to change notification settings - Fork 82
Refactor pytest fixtures - Part 1 #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cesco-fran
wants to merge
36
commits into
openfisca:master
Choose a base branch
from
cesco-fran:refactor-pytest-fixtures
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
0bf1222
Separate test_countries fixtures and make them accessible to others t…
88adca2
Refactor test_simulation_builder fixtures
1cd22b7
Add new fixtures in conftest
542490f
Introduce simulator_builder fixture in test_simulations
47af3cf
Create single fixture
91b0735
Refactor test_formulas fixtures
ffc1d1c
flake8
benjello 361941b
Add single fixture to test_calculate_output
f4aff8c
Add single fixture to test_entities
f5debc3
Add couple to conftest
65ac243
Add couple to dump_restore
31d53aa
Add couple to test_entities
cb48a5d
Add couple to test_simulation_builder
be404c0
Add simulation_single simulation_couple
81894f9
Fix undefined variables
eca7b51
Add single to test_calculate_ and fix fix F401
ccabe80
Add simulation_single_with_variables
de07a0d
Add simulation to conftest
2088980
Fix F401 on test_cycles
47dc01f
Add simulation_with_variables
1cdba50
Add conftest fixture at test_axes
d8b2a42
Remove unnecessary tax_benefit_system fixture in test_countries
d141e9c
Add make_simulation_from_yaml in test_entities
6cf2e17
Add make_simulation_from_entities in test_entities
a09bd59
Add case and ages_case fixtures in test_entities
35fe22c
Add period and year_period fixtures in test_entities
5c70aa5
Fix typo
1bae580
Fix flake8 errors
e4e8eda
Add make_simulation_from_yaml in test_axes
fb9089f
flake8
benjello af2af13
Add tax_benefit_system fixtures to test_extensions
9244c4e
Refactor fixtures in test_opt_out_cache
ab640b9
Refactor fixtures in test_parameters add tmp_path
bf03583
Refactor fixtures in test_reform
f77117b
Use simulations fixtures in test_simulation
a162adb
Refactor with the make_variable fixture
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from pytest import fixture | ||
|
||
from openfisca_country_template import CountryTaxBenefitSystem | ||
from openfisca_country_template.entities import * # noqa analysis:ignore | ||
|
||
from openfisca_core.simulation_builder import SimulationBuilder | ||
from openfisca_core.tools.test_runner import yaml | ||
from openfisca_core.variables import Variable | ||
from openfisca_core.periods import ETERNITY | ||
from openfisca_core.entities import Entity, GroupEntity | ||
from openfisca_core.model_api import * # noqa analysis:ignore | ||
|
||
|
||
# PERIODS | ||
|
||
@fixture | ||
def period(): | ||
return "2016-01" | ||
|
||
|
||
# ENTITIES | ||
|
||
|
||
class TestVariable(Variable): | ||
definition_period = ETERNITY | ||
value_type = float | ||
|
||
def __init__(self, entity): | ||
self.__class__.entity = entity | ||
super().__init__() | ||
|
||
|
||
@fixture | ||
def persons(): | ||
class TestPersonEntity(Entity): | ||
def get_variable(self, variable_name): | ||
result = TestVariable(self) | ||
result.name = variable_name | ||
return result | ||
|
||
def check_variable_defined_for_entity(self, variable_name): | ||
return True | ||
|
||
return TestPersonEntity("person", "persons", "", "") | ||
|
||
|
||
@fixture | ||
def group_entity(): | ||
class Household(GroupEntity): | ||
def get_variable(self, variable_name): | ||
result = TestVariable(self) | ||
result.name = variable_name | ||
return result | ||
|
||
def check_variable_defined_for_entity(self, variable_name): | ||
return True | ||
|
||
roles = [{ | ||
'key': 'parent', | ||
'plural': 'parents', | ||
'max': 2 | ||
}, { | ||
'key': 'child', | ||
'plural': 'children' | ||
}] | ||
|
||
return Household("household", "households", "", "", roles) | ||
|
||
|
||
@fixture | ||
def single(): | ||
return { | ||
"persons": { | ||
"Alicia": { | ||
"birth": { | ||
"2017-01": None | ||
}, | ||
"disposable_income": { | ||
"2017-01": None | ||
} | ||
} | ||
}, | ||
"households": { | ||
"_": { | ||
"parents": ["Alicia"] | ||
} | ||
} | ||
} | ||
|
||
|
||
@fixture | ||
def couple(): | ||
return { | ||
"persons": { | ||
"Alicia": { | ||
"birth": { | ||
"ETERNITY": "1980-01-01" | ||
}, | ||
"salary": { | ||
"2017-01": 4000 | ||
}, | ||
"disposable_income": { | ||
"2017-01": None | ||
} | ||
}, | ||
"Javier": { | ||
"birth": { | ||
"ETERNITY": "1984-01-01" | ||
}, | ||
"salary": { | ||
"2017-01": 2500 | ||
}, | ||
"disposable_income": { | ||
"2017-01": None | ||
} | ||
} | ||
}, | ||
"households": { | ||
"_": { | ||
"parents": ["Alicia", "Javier"], | ||
"total_benefits": { | ||
"2017-01": None | ||
}, | ||
"total_taxes": { | ||
"2017-01": None | ||
} | ||
} | ||
} | ||
} | ||
|
||
# TAX BENEFIt SYSTEMS | ||
|
||
|
||
@fixture | ||
def tax_benefit_system(): | ||
return CountryTaxBenefitSystem() | ||
|
||
|
||
# SIMULATIONS | ||
|
||
|
||
@fixture | ||
def simulation_builder(): | ||
return SimulationBuilder() | ||
|
||
|
||
@fixture | ||
def simulation(tax_benefit_system, simulation_builder): | ||
return SimulationBuilder().build_default_simulation(tax_benefit_system) | ||
|
||
|
||
@fixture | ||
def make_simulation_from_entities(tax_benefit_system, simulation_builder): | ||
def _make_simulation_from_entities(entities): | ||
return simulation_builder.build_from_entities(tax_benefit_system, entities) | ||
return _make_simulation_from_entities | ||
|
||
|
||
@fixture | ||
def make_simulation_from_tbs_and_variables(simulation_builder, period): | ||
def _make_simulation_from_variables_and_tbs(tbs, variables): | ||
simulation_builder.default_period = period | ||
return simulation_builder.build_from_variables(tbs, variables) | ||
return _make_simulation_from_variables_and_tbs | ||
|
||
|
||
@fixture | ||
def make_simulation_from_yaml(tax_benefit_system, simulation_builder): | ||
def _make_simulation_from_yaml(simulation_yaml): | ||
return simulation_builder.build_from_dict(tax_benefit_system, yaml.safe_load(simulation_yaml)) | ||
return _make_simulation_from_yaml | ||
|
||
|
||
@fixture | ||
def simulation_single(make_simulation_from_entities, single): | ||
return make_simulation_from_entities(single) | ||
|
||
|
||
@fixture | ||
def simulation_couple(make_simulation_from_entities, couple): | ||
return make_simulation_from_entities(couple) | ||
|
||
|
||
# VARIABLES | ||
|
||
|
||
@fixture | ||
def make_variable(): | ||
def _make_variable(name, **new_methods_and_attrs): | ||
methods_and_attrs = {} | ||
default = dict(value_type = int, entity = Person, definition_period = MONTH) | ||
methods_and_attrs.update(default) | ||
methods_and_attrs.update(new_methods_and_attrs) | ||
return type(name, (Variable, ), methods_and_attrs) | ||
return _make_variable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,47 @@ | ||
from openfisca_core.model_api import * # noqa analysis:ignore | ||
from openfisca_core.simulation_builder import SimulationBuilder | ||
from openfisca_core.tools import assert_near | ||
|
||
from openfisca_country_template import CountryTaxBenefitSystem | ||
from openfisca_country_template.entities import * # noqa analysis:ignore | ||
from openfisca_country_template.situation_examples import single | ||
|
||
from pytest import fixture, raises | ||
|
||
|
||
@fixture | ||
def simulation(): | ||
return SimulationBuilder().build_from_entities(tax_benefit_system, single) | ||
from openfisca_core.tools import assert_near | ||
from openfisca_core.simulations import calculate_output_add | ||
from openfisca_core.simulations import calculate_output_divide | ||
from openfisca_core.periods import YEAR | ||
|
||
|
||
class simple_variable(Variable): | ||
entity = Person | ||
definition_period = MONTH | ||
value_type = int | ||
@fixture | ||
def simple_variable(make_variable): | ||
return make_variable('simple_variable') | ||
|
||
|
||
class variable_with_calculate_output_add(Variable): | ||
entity = Person | ||
definition_period = MONTH | ||
value_type = int | ||
calculate_output = calculate_output_add | ||
@fixture | ||
def variable_with_calculate_output_add(make_variable): | ||
return make_variable('variable_with_calculate_output_add', calculate_output = calculate_output_add) | ||
|
||
|
||
class variable_with_calculate_output_divide(Variable): | ||
entity = Person | ||
definition_period = YEAR | ||
value_type = int | ||
calculate_output = calculate_output_divide | ||
@fixture | ||
def variable_with_calculate_output_divide(make_variable): | ||
return make_variable('variable_with_calculate_output_divide', | ||
definition_period = YEAR, | ||
calculate_output = calculate_output_divide) | ||
|
||
|
||
tax_benefit_system = CountryTaxBenefitSystem() | ||
tax_benefit_system.add_variables( | ||
simple_variable, | ||
variable_with_calculate_output_add, | ||
variable_with_calculate_output_divide | ||
) | ||
@fixture | ||
def simulation_single_with_variables(simulation_builder, tax_benefit_system, single, | ||
simple_variable, variable_with_calculate_output_add, variable_with_calculate_output_divide): | ||
tax_benefit_system.add_variables(simple_variable, variable_with_calculate_output_add, variable_with_calculate_output_divide) | ||
return simulation_builder.build_from_entities(tax_benefit_system, single) | ||
|
||
|
||
def test_calculate_output_default(simulation): | ||
def test_calculate_output_default(simulation_single_with_variables): | ||
with raises(ValueError): | ||
simulation.calculate_output('simple_variable', 2017) | ||
simulation_single_with_variables.calculate_output('simple_variable', 2017) | ||
|
||
|
||
def test_calculate_output_add(simulation): | ||
simulation.set_input('variable_with_calculate_output_add', '2017-01', [10]) | ||
simulation.set_input('variable_with_calculate_output_add', '2017-05', [20]) | ||
simulation.set_input('variable_with_calculate_output_add', '2017-12', [70]) | ||
assert_near(simulation.calculate_output('variable_with_calculate_output_add', 2017), 100) | ||
def test_calculate_output_add(simulation_single_with_variables): | ||
simulation_single_with_variables.set_input('variable_with_calculate_output_add', '2017-01', [10]) | ||
simulation_single_with_variables.set_input('variable_with_calculate_output_add', '2017-05', [20]) | ||
simulation_single_with_variables.set_input('variable_with_calculate_output_add', '2017-12', [70]) | ||
assert_near(simulation_single_with_variables.calculate_output('variable_with_calculate_output_add', 2017), 100) | ||
|
||
|
||
def test_calculate_output_divide(simulation): | ||
simulation.set_input('variable_with_calculate_output_divide', 2017, [12000]) | ||
assert_near(simulation.calculate_output('variable_with_calculate_output_divide', '2017-06'), 1000) | ||
def test_calculate_output_divide(simulation_single_with_variables): | ||
simulation_single_with_variables.set_input('variable_with_calculate_output_divide', 2017, [12000]) | ||
assert_near(simulation_single_with_variables.calculate_output('variable_with_calculate_output_divide', '2017-06'), 1000) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.