Repository navigation
Expand file tree
/
Copy pathtest_most_active_mentors.py
More file actions
385 lines (327 loc) · 13.8 KB
/
Copy pathtest_most_active_mentors.py
File metadata and controls
385 lines (327 loc) · 13.8 KB
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
"""A module containing unit tests for the most_active_mentors module.
This module contains unit tests for the count_comments_per_user and
get_mentor_count functions in the most_active_mentors module.
The tests use mock GitHub issues and comments to test the functions' behavior.
Classes:
TestCountCommentsPerUser: A class testing count_comments_per_user.
TestGetMentorCount: A class to test the
get_mentor_count function.
"""
import unittest
from datetime import datetime
from unittest.mock import MagicMock
from classes import IssueWithMetrics
from most_active_mentors import count_comments_per_user, get_mentor_count
class TestCountCommentsPerUser(unittest.TestCase):
"""Test the count_comments_per_user function."""
def test_count_comments_per_user_limit(self):
"""Test that count_comments_per_user correctly counts user comments.
This test mocks the GitHub connection and issue comments, and checks
that count_comments_per_user correctly considers user comments for
counting.
"""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.user.login = "issue_owner"
mock_issue1.created_at = datetime.fromisoformat("2023-01-01T00:00:00Z")
# Set up 21 mock GitHub issue comments - only 20 should be counted
comments_list = []
for i in range(22):
mock_comment1 = MagicMock()
mock_comment1.user.login = "very_active_user"
mock_comment1.created_at = datetime.fromisoformat(
f"2023-01-02T{i:02d}:00:00Z"
)
comments_list.append(mock_comment1)
mock_issue1.get_comments.return_value = comments_list
# Call the function
result = count_comments_per_user(mock_issue1)
expected_result = {"very_active_user": 3}
# Check the results
self.assertEqual(result, expected_result)
def test_count_comments_per_user_with_ignores(self):
"""Test that count_comments_per_user correctly counts user comments with some users ignored."""
# Set up the mock GitHub issues
mock_issue1 = MagicMock()
mock_issue1.comments = 2
mock_issue1.user.login = "issue_owner"
mock_issue1.created_at = datetime.fromisoformat("2023-01-01T00:00:00Z")
# Set up mock GitHub issue comments by several users
comments_list = []
for i in range(5):
mock_comment1 = MagicMock()
mock_comment1.user.login = "very_active_user"
mock_comment1.created_at = datetime.fromisoformat(
f"2023-01-02T{i:02d}:00:00Z"
)
comments_list.append(mock_comment1)
for i in range(5):
mock_comment1 = MagicMock()
mock_comment1.user.login = "very_active_user_ignored"
mock_comment1.created_at = datetime.fromisoformat(
f"2023-01-02T{i:02d}:00:00Z"
)
comments_list.append(mock_comment1)
mock_issue1.get_comments.return_value = comments_list
# Call the function
result = count_comments_per_user(
mock_issue1, ignore_users=["very_active_user_ignored"]
)
# Only the comments by "very_active_user" should be counted,
# so the count should be 3 since that is the threshold for heavily involved
expected_result = {"very_active_user": 3}
# Check the results
self.assertEqual(result, expected_result)
self.assertNotIn("very_active_user_ignored", result)
def test_get_mentor_count(self):
"""Test that get_mentor_count correctly counts comments per user."""
mentor_activity = {"sue": 15, "bob": 10}
# Create mock data
issues_with_metrics = [
IssueWithMetrics(
"Issue 1",
"https://github.com/user/repo/issues/1",
"alice",
None,
mentor_activity=mentor_activity,
),
IssueWithMetrics(
"Issue 2",
"https://github.com/user/repo/issues/2",
"bob",
None,
mentor_activity=mentor_activity,
),
]
# Call the function and check the result
result = get_mentor_count(issues_with_metrics, 2)
expected_result = 2
self.assertEqual(result, expected_result)
class TestGhostUserHandling(unittest.TestCase):
"""Covers ghost (deleted) user handling in ignore_comment."""
def test_ghost_user_comment_is_ignored(self):
"""A comment with user=None (ghost/deleted account) is skipped."""
mock_issue = MagicMock()
mock_issue.user.login = "owner"
ghost_comment = MagicMock()
ghost_comment.user = None
ghost_comment.created_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
real_comment = MagicMock()
real_comment.user.login = "mentor"
real_comment.user.type = "User"
real_comment.created_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
mock_issue.get_comments.return_value = [ghost_comment, real_comment]
result = count_comments_per_user(mock_issue)
self.assertEqual(result, {"mentor": 1})
class TestMostActiveMentorsExtraBranches(unittest.TestCase):
"""Covers most_active_mentors review-comments path."""
def _make_issue(self, owner_login="issue_owner"):
mock_issue = MagicMock()
mock_issue.user.login = owner_login
mock_issue.get_comments.return_value = []
return mock_issue
def test_count_comments_per_user_with_pr_reviews(self):
"""Reviews from a human reviewer are counted; bots are ignored."""
mock_issue = self._make_issue()
# Two reviews from the same human reviewer.
review_a = MagicMock()
review_a.user.login = "reviewer"
review_a.user.type = "User"
review_a.submitted_at = datetime.fromisoformat("2023-01-02T00:00:00Z")
review_b = MagicMock()
review_b.user.login = "reviewer"
review_b.user.type = "User"
review_b.submitted_at = datetime.fromisoformat("2023-01-03T00:00:00Z")
# One review from a bot (must be ignored by ignore_comment).
bot_review = MagicMock()
bot_review.user.login = "dependabot"
bot_review.user.type = "Bot"
bot_review.submitted_at = datetime.fromisoformat("2023-01-04T00:00:00Z")
mock_pr = MagicMock()
mock_pr.get_reviews.return_value = [review_a, review_b, bot_review]
result = count_comments_per_user(mock_issue, pull_request=mock_pr)
self.assertEqual(result, {"reviewer": 2})
def test_count_comments_per_user_review_limit(self):
"""Reviews beyond max_comments_to_eval are not counted."""
mock_issue = self._make_issue()
reviews = []
for i in range(25):
r = MagicMock()
r.user.login = "reviewer"
r.user.type = "User"
r.submitted_at = datetime.fromisoformat(f"2023-01-{i+1:02d}T00:00:00Z")
reviews.append(r)
mock_pr = MagicMock()
mock_pr.get_reviews.return_value = reviews
result = count_comments_per_user(
mock_issue, pull_request=mock_pr, max_comments_to_eval=5
)
self.assertEqual(result, {"reviewer": 5})
class TestCountCommentsDiscussions(unittest.TestCase):
"""Covers the discussion branch of count_comments_per_user.
Before the fix for #774 this branch was dead code because:
1. The GraphQL query fetched no comment author data.
2. Attribute access on dict nodes would raise AttributeError.
3. The ignore_comment call passed comment.user as both issue_user
and comment_user, so every comment was silently skipped.
"""
def _make_discussion(self, author_login, comments):
"""Return a minimal discussion dict as returned by get_discussions."""
return {
"title": "Test discussion",
"url": "https://github.com/org/repo/discussions/1",
"createdAt": "2024-01-01T00:00:00Z",
"author": {"login": author_login, "__typename": "User"},
"comments": {"nodes": comments},
"answerChosenAt": None,
"closedAt": None,
}
def test_counts_commenter(self):
"""A comment by a user other than the author is counted."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "mentor", "__typename": "User"},
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {"mentor": 1})
def test_ignores_discussion_author_self_comment(self):
"""Comments by the discussion author are not counted."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "op", "__typename": "User"},
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {})
def test_ignores_bot_commenter(self):
"""Comments from Bot actors are not counted."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "github-actions", "__typename": "Bot"},
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {})
def test_ignores_user_in_ignore_list(self):
"""Comments by explicitly ignored users are not counted."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "spammer", "__typename": "User"},
},
],
)
result = count_comments_per_user(
None, discussion=discussion, ignore_users=["spammer"]
)
self.assertEqual(result, {})
def test_multiple_commenters(self):
"""Multiple distinct commenters each get their own count."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "alice", "__typename": "User"},
},
{
"createdAt": "2024-01-03T00:00:00Z",
"author": {"login": "bob", "__typename": "User"},
},
{
"createdAt": "2024-01-04T00:00:00Z",
"author": {"login": "alice", "__typename": "User"},
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {"alice": 2, "bob": 1})
def test_no_comments(self):
"""A discussion with an empty comments list returns an empty dict."""
discussion = self._make_discussion("op", [])
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {})
def test_respects_max_comments_to_eval(self):
"""Comments beyond max_comments_to_eval are not evaluated."""
comments = [
{
"createdAt": f"2024-01-{i + 2:02d}T00:00:00Z",
"author": {"login": f"mentor{i}", "__typename": "User"},
}
for i in range(5)
]
discussion = self._make_discussion("op", comments)
result = count_comments_per_user(
None, discussion=discussion, max_comments_to_eval=2
)
self.assertEqual(result, {"mentor0": 1, "mentor1": 1})
def test_caps_count_at_heavily_involved(self):
"""A single commenter is capped at heavily_involved comments."""
comments = [
{
"createdAt": f"2024-01-{i + 2:02d}T00:00:00Z",
"author": {"login": "mentor", "__typename": "User"},
}
for i in range(5)
]
discussion = self._make_discussion("op", comments)
result = count_comments_per_user(
None, discussion=discussion, heavily_involved=3
)
self.assertEqual(result, {"mentor": 3})
def test_null_author_skipped(self):
"""A comment with a null author (deleted account) is skipped gracefully."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": None,
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {})
def test_null_created_at_skipped(self):
"""A comment with a null createdAt (pending) is skipped."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": None,
"author": {"login": "mentor", "__typename": "User"},
},
],
)
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {})
def test_null_discussion_author(self):
"""A discussion whose author is null (deleted OP) is handled."""
discussion = self._make_discussion(
"op",
[
{
"createdAt": "2024-01-02T00:00:00Z",
"author": {"login": "mentor", "__typename": "User"},
},
],
)
discussion["author"] = None
result = count_comments_per_user(None, discussion=discussion)
self.assertEqual(result, {"mentor": 1})