Skip to content

Commit

Permalink
test(simulation): add test to calculate add
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 16, 2024
1 parent 49d3a00 commit 7a62848
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 477 deletions.
15 changes: 3 additions & 12 deletions openfisca_core/periods/tests/test__parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,9 @@
["1001-01", Period((DateUnit.MONTH, Instant((1001, 1, 1)), 1))],
["1001-12", Period((DateUnit.MONTH, Instant((1001, 12, 1)), 1))],
["1001-01-01", Period((DateUnit.DAY, Instant((1001, 1, 1)), 1))],
[
"1001-W01",
Period((DateUnit.WEEK, Instant((1000, 12, 29)), 1)),
],
[
"1001-W52",
Period((DateUnit.WEEK, Instant((1001, 12, 21)), 1)),
],
[
"1001-W01-1",
Period((DateUnit.WEEKDAY, Instant((1000, 12, 29)), 1)),
],
["1001-W01", Period((DateUnit.WEEK, Instant((1000, 12, 29)), 1))],
["1001-W52", Period((DateUnit.WEEK, Instant((1001, 12, 21)), 1))],
["1001-W01-1", Period((DateUnit.WEEKDAY, Instant((1000, 12, 29)), 1))],
],
)
def test__parse_period(arg, expected):
Expand Down
105 changes: 15 additions & 90 deletions openfisca_core/periods/tests/test_instant.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,26 @@
@pytest.mark.parametrize(
"instant, offset, unit, expected",
[
[
Instant((2020, 2, 29)),
"first-of",
DateUnit.YEAR,
Instant((2020, 1, 1)),
],
[
Instant((2020, 2, 29)),
"first-of",
DateUnit.MONTH,
Instant((2020, 2, 1)),
],
[
Instant((2020, 2, 29)),
"first-of",
DateUnit.WEEK,
Instant((2020, 2, 24)),
],
[Instant((2020, 2, 29)), "first-of", DateUnit.YEAR, Instant((2020, 1, 1))],
[Instant((2020, 2, 29)), "first-of", DateUnit.MONTH, Instant((2020, 2, 1))],
[Instant((2020, 2, 29)), "first-of", DateUnit.WEEK, Instant((2020, 2, 24))],
[Instant((2020, 2, 29)), "first-of", DateUnit.DAY, None],
[Instant((2020, 2, 29)), "first-of", DateUnit.WEEKDAY, None],
[
Instant((2020, 2, 29)),
"last-of",
DateUnit.YEAR,
Instant((2020, 12, 31)),
],
[
Instant((2020, 2, 29)),
"last-of",
DateUnit.MONTH,
Instant((2020, 2, 29)),
],
[
Instant((2020, 2, 29)),
"last-of",
DateUnit.WEEK,
Instant((2020, 3, 1)),
],
[Instant((2020, 2, 29)), "last-of", DateUnit.YEAR, Instant((2020, 12, 31))],
[Instant((2020, 2, 29)), "last-of", DateUnit.MONTH, Instant((2020, 2, 29))],
[Instant((2020, 2, 29)), "last-of", DateUnit.WEEK, Instant((2020, 3, 1))],
[Instant((2020, 2, 29)), "last-of", DateUnit.DAY, None],
[Instant((2020, 2, 29)), "last-of", DateUnit.WEEKDAY, None],
[
Instant((2020, 2, 29)),
-3,
DateUnit.YEAR,
Instant((2017, 2, 28)),
],
[
Instant((2020, 2, 29)),
-3,
DateUnit.MONTH,
Instant((2019, 11, 29)),
],
[
Instant((2020, 2, 29)),
-3,
DateUnit.WEEK,
Instant((2020, 2, 8)),
],
[
Instant((2020, 2, 29)),
-3,
DateUnit.DAY,
Instant((2020, 2, 26)),
],
[
Instant((2020, 2, 29)),
-3,
DateUnit.WEEKDAY,
Instant((2020, 2, 26)),
],
[
Instant((2020, 2, 29)),
3,
DateUnit.YEAR,
Instant((2023, 2, 28)),
],
[
Instant((2020, 2, 29)),
3,
DateUnit.MONTH,
Instant((2020, 5, 29)),
],
[
Instant((2020, 2, 29)),
3,
DateUnit.WEEK,
Instant((2020, 3, 21)),
],
[Instant((2020, 2, 29)), -3, DateUnit.YEAR, Instant((2017, 2, 28))],
[Instant((2020, 2, 29)), -3, DateUnit.MONTH, Instant((2019, 11, 29))],
[Instant((2020, 2, 29)), -3, DateUnit.WEEK, Instant((2020, 2, 8))],
[Instant((2020, 2, 29)), -3, DateUnit.DAY, Instant((2020, 2, 26))],
[Instant((2020, 2, 29)), -3, DateUnit.WEEKDAY, Instant((2020, 2, 26))],
[Instant((2020, 2, 29)), 3, DateUnit.YEAR, Instant((2023, 2, 28))],
[Instant((2020, 2, 29)), 3, DateUnit.MONTH, Instant((2020, 5, 29))],
[Instant((2020, 2, 29)), 3, DateUnit.WEEK, Instant((2020, 3, 21))],
[Instant((2020, 2, 29)), 3, DateUnit.DAY, Instant((2020, 3, 3))],
[
Instant((2020, 2, 29)),
3,
DateUnit.WEEKDAY,
Instant((2020, 3, 3)),
],
[Instant((2020, 2, 29)), 3, DateUnit.WEEKDAY, Instant((2020, 3, 3))],
],
)
def test_offset(instant, offset, unit, expected):
Expand Down
17 changes: 9 additions & 8 deletions openfisca_core/scripts/measure_performances.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from openfisca_core import periods, simulations
from openfisca_core.entities import build_entity
from openfisca_core.periods import DateUnit
from openfisca_core.taxbenefitsystems import TaxBenefitSystem
from openfisca_core.tools import assert_near
from openfisca_core.variables import Variable
Expand Down Expand Up @@ -80,7 +81,7 @@ class city_code(Variable):
value_type = "FixedStr"
max_length = 5
entity = Famille
definition_period = periods.ETERNITY
definition_period = DateUnit.ETERNITY
label = """Code INSEE "city_code" de la commune de résidence de la famille"""


