forked from github/issue-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_time_to_first_response.py
398 lines (330 loc) · 16 KB
/
test_time_to_first_response.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"""A module containing unit tests for the time_to_first_response module.
This module contains unit tests for the measure_time_to_first_response and
get_stats_time_to_first_response functions in the time_to_first_response module.
The tests use mock GitHub issues and comments to test the functions' behavior.
Classes:
TestMeasureTimeToFirstResponse: A class to test the measure_time_to_first_response function.
TestGetAverageTimeToFirstResponse: A class to test the
get_stats_time_to_first_response function.
"""
import unittest
from datetime import datetime, timedelta
from unittest.mock import MagicMock
from classes import IssueWithMetrics
from time_to_first_response import (
get_stats_time_to_first_response,
measure_time_to_first_response,
)
class TestMeasureTimeToFirstResponse(unittest.TestCase):
"""Test the measure_time_to_first_response function."""
def test_measure_time_to_first_response(self):
"""Test that measure_time_to_first_response calculates the correct time.
This test mocks the GitHub connection and issue comments, and checks that
measure_time_to_first_response calculates the correct time to first response.
"""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments
mock_comment1 = MagicMock()
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_comment2 = MagicMock()
mock_comment2.created_at = datetime.fromisoformat("2023-01-02T12:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1, mock_comment2]
# Call the function
result = measure_time_to_first_response(mock_issue1, None)
expected_result = timedelta(days=1)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_no_comments(self):
"""Test that measure_time_to_first_response returns empty for an issue with no comments."""
# Set up mock issues with no comments
mock_issue1 = MagicMock()
mock_issue1.comments = 0
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Call the function
result = measure_time_to_first_response(mock_issue1, None)
expected_result = None
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_with_pull_request_comments(self):
"""Test that measure_time_to_first_response with pull request comments."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub pull request comments
mock_pr_comment1 = MagicMock()
mock_pr_comment1.submitted_at = datetime.fromisoformat(
"2023-01-02T00:00:00Z"
) # first response
mock_pr_comment2 = MagicMock()
mock_pr_comment2.submitted_at = datetime.fromisoformat("2023-01-02T12:00:00Z")
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None
)
expected_result = timedelta(days=1)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_issue_comment_faster(self):
"""Test that measure_time_to_first_response issue comment faster."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comment
mock_comment1 = MagicMock()
mock_comment1.created_at = datetime.fromisoformat(
"2023-01-02T00:00:00Z"
) # first response
mock_issue1.issue.comments.return_value = [mock_comment1]
# Set up the mock GitHub pull request comment
mock_pr_comment1 = MagicMock()
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None
)
expected_result = timedelta(days=1)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_pull_request_comment_faster(self):
"""Test that measure_time_to_first_response pull request comment faster."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub pull issue comment
mock_comment1 = MagicMock()
mock_comment1.created_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1]
# Set up the mock GitHub pull request comment
mock_pr_comment1 = MagicMock()
mock_pr_comment1.submitted_at = datetime.fromisoformat(
"2023-01-02T00:00:00Z"
) # first response
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None
)
expected_result = timedelta(days=1)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_pull_request_comment_ignore_before_ready(
self,
):
"""Test that measure_time_to_first_response ignores comments from before the pull request was ready for review."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 4
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments (one ignored, one not ignored)
mock_comment1 = MagicMock()
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_comment2 = MagicMock()
mock_comment2.created_at = datetime.fromisoformat("2023-01-05T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1, mock_comment2]
# Set up the mock GitHub pull request comments (one ignored, one not ignored)
mock_pr_comment1 = MagicMock()
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-02T12:00:00Z")
mock_pr_comment2 = MagicMock()
mock_pr_comment2.submitted_at = datetime.fromisoformat(
"2023-01-04T00:00:00Z"
) # first response
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
ready_for_review_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, ready_for_review_at
)
expected_result = timedelta(days=1)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_ignore_users(self):
"""Test that measure_time_to_first_response ignores comments from ignored users."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 4
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments (one ignored, one not ignored)
mock_comment1 = MagicMock()
mock_comment1.user.login = "ignored_user"
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_comment2 = MagicMock()
mock_comment2.user.login = "not_ignored_user"
mock_comment2.created_at = datetime.fromisoformat("2023-01-05T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1, mock_comment2]
# Set up the mock GitHub pull request comments (one ignored, one not ignored)
mock_pr_comment1 = MagicMock()
mock_pr_comment1.user.login = "ignored_user"
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_pr_comment2 = MagicMock()
mock_pr_comment2.user.login = "not_ignored_user"
mock_pr_comment2.submitted_at = datetime.fromisoformat(
"2023-01-04T00:00:00Z"
) # first response
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None, ["ignored_user"]
)
expected_result = timedelta(days=3)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_only_ignored_users(self):
"""Test that measure_time_to_first_response returns empty for an issue with only ignored users."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 4
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments (all ignored)
mock_comment1 = MagicMock()
mock_comment1.user.login = "ignored_user"
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_comment2 = MagicMock()
mock_comment2.user.login = "ignored_user2"
mock_comment2.created_at = datetime.fromisoformat("2023-01-05T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1, mock_comment2]
# Set up the mock GitHub pull request comments (all ignored)
mock_pr_comment1 = MagicMock()
mock_pr_comment1.user.login = "ignored_user"
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_pr_comment2 = MagicMock()
mock_pr_comment2.user.login = "ignored_user2"
mock_pr_comment2.submitted_at = datetime.fromisoformat("2023-01-04T12:00:00Z")
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
# Call the function
result = measure_time_to_first_response(
mock_issue1,
None,
mock_pull_request,
None,
["ignored_user", "ignored_user2"],
)
expected_result = None
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_ignore_issue_owners_comment(self):
"""Test that measure_time_to_first_response ignore issue owner's comment."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 4
mock_issue1.issue.user.login = "issue_owner"
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments
mock_comment1 = MagicMock()
mock_comment1.user.login = "issue_owner"
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_comment2 = MagicMock()
mock_comment2.user.login = "other_user"
mock_comment2.created_at = datetime.fromisoformat("2023-01-05T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1, mock_comment2]
# Set up the mock GitHub pull request comments
mock_pr_comment1 = MagicMock()
mock_pr_comment1.user.login = "issue_owner"
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_pr_comment2 = MagicMock()
mock_pr_comment2.user.login = "other_user"
mock_pr_comment2.submitted_at = datetime.fromisoformat("2023-01-04T00:00:00Z")
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None
)
expected_result = timedelta(days=3)
# Check the results
self.assertEqual(result, expected_result)
def test_measure_time_to_first_response_ignore_bot(self):
"""Test that measure_time_to_first_response ignore bot's comment."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.created_at = "2023-01-01T00:00:00Z"
# Set up the mock GitHub issue comments
mock_comment1 = MagicMock()
mock_comment1.user.type = "Bot"
mock_comment1.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
mock_issue1.issue.comments.return_value = [mock_comment1]
# Set up the mock GitHub pull request comments
mock_pr_comment1 = MagicMock()
mock_pr_comment1.user.type = "Bot"
mock_pr_comment1.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_pr_comment2 = MagicMock()
mock_pr_comment2.user.type = "User"
mock_pr_comment2.submitted_at = datetime.fromisoformat(
"2023-01-04T00:00:00Z"
) # first response
mock_pull_request = MagicMock()
mock_pull_request.reviews.return_value = [mock_pr_comment1, mock_pr_comment2]
# Call the function
result = measure_time_to_first_response(
mock_issue1, None, mock_pull_request, None
)
expected_result = timedelta(days=3)
# Check the results
self.assertEqual(result, expected_result)
class TestGetStatsTimeToFirstResponse(unittest.TestCase):
"""Test the get_stats_time_to_first_response function."""
def test_get_stats_time_to_first_response(self):
"""Test that get_stats_time_to_first_response calculates the correct average.
This test creates a list of mock GitHub issues with time to first response
attributes, calls get_stats_time_to_first_response with the list, and
checks that the function returns the correct average time to first response.
"""
# Create mock data
issues_with_metrics = [
IssueWithMetrics(
"Issue 1",
"https://github.com/user/repo/issues/1",
"alice",
timedelta(days=1),
),
IssueWithMetrics(
"Issue 2",
"https://github.com/user/repo/issues/2",
"bob",
timedelta(days=2),
),
IssueWithMetrics(
"Issue 3", "https://github.com/user/repo/issues/3", "carol", None
),
]
# Call the function and check the result
result = get_stats_time_to_first_response(issues_with_metrics)["avg"]
expected_result = timedelta(days=1.5)
self.assertEqual(result, expected_result)
def test_get_stats_time_to_first_response_with_all_none(self):
"""Test that get_stats_time_to_first_response with all None data."""
# Create mock data with all None
issues_with_metrics = [
IssueWithMetrics(
"Issue 1", "https://github.com/user/repo/issues/1", "alice", None
),
IssueWithMetrics(
"Issue 2", "https://github.com/user/repo/issues/2", "bob", None
),
]
# Call the function and check the result
result = get_stats_time_to_first_response(issues_with_metrics)
expected_result = None
self.assertEqual(result, expected_result)