Skip to content

Commit 724e23b

Browse files
committed
Add unit test for parse_period
1 parent 1840cf8 commit 724e23b

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

tests/test_utils.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import datetime
2+
13
import pytest
24

3-
from good_first_issues.utils import get_row_ids, identify_limit
5+
from good_first_issues.utils import get_row_ids, identify_limit, parse_period
46

57

68
@pytest.mark.parametrize(
@@ -23,3 +25,95 @@ def test_get_row_ids(issues, limiter, expected):
2325
Check correctness of iterable returned.
2426
"""
2527
assert get_row_ids(issues, limiter) == expected
28+
29+
30+
# ------- TEST parse_period -------
31+
32+
33+
# Fixture to mock the current time for consistent testing
34+
@pytest.fixture
35+
def mock_datetime_now(monkeypatch):
36+
# Define a fixed datetime for testing
37+
fixed_datetime = datetime.datetime(
38+
2023, 10, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
39+
)
40+
41+
# Mock the `datetime.datetime.now` method
42+
class MockDateTime(datetime.datetime):
43+
@classmethod
44+
def now(cls, tz=None):
45+
return fixed_datetime
46+
47+
# Replace the real `datetime.datetime` with our mock
48+
monkeypatch.setattr(datetime, "datetime", MockDateTime)
49+
50+
51+
@pytest.mark.parametrize(
52+
"duration, expected_period, expected_timedelta",
53+
[
54+
("10m", 10, datetime.timedelta(minutes=10)),
55+
("5min", 5, datetime.timedelta(minutes=5)),
56+
("15mins", 15, datetime.timedelta(minutes=15)),
57+
("30minutes", 30, datetime.timedelta(minutes=30)),
58+
("2h", 120, datetime.timedelta(hours=2)),
59+
("3hr", 180, datetime.timedelta(hours=3)),
60+
("4hrs", 240, datetime.timedelta(hours=4)),
61+
("5hours", 300, datetime.timedelta(hours=5)),
62+
("1d", 1440, datetime.timedelta(days=1)),
63+
("2day", 2880, datetime.timedelta(days=2)),
64+
("3days", 4320, datetime.timedelta(days=3)),
65+
],
66+
)
67+
def test_parse_period_valid(
68+
mock_datetime_now, duration, expected_period, expected_timedelta
69+
):
70+
result = parse_period(duration)
71+
assert result.absolute_period == expected_period
72+
assert (
73+
result.utc_date_time
74+
== datetime.datetime(2023, 10, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
75+
- expected_timedelta
76+
)
77+
78+
79+
@pytest.mark.parametrize(
80+
"duration",
81+
[
82+
"10", # Missing duration unit
83+
"10x", # Invalid duration unit
84+
"minutes", # Missing period
85+
"-10m", # Negative period
86+
"10.5m", # Non-integer period
87+
],
88+
)
89+
def test_parse_period_invalid(duration):
90+
with pytest.raises(SystemExit):
91+
parse_period(duration)
92+
93+
94+
def test_parse_period_zero_duration(mock_datetime_now):
95+
result = parse_period("0m")
96+
assert result.absolute_period == 0
97+
assert result.utc_date_time == datetime.datetime(
98+
2023, 10, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
99+
)
100+
101+
102+
def test_parse_period_large_duration(mock_datetime_now):
103+
result = parse_period("1000d")
104+
assert result.absolute_period == 1000 * 60 * 24
105+
assert result.utc_date_time == datetime.datetime(
106+
2023, 10, 1, 12, 0, 0, tzinfo=datetime.timezone.utc
107+
) - datetime.timedelta(days=1000)
108+
109+
110+
def test_parse_period_error_message(capsys):
111+
with pytest.raises(SystemExit):
112+
parse_period("10x")
113+
captured = capsys.readouterr()
114+
assert "❌ Invalid status duration" in captured.err
115+
116+
with pytest.raises(SystemExit):
117+
parse_period("10")
118+
captured = capsys.readouterr()
119+
assert "Invalid duration" in captured.err

0 commit comments

Comments
 (0)