-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtest_pr_comments.py
More file actions
138 lines (109 loc) · 5.23 KB
/
test_pr_comments.py
File metadata and controls
138 lines (109 loc) · 5.23 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
"""Tests for the pr_comments module.
This module contains tests for the count_pr_comments and get_stats_pr_comments
functions.
"""
import unittest
from unittest.mock import MagicMock
from classes import IssueWithMetrics
from pr_comments import count_pr_comments, get_stats_pr_comments
class TestCountPRComments(unittest.TestCase):
"""Test the count_pr_comments function."""
def test_count_pr_comments_with_comments(self):
"""Test counting PR comments with actual comments."""
# Mock issue with comments
mock_issue = MagicMock()
mock_comment1 = MagicMock()
mock_comment1.user.type = "User"
mock_comment1.user.login = "user1"
mock_comment2 = MagicMock()
mock_comment2.user.type = "User"
mock_comment2.user.login = "user2"
mock_issue.issue.comments.return_value = [mock_comment1, mock_comment2]
# Mock pull request with review comments
mock_pull_request = MagicMock()
mock_review_comment1 = MagicMock()
mock_review_comment1.user.type = "User"
mock_review_comment1.user.login = "user3"
mock_pull_request.review_comments.return_value = [mock_review_comment1]
result = count_pr_comments(mock_issue, mock_pull_request, [])
self.assertEqual(result, 3)
def test_count_pr_comments_with_bots_ignored(self):
"""Test that bot comments are ignored."""
# Mock issue with bot comment
mock_issue = MagicMock()
mock_bot_comment = MagicMock()
mock_bot_comment.user.type = "Bot"
mock_bot_comment.user.login = "github-actions[bot]"
mock_user_comment = MagicMock()
mock_user_comment.user.type = "User"
mock_user_comment.user.login = "user1"
mock_issue.issue.comments.return_value = [mock_bot_comment, mock_user_comment]
mock_pull_request = MagicMock()
mock_pull_request.review_comments.return_value = []
result = count_pr_comments(mock_issue, mock_pull_request, [])
self.assertEqual(result, 1)
def test_count_pr_comments_with_ignored_users(self):
"""Test that ignored users are not counted."""
# Mock issue with comments from ignored user
mock_issue = MagicMock()
mock_comment1 = MagicMock()
mock_comment1.user.type = "User"
mock_comment1.user.login = "ignored_user"
mock_comment2 = MagicMock()
mock_comment2.user.type = "User"
mock_comment2.user.login = "regular_user"
mock_issue.issue.comments.return_value = [mock_comment1, mock_comment2]
mock_pull_request = MagicMock()
mock_pull_request.review_comments.return_value = []
result = count_pr_comments(mock_issue, mock_pull_request, ["ignored_user"])
self.assertEqual(result, 1)
def test_count_pr_comments_no_pull_request(self):
"""Test that None is returned when no pull request is provided."""
mock_issue = MagicMock()
result = count_pr_comments(mock_issue, None, [])
self.assertIsNone(result)
def test_count_pr_comments_no_issue(self):
"""Test that None is returned when no issue is provided."""
mock_pull_request = MagicMock()
result = count_pr_comments(None, mock_pull_request, [])
self.assertIsNone(result)
def test_count_pr_comments_exception_handling(self):
"""Test that exceptions are handled gracefully."""
# Mock issue that raises exception
mock_issue = MagicMock()
mock_issue.issue.comments.side_effect = AttributeError("No comments")
mock_pull_request = MagicMock()
mock_pull_request.review_comments.side_effect = AttributeError(
"No review comments"
)
result = count_pr_comments(mock_issue, mock_pull_request, [])
self.assertEqual(result, 0)
class TestGetStatsPRComments(unittest.TestCase):
"""Test the get_stats_pr_comments function."""
def test_get_stats_pr_comments_with_data(self):
"""Test calculating PR comment statistics with data."""
issues_with_metrics = [
IssueWithMetrics("PR 1", "url1", "user1", pr_comment_count=5),
IssueWithMetrics("PR 2", "url2", "user2", pr_comment_count=10),
IssueWithMetrics("PR 3", "url3", "user3", pr_comment_count=3),
IssueWithMetrics("Issue 1", "url4", "user4"), # No comment count (not a PR)
]
result = get_stats_pr_comments(issues_with_metrics)
self.assertIsNotNone(result)
self.assertEqual(result["avg"], 6.0) # (5+10+3)/3
self.assertEqual(result["med"], 5.0)
self.assertEqual(result["90p"], 9.0) # 90th percentile
def test_get_stats_pr_comments_no_data(self):
"""Test calculating PR comment statistics with no PR data."""
issues_with_metrics = [
IssueWithMetrics("Issue 1", "url1", "user1"), # No comment count
IssueWithMetrics("Issue 2", "url2", "user2"), # No comment count
]
result = get_stats_pr_comments(issues_with_metrics)
self.assertIsNone(result)
def test_get_stats_pr_comments_empty_list(self):
"""Test calculating PR comment statistics with empty list."""
result = get_stats_pr_comments([])
self.assertIsNone(result)
if __name__ == "__main__":
unittest.main()