From 583ceddeacc4bb35de35f031bf81fc4d31ffde67 Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Sat, 30 Jul 2022 20:26:35 +0200 Subject: [PATCH] Consolidate tests --- .../periods/tests/helpers/__init__.py | 0 .../helpers/test__parse_simple_period.py | 18 -- .../periods/tests/helpers/test_instant.py | 67 ------ .../tests/helpers/test_instant_date.py | 32 --- .../tests/helpers/test_key_period_size.py | 16 -- .../periods/tests/helpers/test_period.py | 96 -------- .../periods/tests/period/__init__.py | 0 .../periods/tests/period/test_size_in_days.py | 19 -- .../periods/tests/period/test_str.py | 33 --- openfisca_core/periods/tests/test_helpers.py | 210 ++++++++++++++++++ openfisca_core/periods/tests/test_period.py | 80 +++++++ tests/core/test_periods.py | 26 --- 12 files changed, 290 insertions(+), 307 deletions(-) delete mode 100644 openfisca_core/periods/tests/helpers/__init__.py delete mode 100644 openfisca_core/periods/tests/helpers/test__parse_simple_period.py delete mode 100644 openfisca_core/periods/tests/helpers/test_instant.py delete mode 100644 openfisca_core/periods/tests/helpers/test_instant_date.py delete mode 100644 openfisca_core/periods/tests/helpers/test_key_period_size.py delete mode 100644 openfisca_core/periods/tests/helpers/test_period.py delete mode 100644 openfisca_core/periods/tests/period/__init__.py delete mode 100644 openfisca_core/periods/tests/period/test_size_in_days.py delete mode 100644 openfisca_core/periods/tests/period/test_str.py create mode 100644 openfisca_core/periods/tests/test_helpers.py create mode 100644 openfisca_core/periods/tests/test_period.py delete mode 100644 tests/core/test_periods.py diff --git a/openfisca_core/periods/tests/helpers/__init__.py b/openfisca_core/periods/tests/helpers/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/openfisca_core/periods/tests/helpers/test__parse_simple_period.py b/openfisca_core/periods/tests/helpers/test__parse_simple_period.py deleted file mode 100644 index 09e2bb344a..0000000000 --- a/openfisca_core/periods/tests/helpers/test__parse_simple_period.py +++ /dev/null @@ -1,18 +0,0 @@ -import pytest - -from openfisca_core.periods import DateUnit, Instant, Period, helpers - - -@pytest.mark.parametrize("arg, expected", [ - ["1", None], - ["999", None], - ["1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["1000-1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["1000-1-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["1000-01-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["1000-01-99", None], - ]) -def test__parse_simple_period_with_a_valid_argument(arg, expected): - assert helpers._parse_simple_period(arg) == expected diff --git a/openfisca_core/periods/tests/helpers/test_instant.py b/openfisca_core/periods/tests/helpers/test_instant.py deleted file mode 100644 index 422a4ac8ba..0000000000 --- a/openfisca_core/periods/tests/helpers/test_instant.py +++ /dev/null @@ -1,67 +0,0 @@ -import datetime - -import pytest - -from openfisca_core import periods -from openfisca_core.periods import DateUnit, Instant, Period - - -@pytest.mark.parametrize("arg, expected", [ - [None, None], - [datetime.date(1, 1, 1), Instant((1, 1, 1))], - [Instant((1, 1, 1)), Instant((1, 1, 1))], - [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), Instant((1, 1, 1))], - [-1, Instant((-1, 1, 1))], - [0, Instant((0, 1, 1))], - [1, Instant((1, 1, 1))], - [999, Instant((999, 1, 1))], - [1000, Instant((1000, 1, 1))], - ["1000", Instant((1000, 1, 1))], - ["1000-01", Instant((1000, 1, 1))], - ["1000-01-01", Instant((1000, 1, 1))], - [(None,), Instant((None, 1, 1))], - [(None, None), Instant((None, None, 1))], - [(None, None, None), Instant((None, None, None))], - [(datetime.date(1, 1, 1),), Instant((datetime.date(1, 1, 1), 1, 1))], - [(Instant((1, 1, 1)),), Instant((Instant((1, 1, 1)), 1, 1))], - [(Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),), Instant((Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), 1, 1))], - [(-1,), Instant((-1, 1, 1))], - [(-1, -1), Instant((-1, -1, 1))], - [(-1, -1, -1), Instant((-1, -1, -1))], - [("-1",), Instant(("-1", 1, 1))], - [("-1", "-1"), Instant(("-1", "-1", 1))], - [("-1", "-1", "-1"), Instant(("-1", "-1", "-1"))], - [("1-1",), Instant(("1-1", 1, 1))], - [("1-1-1",), Instant(("1-1-1", 1, 1))], - ]) -def test_instant_with_a_valid_argument(arg, expected): - assert periods.instant(arg) == expected - - -@pytest.mark.parametrize("arg, error", [ - [DateUnit.YEAR, ValueError], - [DateUnit.ETERNITY, ValueError], - ["1000-0", ValueError], - ["1000-0-0", ValueError], - ["1000-1", ValueError], - ["1000-1-1", ValueError], - ["1", ValueError], - ["a", ValueError], - ["year", ValueError], - ["eternity", ValueError], - ["999", ValueError], - ["1:1000-01-01", ValueError], - ["a:1000-01-01", ValueError], - ["year:1000-01-01", ValueError], - ["year:1000-01-01:1", ValueError], - ["year:1000-01-01:3", ValueError], - ["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): - with pytest.raises(error): - periods.instant(arg) diff --git a/openfisca_core/periods/tests/helpers/test_instant_date.py b/openfisca_core/periods/tests/helpers/test_instant_date.py deleted file mode 100644 index 722728a6e4..0000000000 --- a/openfisca_core/periods/tests/helpers/test_instant_date.py +++ /dev/null @@ -1,32 +0,0 @@ -import datetime - -import pytest - -from openfisca_core import periods -from openfisca_core.periods import Instant - - -@pytest.mark.parametrize("arg, expected", [ - [None, None], - [Instant((1, 1, 1)), datetime.date(1, 1, 1)], - [Instant((4, 2, 29)), datetime.date(4, 2, 29)], - [(1, 1, 1), datetime.date(1, 1, 1)], - ]) -def test_instant_date_with_a_valid_argument(arg, expected): - assert periods.instant_date(arg) == expected - - -@pytest.mark.parametrize("arg, error", [ - [Instant((-1, 1, 1)), ValueError], - [Instant((1, -1, 1)), ValueError], - [Instant((1, 13, -1)), ValueError], - [Instant((1, 1, -1)), ValueError], - [Instant((1, 1, 32)), ValueError], - [Instant((1, 2, 29)), ValueError], - [Instant(("1", 1, 1)), TypeError], - [(1,), TypeError], - [(1, 1), TypeError], - ]) -def test_instant_date_with_an_invalid_argument(arg, error): - with pytest.raises(error): - periods.instant_date(arg) diff --git a/openfisca_core/periods/tests/helpers/test_key_period_size.py b/openfisca_core/periods/tests/helpers/test_key_period_size.py deleted file mode 100644 index d89a68d7b0..0000000000 --- a/openfisca_core/periods/tests/helpers/test_key_period_size.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest - -from openfisca_core import periods -from openfisca_core.periods import DateUnit, Instant, Period - - -@pytest.mark.parametrize("arg, expected", [ - [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), "100_365"], - [Period((DateUnit.MONTH, Instant((1, 1, 1)), 12)), "200_12"], - [Period((DateUnit.YEAR, Instant((1, 1, 1)), 2)), "300_2"], - [Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 1)), "400_1"], - [(DateUnit.DAY, None, 1), "100_1"], - [(DateUnit.MONTH, None, -1000), "200_-1000"], - ]) -def test_key_period_size_with_a_valid_argument(arg, expected): - assert periods.key_period_size(arg) == expected diff --git a/openfisca_core/periods/tests/helpers/test_period.py b/openfisca_core/periods/tests/helpers/test_period.py deleted file mode 100644 index 12f0dd87dc..0000000000 --- a/openfisca_core/periods/tests/helpers/test_period.py +++ /dev/null @@ -1,96 +0,0 @@ -import datetime - -import pytest - -from openfisca_core import periods -from openfisca_core.periods import DateUnit, Instant, Period - - -@pytest.mark.parametrize("arg, expected", [ - ["eternity", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], - ["ETERNITY", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], - [DateUnit.ETERNITY, Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], - [Instant((1, 1, 1)), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))], - [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), Period((DateUnit.DAY, Instant((1, 1, 1)), 365))], - [-1, Period((DateUnit.YEAR, Instant((-1, 1, 1)), 1))], - [0, Period((DateUnit.YEAR, Instant((0, 1, 1)), 1))], - [1, Period((DateUnit.YEAR, Instant((1, 1, 1)), 1))], - [999, Period((DateUnit.YEAR, Instant((999, 1, 1)), 1))], - [1000, Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["1000-1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["1000-1-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["1004-02-29", Period((DateUnit.DAY, Instant((1004, 2, 29)), 1))], - ["year:1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000-01", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000-01-01", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000-01:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000-01-01:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], - ["year:1000:3", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 3))], - ["year:1000-01:3", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 3))], - ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], - ["month:1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["month:1000-01-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["month:1000-01:1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], - ["month:1000-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], - ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], - ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], - ["day:1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], - ["day:1000-01-01:3", Period((DateUnit.DAY, Instant((1000, 1, 1)), 3))], - ]) -def test_instant_with_a_valid_argument(arg, expected): - assert periods.period(arg) == expected - - -@pytest.mark.parametrize("arg, error", [ - [None, ValueError], - [DateUnit.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", 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], - [(None, None, None, None), ValueError], - [(datetime.date(1, 1, 1),), ValueError], - [(Instant((1, 1, 1)),), ValueError], - [(Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),), ValueError], - [(1,), ValueError], - [(1, 1), ValueError], - [(1, 1, 1), ValueError], - [(-1,), ValueError], - [(-1, -1), ValueError], - [(-1, -1, -1), ValueError], - [("-1",), ValueError], - [("-1", "-1"), ValueError], - [("-1", "-1", "-1"), ValueError], - [("1-1",), ValueError], - [("1-1-1",), ValueError], - ]) -def test_instant_with_an_invalid_argument(arg, error): - with pytest.raises(error): - periods.period(arg) diff --git a/openfisca_core/periods/tests/period/__init__.py b/openfisca_core/periods/tests/period/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/openfisca_core/periods/tests/period/test_size_in_days.py b/openfisca_core/periods/tests/period/test_size_in_days.py deleted file mode 100644 index 79351455a6..0000000000 --- a/openfisca_core/periods/tests/period/test_size_in_days.py +++ /dev/null @@ -1,19 +0,0 @@ -import pytest - -from openfisca_core.periods import DateUnit, Instant, Period - - -@pytest.mark.parametrize("date_unit, instant, size, expected", [ - [DateUnit.DAY, Instant((2022, 12, 31)), 1, 1], - [DateUnit.DAY, Instant((2022, 12, 31)), 3, 3], - [DateUnit.MONTH, Instant((2022, 12, 1)), 1, 31], - [DateUnit.MONTH, Instant((2012, 2, 3)), 1, 29], - [DateUnit.MONTH, Instant((2022, 1, 3)), 3, 31 + 28 + 31], - [DateUnit.MONTH, Instant((2012, 1, 3)), 3, 31 + 29 + 31], - [DateUnit.YEAR, Instant((2022, 12, 1)), 1, 365], - [DateUnit.YEAR, Instant((2012, 1, 1)), 1, 366], - [DateUnit.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 diff --git a/openfisca_core/periods/tests/period/test_str.py b/openfisca_core/periods/tests/period/test_str.py deleted file mode 100644 index 874a8405ab..0000000000 --- a/openfisca_core/periods/tests/period/test_str.py +++ /dev/null @@ -1,33 +0,0 @@ -import pytest - -from openfisca_core.periods import DateUnit, 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"], - ]) -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"], - ]) -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"], - ]) -def test_str_with_days(date_unit, instant, size, expected): - assert str(Period((date_unit, instant, size))) == expected diff --git a/openfisca_core/periods/tests/test_helpers.py b/openfisca_core/periods/tests/test_helpers.py new file mode 100644 index 0000000000..2095e940f3 --- /dev/null +++ b/openfisca_core/periods/tests/test_helpers.py @@ -0,0 +1,210 @@ +import datetime + +import pytest + +from openfisca_core import periods +from openfisca_core.periods import DateUnit, Instant, Period, helpers + + +@pytest.mark.parametrize("arg, expected", [ + [None, None], + [datetime.date(1, 1, 1), Instant((1, 1, 1))], + [Instant((1, 1, 1)), Instant((1, 1, 1))], + [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), Instant((1, 1, 1))], + [-1, Instant((-1, 1, 1))], + [0, Instant((0, 1, 1))], + [1, Instant((1, 1, 1))], + [999, Instant((999, 1, 1))], + [1000, Instant((1000, 1, 1))], + ["1000", Instant((1000, 1, 1))], + ["1000-01", Instant((1000, 1, 1))], + ["1000-01-01", Instant((1000, 1, 1))], + [(None,), Instant((None, 1, 1))], + [(None, None), Instant((None, None, 1))], + [(None, None, None), Instant((None, None, None))], + [(datetime.date(1, 1, 1),), Instant((datetime.date(1, 1, 1), 1, 1))], + [(Instant((1, 1, 1)),), Instant((Instant((1, 1, 1)), 1, 1))], + [(Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),), Instant((Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), 1, 1))], + [(-1,), Instant((-1, 1, 1))], + [(-1, -1), Instant((-1, -1, 1))], + [(-1, -1, -1), Instant((-1, -1, -1))], + [("-1",), Instant(("-1", 1, 1))], + [("-1", "-1"), Instant(("-1", "-1", 1))], + [("-1", "-1", "-1"), Instant(("-1", "-1", "-1"))], + [("1-1",), Instant(("1-1", 1, 1))], + [("1-1-1",), Instant(("1-1-1", 1, 1))], + ]) +def test_instant_with_a_valid_argument(arg, expected): + assert periods.instant(arg) == expected + + +@pytest.mark.parametrize("arg, error", [ + [DateUnit.YEAR, ValueError], + [DateUnit.ETERNITY, ValueError], + ["1000-0", ValueError], + ["1000-0-0", ValueError], + ["1000-1", ValueError], + ["1000-1-1", ValueError], + ["1", ValueError], + ["a", ValueError], + ["year", ValueError], + ["eternity", ValueError], + ["999", ValueError], + ["1:1000-01-01", ValueError], + ["a:1000-01-01", ValueError], + ["year:1000-01-01", ValueError], + ["year:1000-01-01:1", ValueError], + ["year:1000-01-01:3", ValueError], + ["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): + with pytest.raises(error): + periods.instant(arg) + + +@pytest.mark.parametrize("arg, expected", [ + [None, None], + [Instant((1, 1, 1)), datetime.date(1, 1, 1)], + [Instant((4, 2, 29)), datetime.date(4, 2, 29)], + [(1, 1, 1), datetime.date(1, 1, 1)], + ]) +def test_instant_date_with_a_valid_argument(arg, expected): + assert periods.instant_date(arg) == expected + + +@pytest.mark.parametrize("arg, error", [ + [Instant((-1, 1, 1)), ValueError], + [Instant((1, -1, 1)), ValueError], + [Instant((1, 13, -1)), ValueError], + [Instant((1, 1, -1)), ValueError], + [Instant((1, 1, 32)), ValueError], + [Instant((1, 2, 29)), ValueError], + [Instant(("1", 1, 1)), TypeError], + [(1,), TypeError], + [(1, 1), TypeError], + ]) +def test_instant_date_with_an_invalid_argument(arg, error): + with pytest.raises(error): + periods.instant_date(arg) + + +@pytest.mark.parametrize("arg, expected", [ + ["eternity", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], + ["ETERNITY", Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], + [DateUnit.ETERNITY, Period((DateUnit.ETERNITY, Instant((1, 1, 1)), float("inf")))], + [Instant((1, 1, 1)), Period((DateUnit.DAY, Instant((1, 1, 1)), 1))], + [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), Period((DateUnit.DAY, Instant((1, 1, 1)), 365))], + [-1, Period((DateUnit.YEAR, Instant((-1, 1, 1)), 1))], + [0, Period((DateUnit.YEAR, Instant((0, 1, 1)), 1))], + [1, Period((DateUnit.YEAR, Instant((1, 1, 1)), 1))], + [999, Period((DateUnit.YEAR, Instant((999, 1, 1)), 1))], + [1000, Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["1000-1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["1000-1-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["1004-02-29", Period((DateUnit.DAY, Instant((1004, 2, 29)), 1))], + ["year:1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000-01", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000-01-01", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000-01:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000-01-01:1", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["year:1000:3", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 3))], + ["year:1000-01:3", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 3))], + ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], + ["month:1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["month:1000-01-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["month:1000-01:1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["month:1000-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], + ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], + ["month:1000-01-01:3", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 3))], + ["day:1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["day:1000-01-01:3", Period((DateUnit.DAY, Instant((1000, 1, 1)), 3))], + ]) +def test_period_with_a_valid_argument(arg, expected): + assert periods.period(arg) == expected + + +@pytest.mark.parametrize("arg, error", [ + [None, ValueError], + [DateUnit.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", 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], + [(None, None, None, None), ValueError], + [(datetime.date(1, 1, 1),), ValueError], + [(Instant((1, 1, 1)),), ValueError], + [(Period((DateUnit.DAY, Instant((1, 1, 1)), 365)),), ValueError], + [(1,), ValueError], + [(1, 1), ValueError], + [(1, 1, 1), ValueError], + [(-1,), ValueError], + [(-1, -1), ValueError], + [(-1, -1, -1), ValueError], + [("-1",), ValueError], + [("-1", "-1"), ValueError], + [("-1", "-1", "-1"), ValueError], + [("1-1",), ValueError], + [("1-1-1",), ValueError], + ]) +def test_period_with_an_invalid_argument(arg, error): + with pytest.raises(error): + periods.period(arg) + + +@pytest.mark.parametrize("arg, expected", [ + ["1", None], + ["999", None], + ["1000", Period((DateUnit.YEAR, Instant((1000, 1, 1)), 1))], + ["1000-1", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["1000-01", Period((DateUnit.MONTH, Instant((1000, 1, 1)), 1))], + ["1000-1-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["1000-01-1", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["1000-01-01", Period((DateUnit.DAY, Instant((1000, 1, 1)), 1))], + ["1000-01-99", None], + ]) +def test__parse_simple_period_with_a_valid_argument(arg, expected): + assert helpers._parse_simple_period(arg) == expected + + +@pytest.mark.parametrize("arg, expected", [ + [Period((DateUnit.DAY, Instant((1, 1, 1)), 365)), "100_365"], + [Period((DateUnit.MONTH, Instant((1, 1, 1)), 12)), "200_12"], + [Period((DateUnit.YEAR, Instant((1, 1, 1)), 2)), "300_2"], + [Period((DateUnit.ETERNITY, Instant((1, 1, 1)), 1)), "400_1"], + [(DateUnit.DAY, None, 1), "100_1"], + [(DateUnit.MONTH, None, -1000), "200_-1000"], + ]) +def test_key_period_size_with_a_valid_argument(arg, expected): + assert periods.key_period_size(arg) == expected diff --git a/openfisca_core/periods/tests/test_period.py b/openfisca_core/periods/tests/test_period.py new file mode 100644 index 0000000000..dbc846af26 --- /dev/null +++ b/openfisca_core/periods/tests/test_period.py @@ -0,0 +1,80 @@ +import pytest + +from openfisca_core import periods +from openfisca_core.periods import DateUnit, 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"], + ]) +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"], + ]) +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"], + ]) +def test_str_with_days(date_unit, instant, size, expected): + assert str(Period((date_unit, instant, size))) == expected + + +@pytest.mark.parametrize("period, unit, length, first, last", [ + (periods.period('year:2014:2'), DateUnit.YEAR, 2, periods.period('2014'), periods.period('2015')), + (periods.period(2017), DateUnit.MONTH, 12, periods.period('2017-01'), periods.period('2017-12')), + (periods.period('year:2014:2'), DateUnit.MONTH, 24, periods.period('2014-01'), periods.period('2015-12')), + (periods.period('month:2014-03:3'), DateUnit.MONTH, 3, periods.period('2014-03'), periods.period('2014-05')), + (periods.period(2017), DateUnit.DAY, 365, periods.period('2017-01-01'), periods.period('2017-12-31')), + (periods.period('year:2014:2'), DateUnit.DAY, 730, periods.period('2014-01-01'), periods.period('2015-12-31')), + (periods.period('month:2014-03:3'), DateUnit.DAY, 92, periods.period('2014-03-01'), periods.period('2014-05-31')), + ]) +def test_subperiods(period, unit, length, first, last): + subperiods = period.get_subperiods(unit) + assert len(subperiods) == length + assert subperiods[0] == first + assert subperiods[-1] == last + + +@pytest.mark.parametrize("date_unit, instant, size, expected", [ + [DateUnit.MONTH, Instant((2022, 12, 1)), 1, 1], + [DateUnit.MONTH, Instant((2012, 2, 3)), 1, 1], + [DateUnit.MONTH, Instant((2022, 1, 3)), 3, 3], + [DateUnit.MONTH, Instant((2012, 1, 3)), 3, 3], + [DateUnit.YEAR, Instant((2022, 12, 1)), 1, 12], + [DateUnit.YEAR, Instant((2012, 1, 1)), 1, 12], + [DateUnit.YEAR, Instant((2022, 1, 1)), 2, 24], + ]) +def test_day_size_in_months(date_unit, instant, size, expected): + period = Period((date_unit, instant, size)) + assert period.size_in_months == expected + + +@pytest.mark.parametrize("date_unit, instant, size, expected", [ + [DateUnit.DAY, Instant((2022, 12, 31)), 1, 1], + [DateUnit.DAY, Instant((2022, 12, 31)), 3, 3], + [DateUnit.MONTH, Instant((2022, 12, 1)), 1, 31], + [DateUnit.MONTH, Instant((2012, 2, 3)), 1, 29], + [DateUnit.MONTH, Instant((2022, 1, 3)), 3, 31 + 28 + 31], + [DateUnit.MONTH, Instant((2012, 1, 3)), 3, 31 + 29 + 31], + [DateUnit.YEAR, Instant((2022, 12, 1)), 1, 365], + [DateUnit.YEAR, Instant((2012, 1, 1)), 1, 366], + [DateUnit.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 diff --git a/tests/core/test_periods.py b/tests/core/test_periods.py deleted file mode 100644 index b574bb8e0f..0000000000 --- a/tests/core/test_periods.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- - - -import pytest - -from openfisca_core.periods import Period, Instant, YEAR, MONTH, DAY, period - - -@pytest.mark.parametrize("test", [ - (period('year:2014:2'), YEAR, 2, period('2014'), period('2015')), - (period(2017), MONTH, 12, period('2017-01'), period('2017-12')), - (period('year:2014:2'), MONTH, 24, period('2014-01'), period('2015-12')), - (period('month:2014-03:3'), MONTH, 3, period('2014-03'), period('2014-05')), - (period(2017), DAY, 365, period('2017-01-01'), period('2017-12-31')), - (period('year:2014:2'), DAY, 730, period('2014-01-01'), period('2015-12-31')), - (period('month:2014-03:3'), DAY, 92, period('2014-03-01'), period('2014-05-31')), - ]) -def test_subperiods(test): - - def check_subperiods(period, unit, length, first, last): - subperiods = period.get_subperiods(unit) - assert len(subperiods) == length - assert subperiods[0] == first - assert subperiods[-1] == last - - check_subperiods(*test)