Expand Down Expand Up @@ -114,7 +115,7 @@ class dom_tom(Variable):
label = "La famille habite-t-elle les DOM-TOM ?"

def formula(self, simulation, period):
period = period.start.period(periods.YEAR).offset("first-of")
period = period.start.period(DateUnit.YEAR).offset("first-of")
city_code = simulation.calculate("city_code", period)
return np.logical_or(startswith(city_code, "97"), startswith(city_code, "98"))

Expand All @@ -125,7 +126,7 @@ class revenu_disponible(Variable):
label = "Revenu disponible de l'individu"

def formula(self, simulation, period):
period = period.start.period(periods.YEAR).offset("first-of")
period = period.start.period(DateUnit.YEAR).offset("first-of")
rsa = simulation.calculate("rsa", period)
salaire_imposable = simulation.calculate("salaire_imposable", period)
return rsa + salaire_imposable * 0.7
Expand All @@ -137,17 +138,17 @@ class rsa(Variable):
label = "RSA"

def formula_2010_01_01(self, simulation, period):
period = period.start.period(periods.MONTH).offset("first-of")
period = period.start.period(DateUnit.MONTH).offset("first-of")
salaire_imposable = simulation.calculate("salaire_imposable", period)
return (salaire_imposable < 500) * 100.0

def formula_2011_01_01(self, simulation, period):
period = period.start.period(periods.MONTH).offset("first-of")
period = period.start.period(DateUnit.MONTH).offset("first-of")
salaire_imposable = simulation.calculate("salaire_imposable", period)
return (salaire_imposable < 500) * 200.0

def formula_2013_01_01(self, simulation, period):
period = period.start.period(periods.MONTH).offset("first-of")
period = period.start.period(DateUnit.MONTH).offset("first-of")
salaire_imposable = simulation.calculate("salaire_imposable", period)
return (salaire_imposable < 500) * 300

Expand All @@ -158,7 +159,7 @@ class salaire_imposable(Variable):
label = "Salaire imposable"

def formula(individu, period):
period = period.start.period(periods.YEAR).offset("first-of")
period = period.start.period(DateUnit.YEAR).offset("first-of")
dom_tom = individu.famille("dom_tom", period)
salaire_net = individu("salaire_net", period)
return salaire_net * 0.9 - 100 * dom_tom
Expand All @@ -170,7 +171,7 @@ class salaire_net(Variable):
label = "Salaire net"

def formula(self, simulation, period):
period = period.start.period(periods.YEAR).offset("first-of")
period = period.start.period(DateUnit.YEAR).offset("first-of")
salaire_brut = simulation.calculate("salaire_brut", period)
return salaire_brut * 0.8

Expand Down
Loading

0 comments on commit 7a62848

Please sign in to comment.