From 34d0b4f717b457b500caca9d6f7fd19c13f6cf3a Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Sun, 15 Sep 2024 17:27:20 +0200 Subject: [PATCH] test(simulation): add test to calculate add --- .../simulations/tests/test_invariants.py | 248 ------------------ openfisca_core/variables/variable.py | 7 +- tests/core/test_calculate_output.py | 9 +- tests/core/test_countries.py | 5 +- tests/core/test_cycles.py | 17 +- tests/core/test_formulas.py | 17 +- tests/core/test_holders.py | 5 +- tests/core/test_opt_out_cache.py | 7 +- tests/core/test_projectors.py | 16 +- tests/core/test_reforms.py | 15 +- tests/core/test_simulation_builder.py | 9 +- .../tools/test_runner/test_yaml_runner.py | 5 +- tests/core/variables/test_annualize.py | 9 +- .../core/variables/test_definition_period.py | 43 --- tests/core/variables/test_variables.py | 32 +-- tests/fixtures/appclient.py | 2 +- tests/fixtures/variables.py | 4 +- 17 files changed, 85 insertions(+), 365 deletions(-) delete mode 100644 openfisca_core/simulations/tests/test_invariants.py delete mode 100644 tests/core/variables/test_definition_period.py diff --git a/openfisca_core/simulations/tests/test_invariants.py b/openfisca_core/simulations/tests/test_invariants.py deleted file mode 100644 index ff9564f6e2..0000000000 --- a/openfisca_core/simulations/tests/test_invariants.py +++ /dev/null @@ -1,248 +0,0 @@ -from typing import TypedDict - -from openfisca_core.simulations.types import Variable - -import enum - -import pytest - -from openfisca_core import periods - - -class EitherTag(enum.Enum): - FAILURE = enum.auto() - SUCCESS = enum.auto() - - -class Either: - def __init__(self, value, tag=EitherTag.SUCCESS): - self._value = value - self._tag = tag - - @property - def isSuccess(self): - return self._tag == EitherTag.SUCCESS - - @property - def isFailure(self): - return self._tag == EitherTag.FAILURE - - def map(self, f): - if self.isSuccess: - return Either.succeed(f(**self.unwrap())) - - return self - - def join(self): - if self.isSuccess and isinstance(self.unwrap(), Either): - return self.unwrap() - - return self - - def then(self, f): - return self.map(f).join() - - def unwrap(self): - return self._value - - @staticmethod - def fail(value): - return Either(value, EitherTag.FAILURE) - - @staticmethod - def succeed(value): - return Either(value, EitherTag.SUCCESS) - - -class TestVariable(Variable): - def __init__(self, name, definition_period): - self.name = name - self.definition_period = definition_period - - -class ValidationParams(TypedDict): - variable: Variable - period: periods.Period - - -def are_periods_compatible_1(**params): - variable = params["variable"] - period = params["period"] - - if ( - variable.definition_period in (periods.MONTH, periods.DAY) - and period.unit == periods.WEEK - ): - return Either.fail( - f"Unable to compute variable '{variable.name}' for period " - f"{period}, as {period} and {variable.definition_period} are " - "incompatible periods. You can, however, change the requested " - "period to 'period.this_year'." - ) - - return Either.succeed(params) - - -def are_periods_compatible_2(**params): - variable = params["variable"] - period = params["period"] - - if ( - variable.definition_period in (periods.WEEK, periods.WEEKDAY) - and period.unit == periods.MONTH - ): - return Either.fail( - f"Unable to compute variable '{variable.name}' for period " - f"{period}, as {period} and {variable.definition_period} are " - "incompatible periods. You can, however, change the requested " - "period to 'period.this_year' or 'period.first_week'." - ) - - return Either.succeed(params) - - -def derive_calculate_add(**params): - either = ( - Either(params).then(are_periods_compatible_1).then(are_periods_compatible_2) - ) - - if either.isSuccess: - return either.unwrap() - - raise ValueError(either.unwrap()) - - -@pytest.mark.parametrize( - "period_unit, period_str, expected", - [ - (periods.YEAR, "2020", True), - (periods.YEAR, "2020-01", True), - (periods.YEAR, "2020-01-01", True), - (periods.YEAR, "2020-W01", True), - (periods.YEAR, "2020-W01-1", True), - (periods.MONTH, "2020", True), - (periods.MONTH, "2020-01", True), - (periods.MONTH, "2020-01-01", True), - (periods.MONTH, "2020-W01", False), - (periods.MONTH, "2020-W01-1", True), - (periods.DAY, "2020", True), - (periods.DAY, "2020-01", True), - (periods.DAY, "2020-01-01", True), - (periods.DAY, "2020-W01", False), - (periods.DAY, "2020-W01-1", True), - (periods.WEEK, "2020", True), - (periods.WEEK, "2020-01", True), - (periods.WEEK, "2020-01-01", True), - (periods.WEEK, "2020-W01", True), - (periods.WEEK, "2020-W01-1", True), - (periods.WEEKDAY, "2020", True), - (periods.WEEKDAY, "2020-01", True), - (periods.WEEKDAY, "2020-01-01", True), - (periods.WEEKDAY, "2020-W01", True), - (periods.WEEKDAY, "2020-W01-1", True), - ], -) -def test_are_periods_compatible_1(period_unit, period_str, expected): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - either = are_periods_compatible_1(variable=variable, period=period) - assert either.isSuccess is expected - - -@pytest.mark.parametrize( - "period_unit, period_str, expected", - [ - (periods.YEAR, "2020", True), - (periods.YEAR, "2020-01", True), - (periods.YEAR, "2020-01-01", True), - (periods.YEAR, "2020-W01", True), - (periods.YEAR, "2020-W01-1", True), - (periods.MONTH, "2020", True), - (periods.MONTH, "2020-01", True), - (periods.MONTH, "2020-01-01", True), - (periods.MONTH, "2020-W01", True), - (periods.MONTH, "2020-W01-1", True), - (periods.DAY, "2020", True), - (periods.DAY, "2020-01", True), - (periods.DAY, "2020-01-01", True), - (periods.DAY, "2020-W01", True), - (periods.DAY, "2020-W01-1", True), - (periods.WEEK, "2020", True), - (periods.WEEK, "2020-01", False), - (periods.WEEK, "2020-01-01", True), - (periods.WEEK, "2020-W01", True), - (periods.WEEK, "2020-W01-1", True), - (periods.WEEKDAY, "2020", True), - (periods.WEEKDAY, "2020-01", False), - (periods.WEEKDAY, "2020-01-01", True), - (periods.WEEKDAY, "2020-W01", True), - (periods.WEEKDAY, "2020-W01-1", True), - ], -) -def test_are_periods_compatible_2(period_unit, period_str, expected): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - either = are_periods_compatible_2(variable=variable, period=period) - assert either.isSuccess is expected - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.YEAR, "2020"), - (periods.YEAR, "2020-01"), - (periods.YEAR, "2020-01-01"), - (periods.YEAR, "2020-W01"), - (periods.YEAR, "2020-W01-1"), - (periods.MONTH, "2020"), - (periods.MONTH, "2020-01"), - (periods.MONTH, "2020-01-01"), - (periods.MONTH, "2020-W01-1"), - (periods.DAY, "2020"), - (periods.DAY, "2020-01"), - (periods.DAY, "2020-01-01"), - (periods.DAY, "2020-W01-1"), - (periods.WEEK, "2020"), - (periods.WEEK, "2020-01-01"), - (periods.WEEK, "2020-W01"), - (periods.WEEK, "2020-W01-1"), - (periods.WEEKDAY, "2020"), - (periods.WEEKDAY, "2020-01-01"), - (periods.WEEKDAY, "2020-W01"), - (periods.WEEKDAY, "2020-W01-1"), - ], -) -def test_derive_calculate_add(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - assert derive_calculate_add(variable=variable, period=period) - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.MONTH, "2020-W01"), - (periods.DAY, "2020-W01"), - ], -) -def test_derive_calculate_add_with_invalid_period_1(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - with pytest.raises(ValueError) as error: - derive_calculate_add(variable=variable, period=period) - assert "period.first_week" not in str(error.value) - - -@pytest.mark.parametrize( - "period_unit, period_str", - [ - (periods.WEEK, "2020-01"), - (periods.WEEKDAY, "2020-01"), - ], -) -def test_derive_calculate_add_with_invalid_period_2(period_unit, period_str): - variable = TestVariable("variable", period_unit) - period = periods.period(period_str) - with pytest.raises(ValueError) as error: - derive_calculate_add(variable=variable, period=period) - assert "period.first_week" in str(error.value) diff --git a/openfisca_core/variables/variable.py b/openfisca_core/variables/variable.py index eeddc97ba3..e70c0d05d9 100644 --- a/openfisca_core/variables/variable.py +++ b/openfisca_core/variables/variable.py @@ -14,6 +14,7 @@ from openfisca_core import periods, tools from openfisca_core.entities import Entity, GroupEntity from openfisca_core.indexed_enums import Enum, EnumArray +from openfisca_core.periods import DateUnit, Period from . import config, helpers @@ -135,7 +136,7 @@ def __init__(self, baseline_variable=None): ) self.entity = self.set(attr, "entity", required=True, setter=self.set_entity) self.definition_period = self.set( - attr, "definition_period", required=True, allowed_values=periods.DateUnit + attr, "definition_period", required=True, allowed_values=DateUnit ) self.label = self.set(attr, "label", allowed_type=str, setter=self.set_label) self.end = self.set(attr, "end", allowed_type=str, setter=self.set_end) @@ -373,7 +374,7 @@ def get_introspection_data(cls): def get_formula( self, - period: Union[Instant, periods.Period, str, int] = None, + period: Union[Instant, Period, str, int] = None, ) -> Optional[Formula]: """Returns the formula to compute the variable at the given period. @@ -398,7 +399,7 @@ def get_formula( 1 ] # peekitem gets the 1st key-value tuple (the oldest start_date and formula). Return the formula. - if isinstance(period, periods.Period): + if isinstance(period, Period): instant = period.start else: try: diff --git a/tests/core/test_calculate_output.py b/tests/core/test_calculate_output.py index 926db52535..ecf59b5f7d 100644 --- a/tests/core/test_calculate_output.py +++ b/tests/core/test_calculate_output.py @@ -2,27 +2,28 @@ from openfisca_country_template import entities, situation_examples -from openfisca_core import periods, simulations, tools +from openfisca_core import simulations, tools +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable class simple_variable(Variable): entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH value_type = int class variable_with_calculate_output_add(Variable): entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH value_type = int calculate_output = simulations.calculate_output_add class variable_with_calculate_output_divide(Variable): entity = entities.Person - definition_period = periods.YEAR + definition_period = DateUnit.YEAR value_type = int calculate_output = simulations.calculate_output_divide diff --git a/tests/core/test_countries.py b/tests/core/test_countries.py index bc03682460..8263ac3c44 100644 --- a/tests/core/test_countries.py +++ b/tests/core/test_countries.py @@ -2,6 +2,7 @@ from openfisca_core import periods, populations, tools from openfisca_core.errors import VariableNameConflictError, VariableNotFoundError +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -100,7 +101,7 @@ def test_variable_with_reference(make_simulation, isolated_tax_benefit_system): assert result > 0 class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() @@ -116,7 +117,7 @@ def formula(household, period): def test_variable_name_conflict(tax_benefit_system): class disposable_income(Variable): reference = "disposable_income" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() diff --git a/tests/core/test_cycles.py b/tests/core/test_cycles.py index 0a9166d020..14886532c6 100644 --- a/tests/core/test_cycles.py +++ b/tests/core/test_cycles.py @@ -4,6 +4,7 @@ from openfisca_core import periods, tools from openfisca_core.errors import CycleError +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -22,7 +23,7 @@ def simulation(tax_benefit_system): class variable1(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable2", period) @@ -31,7 +32,7 @@ def formula(person, period): class variable2(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable1", period) @@ -41,7 +42,7 @@ def formula(person, period): class variable3(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable4", period.last_month) @@ -50,7 +51,7 @@ def formula(person, period): class variable4(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("variable3", period) @@ -61,7 +62,7 @@ def formula(person, period): class variable5(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable6 = person("variable6", period.last_month) @@ -71,7 +72,7 @@ def formula(person, period): class variable6(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable5 = person("variable5", period) @@ -81,7 +82,7 @@ def formula(person, period): class variable7(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): variable5 = person("variable5", period) @@ -92,7 +93,7 @@ def formula(person, period): class cotisation(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): if period.start.month == 12: diff --git a/tests/core/test_formulas.py b/tests/core/test_formulas.py index b9b991f652..c8a5379801 100644 --- a/tests/core/test_formulas.py +++ b/tests/core/test_formulas.py @@ -3,7 +3,8 @@ from openfisca_country_template import entities -from openfisca_core import commons, periods +from openfisca_core import commons +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder from openfisca_core.variables import Variable @@ -11,14 +12,14 @@ class choice(Variable): value_type = int entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class uses_multiplication(Variable): value_type = int entity = entities.Person label = "Variable with formula that uses multiplication" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): choice = person("choice", period) @@ -30,7 +31,7 @@ class returns_scalar(Variable): value_type = int entity = entities.Person label = "Variable with formula that returns a scalar value" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return 666 @@ -40,7 +41,7 @@ class uses_switch(Variable): value_type = int entity = entities.Person label = "Variable with formula that uses switch" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): choice = person("choice", period) @@ -107,8 +108,8 @@ def test_group_encapsulation(): And calculations are projected to all the member families. """ - from openfisca_core import periods from openfisca_core.entities import build_entity + from openfisca_core.periods import DateUnit from openfisca_core.taxbenefitsystems import TaxBenefitSystem person_entity = build_entity( @@ -150,12 +151,12 @@ def test_group_encapsulation(): class household_level_variable(Variable): value_type = int entity = household_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_family_level_variable(Variable): value_type = int entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period) diff --git a/tests/core/test_holders.py b/tests/core/test_holders.py index 2db121ece7..088ca15935 100644 --- a/tests/core/test_holders.py +++ b/tests/core/test_holders.py @@ -8,6 +8,7 @@ from openfisca_core.errors import PeriodMismatchError from openfisca_core.holders import Holder from openfisca_core.memory_config import MemoryConfig +from openfisca_core.periods import DateUnit from openfisca_core.simulations import SimulationBuilder @@ -105,9 +106,9 @@ def test_permanent_variable_filled(single): simulation = single holder = simulation.person.get_holder("birth") value = numpy.asarray(["1980-01-01"], dtype=holder.variable.dtype) - holder.set_input(periods.period(periods.ETERNITY), value) + holder.set_input(periods.period(DateUnit.ETERNITY), value) assert holder.get_array(None) == value - assert holder.get_array(periods.ETERNITY) == value + assert holder.get_array(DateUnit.ETERNITY) == value assert holder.get_array("2016-01") == value diff --git a/tests/core/test_opt_out_cache.py b/tests/core/test_opt_out_cache.py index 55ad202db4..e9fe3a2469 100644 --- a/tests/core/test_opt_out_cache.py +++ b/tests/core/test_opt_out_cache.py @@ -3,6 +3,7 @@ from openfisca_country_template.entities import Person from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable PERIOD = periods.period("2016-01") @@ -12,14 +13,14 @@ class input(Variable): value_type = int entity = Person label = "Input variable" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class intermediate(Variable): value_type = int entity = Person label = "Intermediate result that don't need to be cached" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("input", period) @@ -29,7 +30,7 @@ class output(Variable): value_type = int entity = Person label = "Output variable" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period): return person("intermediate", period) diff --git a/tests/core/test_projectors.py b/tests/core/test_projectors.py index 878b258bf1..27391711c3 100644 --- a/tests/core/test_projectors.py +++ b/tests/core/test_projectors.py @@ -1,8 +1,8 @@ import numpy as np -from openfisca_core import periods from openfisca_core.entities import build_entity from openfisca_core.indexed_enums import Enum +from openfisca_core.periods import DateUnit from openfisca_core.simulations.simulation_builder import SimulationBuilder from openfisca_core.taxbenefitsystems import TaxBenefitSystem from openfisca_core.variables import Variable @@ -138,14 +138,14 @@ class household_enum_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_enum_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = person - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(person, period): return person.household("household_enum_variable", period) @@ -209,7 +209,7 @@ class household_projected_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(household, period): return household.value_from_first_person( @@ -221,7 +221,7 @@ class person_enum_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = person - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY system.add_variables(household_projected_variable, person_enum_variable) @@ -300,14 +300,14 @@ class household_level_variable(Variable): possible_values = enum default_value = enum.FIRST_OPTION entity = household_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY class projected_family_level_variable(Variable): value_type = Enum possible_values = enum default_value = enum.FIRST_OPTION entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period) @@ -315,7 +315,7 @@ def formula(family, period): class decoded_projected_family_level_variable(Variable): value_type = str entity = family_entity - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY def formula(family, period): return family.household("household_level_variable", period).decode_to_str() diff --git a/tests/core/test_reforms.py b/tests/core/test_reforms.py index 1b3a1f291f..0c17bb1169 100644 --- a/tests/core/test_reforms.py +++ b/tests/core/test_reforms.py @@ -6,6 +6,7 @@ from openfisca_core import holders, periods, simulations from openfisca_core.parameters import ParameterNode, ValuesHistory +from openfisca_core.periods import DateUnit, Instant from openfisca_core.reforms import Reform from openfisca_core.tools import assert_near from openfisca_core.variables import Variable @@ -16,7 +17,7 @@ class goes_to_school(Variable): default_value = True entity = Person label = "The person goes to school (only relevant for children)" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH class WithBasicIncomeNeutralized(Reform): @@ -318,7 +319,7 @@ class new_variable(Variable): value_type = int label = "Nouvelle variable introduite par la réforme" entity = Household - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(household, period): return household.empty_array() + 10 @@ -341,7 +342,7 @@ class new_dated_variable(Variable): value_type = int label = "Nouvelle variable introduite par la réforme" entity = Household - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula_2010_01_01(household, period): return household.empty_array() + 10 @@ -365,7 +366,7 @@ def apply(self): def test_update_variable(make_simulation, tax_benefit_system): class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula_2018(household, period): return household.empty_array() + 10 @@ -402,7 +403,7 @@ def apply(self): def test_replace_variable(tax_benefit_system): class disposable_income(Variable): - definition_period = periods.MONTH + definition_period = DateUnit.MONTH entity = Person label = "Disposable income" value_type = float @@ -454,7 +455,7 @@ def apply(self): parameters_new_node = reform.parameters.children["new_node"] assert parameters_new_node is not None - instant = periods.Instant((2013, 1, 1)) + instant = Instant((2013, 1, 1)) parameters_at_instant = reform.get_parameters_at_instant(instant) assert parameters_at_instant.new_node.new_param is True @@ -464,7 +465,7 @@ class some_variable(Variable): value_type = int entity = Person label = "Variable with many attributes" - definition_period = periods.MONTH + definition_period = DateUnit.MONTH set_input = holders.set_input_divide_by_period calculate_output = simulations.calculate_output_add diff --git a/tests/core/test_simulation_builder.py b/tests/core/test_simulation_builder.py index ecfccdcfdc..d1dc0cde75 100644 --- a/tests/core/test_simulation_builder.py +++ b/tests/core/test_simulation_builder.py @@ -6,9 +6,10 @@ from openfisca_country_template import entities, situation_examples -from openfisca_core import periods, tools +from openfisca_core import tools from openfisca_core.errors import SituationParsingError from openfisca_core.indexed_enums import Enum +from openfisca_core.periods import DateUnit from openfisca_core.populations import Population from openfisca_core.simulations import Simulation, SimulationBuilder from openfisca_core.tools import test_runner @@ -18,7 +19,7 @@ @pytest.fixture def int_variable(persons): class intvar(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = int entity = persons @@ -31,7 +32,7 @@ def __init__(self): @pytest.fixture def date_variable(persons): class datevar(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = datetime.date entity = persons @@ -44,7 +45,7 @@ def __init__(self): @pytest.fixture def enum_variable(): class TestEnum(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = Enum dtype = "O" default_value = "0" diff --git a/tests/core/tools/test_runner/test_yaml_runner.py b/tests/core/tools/test_runner/test_yaml_runner.py index ac4ec00854..bf04ade9bb 100644 --- a/tests/core/tools/test_runner/test_yaml_runner.py +++ b/tests/core/tools/test_runner/test_yaml_runner.py @@ -5,8 +5,9 @@ import numpy import pytest -from openfisca_core import errors, periods +from openfisca_core import errors from openfisca_core.entities import Entity +from openfisca_core.periods import DateUnit from openfisca_core.populations import Population from openfisca_core.tools.test_runner import YamlFile, YamlItem, _get_tax_benefit_system from openfisca_core.variables import Variable @@ -72,7 +73,7 @@ def __init__(self, test): class TestVariable(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = float def __init__(self): diff --git a/tests/core/variables/test_annualize.py b/tests/core/variables/test_annualize.py index 39a6316bf4..7bf85d9a46 100644 --- a/tests/core/variables/test_annualize.py +++ b/tests/core/variables/test_annualize.py @@ -4,6 +4,7 @@ from openfisca_country_template.entities import Person from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable, get_annualized_variable @@ -14,7 +15,7 @@ def monthly_variable(): class monthly_variable(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH def formula(person, period, parameters): variable.calculation_count += 1 @@ -46,7 +47,7 @@ def test_without_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 11 @@ -61,7 +62,7 @@ def test_with_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 0 @@ -78,7 +79,7 @@ def test_with_partial_annualize(monthly_variable): yearly_sum = sum( person("monthly_variable", month) - for month in period.get_subperiods(periods.MONTH) + for month in period.get_subperiods(DateUnit.MONTH) ) assert monthly_variable.calculation_count == 11 diff --git a/tests/core/variables/test_definition_period.py b/tests/core/variables/test_definition_period.py deleted file mode 100644 index 7938aaeaef..0000000000 --- a/tests/core/variables/test_definition_period.py +++ /dev/null @@ -1,43 +0,0 @@ -import pytest - -from openfisca_core import periods -from openfisca_core.variables import Variable - - -@pytest.fixture -def variable(persons): - class TestVariable(Variable): - value_type = float - entity = persons - - return TestVariable - - -def test_weekday_variable(variable): - variable.definition_period = periods.WEEKDAY - assert variable() - - -def test_week_variable(variable): - variable.definition_period = periods.WEEK - assert variable() - - -def test_day_variable(variable): - variable.definition_period = periods.DAY - assert variable() - - -def test_month_variable(variable): - variable.definition_period = periods.MONTH - assert variable() - - -def test_year_variable(variable): - variable.definition_period = periods.YEAR - assert variable() - - -def test_eternity_variable(variable): - variable.definition_period = periods.ETERNITY - assert variable() diff --git a/tests/core/variables/test_variables.py b/tests/core/variables/test_variables.py index 08a44a0ce8..3b2790bae7 100644 --- a/tests/core/variables/test_variables.py +++ b/tests/core/variables/test_variables.py @@ -8,7 +8,7 @@ import openfisca_country_template.situation_examples from openfisca_country_template.entities import Person -from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.simulation_builder import SimulationBuilder from openfisca_core.tools import assert_near from openfisca_core.variables import Variable @@ -67,7 +67,7 @@ def get_message(error): class variable__no_date(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without date." @@ -88,7 +88,7 @@ def test_variable__no_date(): class variable__strange_end_attribute(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with dubious end attribute, no formula." end = "1989-00-00" @@ -113,7 +113,7 @@ def test_variable__strange_end_attribute(): class variable__end_attribute(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, no formula." end = "1989-12-31" @@ -141,7 +141,7 @@ def test_variable__end_attribute_set_input(simulation): class end_attribute__one_simple_formula(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, one formula without date." end = "1989-12-31" @@ -187,7 +187,7 @@ def test_dates__end_attribute__one_simple_formula(): class no_end_attribute__one_formula__strange_name(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, one stangely named formula." def formula_2015_toto(individu, period): @@ -208,7 +208,7 @@ def test_add__no_end_attribute__one_formula__strange_name(): class no_end_attribute__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, one dated formula." def formula_2000_01_01(individu, period): @@ -241,7 +241,7 @@ class no_end_attribute__one_formula__eternity(Variable): value_type = int entity = Person definition_period = ( - periods.ETERNITY + DateUnit.ETERNITY ) # For this entity, this variable shouldn't evolve through time label = "Variable without end attribute, one dated formula." @@ -278,7 +278,7 @@ def test_call__no_end_attribute__one_formula__eternity_after(simulation): class no_end_attribute__formulas__start_formats(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable without end attribute, multiple dated formulas." def formula_2000(individu, period): @@ -339,7 +339,7 @@ def test_call__no_end_attribute__formulas__start_formats(simulation): class no_attribute__formulas__different_names__dates_overlap(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable, no end attribute, multiple dated formulas with different names but same dates." def formula_2000(individu, period): @@ -364,7 +364,7 @@ def test_add__no_attribute__formulas__different_names__dates_overlap(): class no_attribute__formulas__different_names__no_overlap(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable, no end attribute, multiple dated formulas with different names and no date overlap." def formula_2000_01_01(individu, period): @@ -404,7 +404,7 @@ def test_call__no_attribute__formulas__different_names__no_overlap(simulation): class end_attribute__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, one dated formula." end = "2001-12-31" @@ -432,7 +432,7 @@ def test_call__end_attribute__one_formula__start(simulation): class stop_attribute_before__one_formula__start(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with stop attribute only coming before formula start." end = "1990-01-01" @@ -454,7 +454,7 @@ def test_add__stop_attribute_before__one_formula__start(): class end_attribute_restrictive__one_formula(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = ( "Variable with end attribute, one dated formula and dates intervals overlap." ) @@ -484,7 +484,7 @@ def test_call__end_attribute_restrictive__one_formula(simulation): class end_attribute__formulas__different_names(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "Variable with end attribute, multiple dated formulas with different names." end = "2010-12-31" @@ -535,7 +535,7 @@ def test_unexpected_attr(): class variable_with_strange_attr(Variable): value_type = int entity = Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH unexpected = "???" with raises(ValueError): diff --git a/tests/fixtures/appclient.py b/tests/fixtures/appclient.py index 035bd6f259..5edcfc2c98 100644 --- a/tests/fixtures/appclient.py +++ b/tests/fixtures/appclient.py @@ -20,7 +20,7 @@ def test_client(tax_benefit_system): class new_variable(Variable): value_type = float entity = entities.Person - definition_period = periods.MONTH + definition_period = DateUnit.MONTH label = "New variable" reference = "https://law.gov.example/new_variable" # Always use the most official source diff --git a/tests/fixtures/variables.py b/tests/fixtures/variables.py index cd0d9b70ce..aab7cda58d 100644 --- a/tests/fixtures/variables.py +++ b/tests/fixtures/variables.py @@ -1,9 +1,9 @@ -from openfisca_core import periods +from openfisca_core.periods import DateUnit from openfisca_core.variables import Variable class TestVariable(Variable): - definition_period = periods.ETERNITY + definition_period = DateUnit.ETERNITY value_type = float def __init__(self, entity):