Skip to content

Commit affbebf

Browse files
committed
Redistribute tests
1 parent 26d25e9 commit affbebf

6 files changed

Lines changed: 68 additions & 149 deletions

File tree

openfisca_core/periods/period_.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ def date(self):
162162
@property
163163
def days(self):
164164
"""Count the number of days in period."""
165-
166165
return (self.stop.date - self.start.date).days + 1
167166

168167
def intersection(self, start, stop):

openfisca_core/periods/tests/helpers/test_instant.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def test_instant_with_a_valid_argument(arg, expected):
5858
["1000-01-01:a", ValueError],
5959
["1000-01-01:1", ValueError],
6060
[(), AssertionError],
61+
[{}, AssertionError],
62+
["", ValueError],
6163
[(None, None, None, None), AssertionError],
6264
])
6365
def test_instant_with_an_invalid_argument(arg, error):

openfisca_core/periods/tests/helpers/test_period.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@
2222
["1000-1-1", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
2323
["1000-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
2424
["1000-01-01", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
25+
["1004-02-29", Period((periods.DAY, Instant((1004, 2, 29)), 1))],
26+
["year:1000", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
27+
["year:1000-01", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
2528
["year:1000-01-01", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
29+
["year:1000:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
30+
["year:1000-01:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
2631
["year:1000-01-01:1", Period((periods.YEAR, Instant((1000, 1, 1)), 1))],
27-
["year:1000-01-01:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
32+
["year:1000:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
33+
["year:1000-01:3", Period((periods.YEAR, Instant((1000, 1, 1)), 3))],
34+
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
35+
["month:1000-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
36+
["month:1000-01-01", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
37+
["month:1000-01:1", Period((periods.MONTH, Instant((1000, 1, 1)), 1))],
38+
["month:1000-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
39+
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
40+
["month:1000-01-01:3", Period((periods.MONTH, Instant((1000, 1, 1)), 3))],
41+
["day:1000-01-01", Period((periods.DAY, Instant((1000, 1, 1)), 1))],
42+
["day:1000-01-01:3", Period((periods.DAY, Instant((1000, 1, 1)), 3))],
2843
])
2944
def test_instant_with_a_valid_argument(arg, expected):
3045
assert periods.period(arg) == expected
@@ -35,16 +50,28 @@ def test_instant_with_a_valid_argument(arg, expected):
3550
[periods.YEAR, ValueError],
3651
[datetime.date(1, 1, 1), ValueError],
3752
["1000-0", ValueError],
53+
["1000-13", ValueError],
3854
["1000-0-0", ValueError],
55+
["1000-1-0", ValueError],
56+
["1000-2-31", ValueError],
3957
["1", ValueError],
4058
["a", ValueError],
4159
["year", ValueError],
4260
["999", ValueError],
43-
["1:1000-01-01", ValueError],
44-
["a:1000-01-01", ValueError],
45-
["1000-01-01:a", ValueError],
61+
["1:1000", ValueError],
62+
["a:1000", ValueError],
63+
["month:1000", ValueError],
64+
["day:1000-01", ValueError],
65+
["1000:a", ValueError],
66+
["1000:1", ValueError],
67+
["1000-01:1", ValueError],
4668
["1000-01-01:1", ValueError],
69+
["month:1000:1", ValueError],
70+
["day:1000:1", ValueError],
71+
["day:1000-01:1", ValueError],
4772
[(), ValueError],
73+
[{}, ValueError],
74+
["", ValueError],
4875
[(None,), ValueError],
4976
[(None, None), ValueError],
5077
[(None, None, None), ValueError],
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from openfisca_core import periods
4+
from openfisca_core.periods import Instant, Period
5+
6+
7+
@pytest.mark.parametrize("date_unit, instant, size, expected", [
8+
[periods.DAY, Instant((2022, 12, 31)), 1, 1],
9+
[periods.DAY, Instant((2022, 12, 31)), 3, 3],
10+
[periods.MONTH, Instant((2022, 12, 1)), 1, 31],
11+
[periods.MONTH, Instant((2012, 2, 3)), 1, 29],
12+
[periods.MONTH, Instant((2022, 1, 3)), 3, 31 + 28 + 31],
13+
[periods.MONTH, Instant((2012, 1, 3)), 3, 31 + 29 + 31],
14+
[periods.YEAR, Instant((2022, 12, 1)), 1, 365],
15+
[periods.YEAR, Instant((2012, 1, 1)), 1, 366],
16+
[periods.YEAR, Instant((2022, 1, 1)), 2, 730],
17+
])
18+
def test_day_size_in_days(date_unit, instant, size, expected):
19+
period = Period((date_unit, instant, size))
20+
assert period.size_in_days == expected
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import pytest
22

3-
from openfisca_core.periods import DateUnit, Instant, Period
3+
from openfisca_core import periods
4+
from openfisca_core.periods import Instant, Period
45

56

67
@pytest.mark.parametrize("date_unit, instant, size, expected", [
7-
[DateUnit.YEAR, Instant((2022, 1, 1)), 1, "2022"],
8-
[DateUnit.MONTH, Instant((2022, 1, 1)), 12, "2022"],
9-
[DateUnit.YEAR, Instant((2022, 3, 1)), 1, "year:2022-03"],
10-
[DateUnit.MONTH, Instant((2022, 3, 1)), 12, "year:2022-03"],
11-
[DateUnit.YEAR, Instant((2022, 1, 1)), 3, "year:2022:3"],
12-
[DateUnit.YEAR, Instant((2022, 1, 3)), 3, "year:2022:3"],
8+
[periods.YEAR, Instant((2022, 1, 1)), 1, "2022"],
9+
[periods.MONTH, Instant((2022, 1, 1)), 12, "2022"],
10+
[periods.YEAR, Instant((2022, 3, 1)), 1, "year:2022-03"],
11+
[periods.MONTH, Instant((2022, 3, 1)), 12, "year:2022-03"],
12+
[periods.YEAR, Instant((2022, 1, 1)), 3, "year:2022:3"],
13+
[periods.YEAR, Instant((2022, 1, 3)), 3, "year:2022:3"],
1314
])
1415
def test_str_with_years(date_unit, instant, size, expected):
1516
assert str(Period((date_unit, instant, size))) == expected
1617

1718

1819
@pytest.mark.parametrize("date_unit, instant, size, expected", [
19-
[DateUnit.MONTH, Instant((2022, 1, 1)), 1, "2022-01"],
20-
[DateUnit.MONTH, Instant((2022, 1, 1)), 3, "month:2022-01:3"],
21-
[DateUnit.MONTH, Instant((2022, 3, 1)), 3, "month:2022-03:3"],
20+
[periods.MONTH, Instant((2022, 1, 1)), 1, "2022-01"],
21+
[periods.MONTH, Instant((2022, 1, 1)), 3, "month:2022-01:3"],
22+
[periods.MONTH, Instant((2022, 3, 1)), 3, "month:2022-03:3"],
2223
])
2324
def test_str_with_months(date_unit, instant, size, expected):
2425
assert str(Period((date_unit, instant, size))) == expected
2526

2627

2728
@pytest.mark.parametrize("date_unit, instant, size, expected", [
28-
[DateUnit.DAY, Instant((2022, 1, 1)), 1, "2022-01-01"],
29-
[DateUnit.DAY, Instant((2022, 1, 1)), 3, "day:2022-01-01:3"],
30-
[DateUnit.DAY, Instant((2022, 3, 1)), 3, "day:2022-03-01:3"],
29+
[periods.DAY, Instant((2022, 1, 1)), 1, "2022-01-01"],
30+
[periods.DAY, Instant((2022, 1, 1)), 3, "day:2022-01-01:3"],
31+
[periods.DAY, Instant((2022, 3, 1)), 3, "day:2022-03-01:3"],
3132
])
3233
def test_str_with_days(date_unit, instant, size, expected):
3334
assert str(Period((date_unit, instant, size))) == expected

tests/core/test_periods.py

Lines changed: 1 addition & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3,137 +3,7 @@
33

44
import pytest
55

6-
from openfisca_core.periods import Period, Instant, YEAR, MONTH, DAY, period
7-
8-
first_jan = Instant((2014, 1, 1))
9-
first_march = Instant((2014, 3, 1))
10-
11-
12-
'''
13-
Test String -> Period
14-
'''
15-
16-
# Years
17-
18-
19-
def test_parsing_year():
20-
assert period('2014') == Period((YEAR, first_jan, 1))
21-
22-
23-
def test_parsing_rolling_year():
24-
assert period('year:2014-03') == Period((YEAR, first_march, 1))
25-
26-
27-
def test_parsing_several_years():
28-
assert period('year:2014:2') == Period((YEAR, first_jan, 2))
29-
30-
31-
def test_wrong_syntax_several_years():
32-
with pytest.raises(ValueError):
33-
period('2014:2')
34-
35-
36-
# Months
37-
38-
def test_parsing_month():
39-
assert period('2014-01') == Period((MONTH, first_jan, 1))
40-
41-
42-
def test_parsing_several_months():
43-
assert period('month:2014-03:3') == Period((MONTH, first_march, 3))
44-
45-
46-
def test_wrong_syntax_several_months():
47-
with pytest.raises(ValueError):
48-
period('2014-3:3')
49-
50-
51-
# Days
52-
53-
def test_parsing_day():
54-
assert period('2014-01-01') == Period((DAY, first_jan, 1))
55-
56-
57-
def test_parsing_several_days():
58-
assert period('day:2014-03-01:3') == Period((DAY, first_march, 3))
59-
60-
61-
def test_wrong_syntax_several_days():
62-
with pytest.raises(ValueError):
63-
period('2014-2-3:2')
64-
65-
66-
def test_day_size_in_days():
67-
assert Period(('day', Instant((2014, 12, 31)), 1)).size_in_days == 1
68-
69-
70-
def test_3_day_size_in_days():
71-
assert Period(('day', Instant((2014, 12, 31)), 3)).size_in_days == 3
72-
73-
74-
def test_month_size_in_days():
75-
assert Period(('month', Instant((2014, 12, 1)), 1)).size_in_days == 31
76-
77-
78-
def test_leap_month_size_in_days():
79-
assert Period(('month', Instant((2012, 2, 3)), 1)).size_in_days == 29
80-
81-
82-
def test_3_month_size_in_days():
83-
assert Period(('month', Instant((2013, 1, 3)), 3)).size_in_days == 31 + 28 + 31
84-
85-
86-
def test_leap_3_month_size_in_days():
87-
assert Period(('month', Instant((2012, 1, 3)), 3)).size_in_days == 31 + 29 + 31
88-
89-
90-
def test_year_size_in_days():
91-
assert Period(('year', Instant((2014, 12, 1)), 1)).size_in_days == 365
92-
93-
94-
def test_leap_year_size_in_days():
95-
assert Period(('year', Instant((2012, 1, 1)), 1)).size_in_days == 366
96-
97-
98-
def test_2_years_size_in_days():
99-
assert Period(('year', Instant((2014, 1, 1)), 2)).size_in_days == 730
100-
101-
# Misc
102-
103-
104-
def test_wrong_date():
105-
with pytest.raises(ValueError):
106-
period("2006-31-03")
107-
108-
109-
def test_ambiguous_period():
110-
with pytest.raises(ValueError):
111-
period('month:2014')
112-
113-
114-
def test_deprecated_signature():
115-
with pytest.raises(TypeError):
116-
period(MONTH, 2014)
117-
118-
119-
def test_wrong_argument():
120-
with pytest.raises(ValueError):
121-
period({})
122-
123-
124-
def test_wrong_argument_1():
125-
with pytest.raises(ValueError):
126-
period([])
127-
128-
129-
def test_none():
130-
with pytest.raises(ValueError):
131-
period(None)
132-
133-
134-
def test_empty_string():
135-
with pytest.raises(ValueError):
136-
period('')
6+
from openfisca_core.periods import YEAR, MONTH, DAY, period
1377

1388

1399
@pytest.mark.parametrize("test", [

0 commit comments

Comments
 (0)