Skip to content

Commit

Permalink
Redistribute tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Jul 31, 2022
1 parent 26d25e9 commit affbebf
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 149 deletions.
1 change: 0 additions & 1 deletion openfisca_core/periods/period_.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def date(self):
@property
def days(self):
"""Count the number of days in period."""

return (self.stop.date - self.start.date).days + 1

def intersection(self, start, stop):
Expand Down
2 changes: 2 additions & 0 deletions openfisca_core/periods/tests/helpers/test_instant.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def test_instant_with_a_valid_argument(arg, expected):
["1000-01-01:a", ValueError],
["1000-01-01:1", ValueError],
[(), AssertionError],
[{}, AssertionError],
["", ValueError],
[(None, None, None, None), AssertionError],
])
def test_instant_with_an_invalid_argument(arg, error):
Expand Down
35 changes: 31 additions & 4 deletions openfisca_core/periods/tests/helpers/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,24 @@
["1000-1-1", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
["1000-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
["1000-01-01", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
["1004-02-29", Period((periods.DAY, Instant((1004, 2, 29)), 1))],
["year:1000", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000-01", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000-01-01", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000-01:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000-01-01:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
["year:1000-01-01:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
["year:1000:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
["year:1000-01:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
["month:1000-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
["month:1000-01-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
["month:1000-01:1", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
["month:1000-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
["day:1000-01-01", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
["day:1000-01-01:3", Period((periods.DAY, Instant((1000, 1, 1)), 3))],
])
def test_instant_with_a_valid_argument(arg, expected):
assert periods.period(arg) == expected
Expand All @@ -35,16 +50,28 @@ def test_instant_with_a_valid_argument(arg, expected):
[periods.YEAR, ValueError],
[datetime.date(1, 1, 1), ValueError],
["1000-0", ValueError],
["1000-13", ValueError],
["1000-0-0", ValueError],
["1000-1-0", ValueError],
["1000-2-31", ValueError],
["1", ValueError],
["a", ValueError],
["year", ValueError],
["999", ValueError],
["1:1000-01-01", ValueError],
["a:1000-01-01", ValueError],
["1000-01-01:a", ValueError],
["1:1000", ValueError],
["a:1000", ValueError],
["month:1000", ValueError],
["day:1000-01", ValueError],
["1000:a", ValueError],
["1000:1", ValueError],
["1000-01:1", ValueError],
["1000-01-01:1", ValueError],
["month:1000:1", ValueError],
["day:1000:1", ValueError],
["day:1000-01:1", ValueError],
[(), ValueError],
[{}, ValueError],
["", ValueError],
[(None,), ValueError],
[(None, None), ValueError],
[(None, None, None), ValueError],
Expand Down
20 changes: 20 additions & 0 deletions openfisca_core/periods/tests/period/test_size_in_days.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from openfisca_core import periods
from openfisca_core.periods import Instant, Period


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[periods.DAY, Instant((2022, 12, 31)), 1, 1],
[periods.DAY, Instant((2022, 12, 31)), 3, 3],
[periods.MONTH, Instant((2022, 12, 1)), 1, 31],
[periods.MONTH, Instant((2012, 2, 3)), 1, 29],
[periods.MONTH, Instant((2022, 1, 3)), 3, 31 + 28 + 31],
[periods.MONTH, Instant((2012, 1, 3)), 3, 31 + 29 + 31],
[periods.YEAR, Instant((2022, 12, 1)), 1, 365],
[periods.YEAR, Instant((2012, 1, 1)), 1, 366],
[periods.YEAR, Instant((2022, 1, 1)), 2, 730],
])
def test_day_size_in_days(date_unit, instant, size, expected):
period = Period((date_unit, instant, size))
assert period.size_in_days == expected
27 changes: 14 additions & 13 deletions openfisca_core/periods/tests/period/test_str.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import pytest

from openfisca_core.periods import DateUnit, Instant, Period
from openfisca_core import periods
from openfisca_core.periods import Instant, Period


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[DateUnit.YEAR, Instant((2022, 1, 1)), 1, "2022"],
[DateUnit.MONTH, Instant((2022, 1, 1)), 12, "2022"],
[DateUnit.YEAR, Instant((2022, 3, 1)), 1, "year:2022-03"],
[DateUnit.MONTH, Instant((2022, 3, 1)), 12, "year:2022-03"],
[DateUnit.YEAR, Instant((2022, 1, 1)), 3, "year:2022:3"],
[DateUnit.YEAR, Instant((2022, 1, 3)), 3, "year:2022:3"],
[periods.YEAR, Instant((2022, 1, 1)), 1, "2022"],
[periods.MONTH, Instant((2022, 1, 1)), 12, "2022"],
[periods.YEAR, Instant((2022, 3, 1)), 1, "year:2022-03"],
[periods.MONTH, Instant((2022, 3, 1)), 12, "year:2022-03"],
[periods.YEAR, Instant((2022, 1, 1)), 3, "year:2022:3"],
[periods.YEAR, Instant((2022, 1, 3)), 3, "year:2022:3"],
])
def test_str_with_years(date_unit, instant, size, expected):
assert str(Period((date_unit, instant, size))) == expected


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[DateUnit.MONTH, Instant((2022, 1, 1)), 1, "2022-01"],
[DateUnit.MONTH, Instant((2022, 1, 1)), 3, "month:2022-01:3"],
[DateUnit.MONTH, Instant((2022, 3, 1)), 3, "month:2022-03:3"],
[periods.MONTH, Instant((2022, 1, 1)), 1, "2022-01"],
[periods.MONTH, Instant((2022, 1, 1)), 3, "month:2022-01:3"],
[periods.MONTH, Instant((2022, 3, 1)), 3, "month:2022-03:3"],
])
def test_str_with_months(date_unit, instant, size, expected):
assert str(Period((date_unit, instant, size))) == expected


@pytest.mark.parametrize("date_unit, instant, size, expected", [
[DateUnit.DAY, Instant((2022, 1, 1)), 1, "2022-01-01"],
[DateUnit.DAY, Instant((2022, 1, 1)), 3, "day:2022-01-01:3"],
[DateUnit.DAY, Instant((2022, 3, 1)), 3, "day:2022-03-01:3"],
[periods.DAY, Instant((2022, 1, 1)), 1, "2022-01-01"],
[periods.DAY, Instant((2022, 1, 1)), 3, "day:2022-01-01:3"],
[periods.DAY, Instant((2022, 3, 1)), 3, "day:2022-03-01:3"],
])
def test_str_with_days(date_unit, instant, size, expected):
assert str(Period((date_unit, instant, size))) == expected
132 changes: 1 addition & 131 deletions tests/core/test_periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,137 +3,7 @@

import pytest

from openfisca_core.periods import Period, Instant, YEAR, MONTH, DAY, period

first_jan = Instant((2014, 1, 1))
first_march = Instant((2014, 3, 1))


'''
Test String -> Period
'''

# Years


def test_parsing_year():
assert period('2014') == Period((YEAR, first_jan, 1))


def test_parsing_rolling_year():
assert period('year:2014-03') == Period((YEAR, first_march, 1))


def test_parsing_several_years():
assert period('year:2014:2') == Period((YEAR, first_jan, 2))


def test_wrong_syntax_several_years():
with pytest.raises(ValueError):
period('2014:2')


# Months

def test_parsing_month():
assert period('2014-01') == Period((MONTH, first_jan, 1))


def test_parsing_several_months():
assert period('month:2014-03:3') == Period((MONTH, first_march, 3))


def test_wrong_syntax_several_months():
with pytest.raises(ValueError):
period('2014-3:3')


# Days

def test_parsing_day():
assert period('2014-01-01') == Period((DAY, first_jan, 1))


def test_parsing_several_days():
assert period('day:2014-03-01:3') == Period((DAY, first_march, 3))


def test_wrong_syntax_several_days():
with pytest.raises(ValueError):
period('2014-2-3:2')


def test_day_size_in_days():
assert Period(('day', Instant((2014, 12, 31)), 1)).size_in_days == 1


def test_3_day_size_in_days():
assert Period(('day', Instant((2014, 12, 31)), 3)).size_in_days == 3


def test_month_size_in_days():
assert Period(('month', Instant((2014, 12, 1)), 1)).size_in_days == 31


def test_leap_month_size_in_days():
assert Period(('month', Instant((2012, 2, 3)), 1)).size_in_days == 29


def test_3_month_size_in_days():
assert Period(('month', Instant((2013, 1, 3)), 3)).size_in_days == 31 + 28 + 31


def test_leap_3_month_size_in_days():
assert Period(('month', Instant((2012, 1, 3)), 3)).size_in_days == 31 + 29 + 31


def test_year_size_in_days():
assert Period(('year', Instant((2014, 12, 1)), 1)).size_in_days == 365


def test_leap_year_size_in_days():
assert Period(('year', Instant((2012, 1, 1)), 1)).size_in_days == 366


def test_2_years_size_in_days():
assert Period(('year', Instant((2014, 1, 1)), 2)).size_in_days == 730

# Misc


def test_wrong_date():
with pytest.raises(ValueError):
period("2006-31-03")


def test_ambiguous_period():
with pytest.raises(ValueError):
period('month:2014')


def test_deprecated_signature():
with pytest.raises(TypeError):
period(MONTH, 2014)


def test_wrong_argument():
with pytest.raises(ValueError):
period({})


def test_wrong_argument_1():
with pytest.raises(ValueError):
period([])


def test_none():
with pytest.raises(ValueError):
period(None)


def test_empty_string():
with pytest.raises(ValueError):
period('')
from openfisca_core.periods import YEAR, MONTH, DAY, period


@pytest.mark.parametrize("test", [
Expand Down

0 comments on commit affbebf

Please sign in to comment.