-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sentinel.py
94 lines (80 loc) · 2.47 KB
/
test_sentinel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import io
from datetime import datetime
from unittest.mock import patch
import pytest
from freezegun import freeze_time
from dateutil import parser
from sentinel_datetime import sentinel
@pytest.mark.parametrize(
"datestr, expected",
[
("2020", "sentinel(2020, 0, 0, 0, 0)"),
("Sep-2nd 11:00:00", "sentinel(0, 9, 2, 11, 0)"),
],
)
@patch("sys.stdout", new_callable=io.StringIO)
def test_stdout_output(mock_stdout, datestr, expected):
default = sentinel()
result = parser.parse(datestr, default=default)
print(result)
assert mock_stdout.getvalue().strip() == expected
@pytest.mark.parametrize(
"datestr, expected",
[
("Sep-2020", False),
("1-Sep-2020", True),
("2020-11", False),
("11-1", True),
("1993", False),
],
)
def test_has_day(datestr, expected):
default = sentinel()
result = parser.parse(datestr, default=default)
assert result.has_day is expected
@pytest.mark.parametrize(
"datestr, expected",
[
("Sep-2020", True),
("1-Sep-2020", True),
("2020-11", True),
("11-1", False),
("1993", True),
],
)
def test_has_year(datestr, expected):
default = sentinel()
result = parser.parse(datestr, default=default)
assert result.has_year is expected
@freeze_time("Jan 18th, 2008")
@pytest.mark.parametrize(
"datestr, expected_today, expected_default",
[
("Sep-2020", datetime(2020, 9, 18), datetime(2020, 9, 2)),
("2020", datetime(2020, 1, 18), datetime(2020, 2, 2)),
],
)
def test_default_datetime(datestr, expected_today, expected_default):
default = sentinel()
result = parser.parse(datestr, default=default)
assert result.todatetime() == expected_today
default = sentinel(default=datetime(1978, 2, 2))
result = parser.parse(datestr, default=default)
assert result.todatetime() == expected_default
@pytest.mark.parametrize(
"datestr, expected",
[
("Sep-2020", datetime(2020, 9, 18)),
("1-Sep-2020", datetime(2020, 9, 1)),
("1-Sep", datetime(2021, 9, 1)),
("Sep-3rd", datetime(2021, 9, 3)),
("11-1", datetime(2021, 11, 1)),
("1993", datetime(1993, 9, 18)),
("2020-08", datetime(2020, 8, 18)),
("2020", datetime(2020, 9, 18)),
],
)
def test_to_datetime(datestr, expected):
default = sentinel(default=datetime(2021, 9, 18))
result = parser.parse(datestr, default=default)
assert result.todatetime() == expected