-
Notifications
You must be signed in to change notification settings - Fork 60
/
test_time_in_draft.py
183 lines (161 loc) · 6.06 KB
/
test_time_in_draft.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""A test suite for the measure_time_in_draft function."""
import unittest
from datetime import datetime, timedelta
from unittest.mock import MagicMock
import pytz
from time_in_draft import get_stats_time_in_draft, measure_time_in_draft
class TestMeasureTimeInDraft(unittest.TestCase):
"""
Unit tests for the measure_time_in_draft function.
"""
def setUp(self):
"""
Setup common test data and mocks.
"""
self.issue = MagicMock()
self.issue.issue.state = "open"
def test_time_in_draft_with_ready_for_review(self):
"""
Test measure_time_in_draft with one draft and review interval.
"""
self.issue.events.return_value = [
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
),
MagicMock(
event="ready_for_review",
created_at=datetime(2021, 1, 3, tzinfo=pytz.utc),
),
]
result = measure_time_in_draft(self.issue)
expected = timedelta(days=2)
self.assertEqual(result, expected, "The time in draft should be 2 days.")
def test_time_in_draft_without_ready_for_review(self):
"""
Test measure_time_in_draft when ready_for_review_at is not provided and issue is still open.
"""
self.issue.events.return_value = [
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
),
]
now = datetime(2021, 1, 4, tzinfo=pytz.utc)
with unittest.mock.patch("time_in_draft.datetime") as mock_datetime:
mock_datetime.now.return_value = now
result = measure_time_in_draft(self.issue)
expected = timedelta(days=3)
self.assertEqual(result, expected, "The time in draft should be 3 days.")
def test_time_in_draft_multiple_intervals(self):
"""
Test measure_time_in_draft with multiple draft intervals.
"""
self.issue.events.return_value = [
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
),
MagicMock(
event="ready_for_review",
created_at=datetime(2021, 1, 3, tzinfo=pytz.utc),
),
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 5, tzinfo=pytz.utc),
),
MagicMock(
event="ready_for_review",
created_at=datetime(2021, 1, 7, tzinfo=pytz.utc),
),
]
result = measure_time_in_draft(self.issue)
expected = timedelta(days=4)
self.assertEqual(result, expected, "The total time in draft should be 4 days.")
def test_time_in_draft_ongoing_draft(self):
"""
Test measure_time_in_draft with an ongoing draft interval.
"""
self.issue.events.return_value = [
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
),
]
with unittest.mock.patch("time_in_draft.datetime") as mock_datetime:
mock_datetime.now.return_value = datetime(2021, 1, 4, tzinfo=pytz.utc)
result = measure_time_in_draft(self.issue)
expected = timedelta(days=3)
self.assertEqual(
result, expected, "The ongoing draft time should be 3 days."
)
def test_time_in_draft_no_draft_events(self):
"""
Test measure_time_in_draft with no draft-related events.
"""
self.issue.events.return_value = []
result = measure_time_in_draft(self.issue)
self.assertIsNone(
result, "The result should be None when there are no draft events."
)
def test_time_in_draft_without_ready_for_review_and_closed(self):
"""
Test measure_time_in_draft for a closed issue with an ongoing draft and ready_for_review_at is not provided.
"""
self.issue.events.return_value = [
MagicMock(
event="converted_to_draft",
created_at=datetime(2021, 1, 1, tzinfo=pytz.utc),
),
]
self.issue.issue.state = "closed"
result = measure_time_in_draft(self.issue)
self.assertIsNone(
result,
"The result should be None for a closed issue with an ongoing draft.",
)
class TestGetStatsTimeInDraft(unittest.TestCase):
"""
Unit tests for the get_stats_time_in_draft function.
"""
def test_get_stats_time_in_draft_with_data(self):
"""
Test get_stats_time_in_draft with valid draft times.
"""
issues = [
MagicMock(time_in_draft=timedelta(days=1)),
MagicMock(time_in_draft=timedelta(days=2)),
MagicMock(time_in_draft=timedelta(days=3)),
]
result = get_stats_time_in_draft(issues)
expected = {
"avg": timedelta(days=2),
"med": timedelta(days=2),
"90p": timedelta(days=2, seconds=69120),
}
self.assertEqual(
result, expected, "The statistics for time in draft are incorrect."
)
def test_get_stats_time_in_draft_no_data(self):
"""
Test get_stats_time_in_draft with no draft times.
"""
issues = [
MagicMock(time_in_draft=None),
MagicMock(time_in_draft=None),
]
result = get_stats_time_in_draft(issues)
self.assertIsNone(
result, "The result should be None when there are no draft times."
)
def test_get_stats_time_in_draft_empty_list(self):
"""
Test get_stats_time_in_draft with an empty list of issues.
"""
issues = []
result = get_stats_time_in_draft(issues)
self.assertIsNone(
result, "The result should be None when the list of issues is empty."
)
if __name__ == "__main__":
unittest.main()