From affbebffb0443b71a56f240602cb72a5c8ff5f2f Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Sat, 30 Jul 2022 19:35:31 +0200 Subject: [PATCH] Redistribute tests --- openfisca_core/periods/period_.py | 1 - .../periods/tests/helpers/test_instant.py | 2 + .../periods/tests/helpers/test_period.py | 35 ++++- .../periods/tests/period/test_size_in_days.py | 20 +++ .../periods/tests/period/test_str.py | 27 ++-- tests/core/test_periods.py | 132 +----------------- 6 files changed, 68 insertions(+), 149 deletions(-) create mode 100644 openfisca_core/periods/tests/period/test_size_in_days.py diff --git a/openfisca_core/periods/period_.py b/openfisca_core/periods/period_.py index 7411d9f72c..d343e3df05 100644 --- a/openfisca_core/periods/period_.py +++ b/openfisca_core/periods/period_.py @@ -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): diff --git a/openfisca_core/periods/tests/helpers/test_instant.py b/openfisca_core/periods/tests/helpers/test_instant.py index dd6002c73f..c58c5897f2 100644 --- a/openfisca_core/periods/tests/helpers/test_instant.py +++ b/openfisca_core/periods/tests/helpers/test_instant.py @@ -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): diff --git a/openfisca_core/periods/tests/helpers/test_period.py b/openfisca_core/periods/tests/helpers/test_period.py index 4df00a9a18..50cc59eae8 100644 --- a/openfisca_core/periods/tests/helpers/test_period.py +++ b/openfisca_core/periods/tests/helpers/test_period.py @@ -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 @@ -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], diff --git a/openfisca_core/periods/tests/period/test_size_in_days.py b/openfisca_core/periods/tests/period/test_size_in_days.py new file mode 100644 index 0000000000..c68d5d82b0 --- /dev/null +++ b/openfisca_core/periods/tests/period/test_size_in_days.py @@ -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 diff --git a/openfisca_core/periods/tests/period/test_str.py b/openfisca_core/periods/tests/period/test_str.py index 874a8405ab..4e0ba81446 100644 --- a/openfisca_core/periods/tests/period/test_str.py +++ b/openfisca_core/periods/tests/period/test_str.py @@ -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 diff --git a/tests/core/test_periods.py b/tests/core/test_periods.py index 58fffd7919..0816a0ce2e 100644 --- a/tests/core/test_periods.py +++ b/tests/core/test_periods.py @@ -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", [