-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_contributor_stats.py
More file actions
255 lines (227 loc) · 7.58 KB
/
test_contributor_stats.py
File metadata and controls
255 lines (227 loc) · 7.58 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
"""This module contains the tests for the ContributorStats class."""
import unittest
from unittest.mock import MagicMock, patch
from contributor_stats import (
ContributorStats,
get_sponsor_information,
is_new_contributor,
merge_contributors,
)
class TestContributorStats(unittest.TestCase):
"""
Test case for the ContributorStats class.
"""
def setUp(self):
"""
Set up a ContributorStats instance for use in tests.
"""
self.contributor = ContributorStats(
"zkoppert",
"https://avatars.githubusercontent.com/u/29484535?v=4",
1261,
"commit_url5",
"",
)
def test_init_new_contributor_true(self):
"""
Test that new_contributor=True is preserved by __init__.
"""
contributor = ContributorStats(
"new_user",
avatar_url="https://example.com/avatar.png",
contribution_count=1,
commit_url="commit_url_new",
sponsor_info="",
new_contributor=True,
)
self.assertTrue(contributor.new_contributor)
def test_init(self):
"""
Test the __init__ method of the ContributorStats class.
"""
self.assertEqual(self.contributor.username, "zkoppert")
self.assertFalse(self.contributor.new_contributor)
self.assertEqual(
self.contributor.avatar_url,
"https://avatars.githubusercontent.com/u/29484535?v=4",
)
self.assertEqual(self.contributor.contribution_count, 1261)
self.assertEqual(
self.contributor.commit_url,
"commit_url5",
)
def test_repr(self):
"""Test the __repr__ method includes key fields."""
expected = (
"contributor_stats(username=zkoppert, "
"avatar_url=https://avatars.githubusercontent.com/u/29484535?v=4, "
"contribution_count=1261, "
"commit_url=commit_url5, "
"sponsor_info=, "
"new_contributor=False)"
)
self.assertEqual(repr(self.contributor), expected)
def test_merge_contributors(self):
"""
Test the merge_contributors function.
"""
contributor1 = ContributorStats(
"user1",
"https://avatars.githubusercontent.com/u/29484535?v=4",
100,
"commit_url1",
"",
)
contributor2 = ContributorStats(
"user2",
"https://avatars.githubusercontent.com/u/29484535?v=4",
200,
"commit_url2",
"",
)
contributor3 = ContributorStats(
"user1",
"https://avatars.githubusercontent.com/u/29484535?v=4",
150,
"commit_url3",
"",
)
all_contributors = [
[
contributor1,
contributor2,
contributor3,
]
]
expected_result = [
ContributorStats(
"user1",
"https://avatars.githubusercontent.com/u/29484535?v=4",
250,
"commit_url1, commit_url3",
"",
),
ContributorStats(
"user2",
"https://avatars.githubusercontent.com/u/29484535?v=4",
200,
"commit_url2",
"",
),
]
result = merge_contributors(all_contributors)
self.assertEqual(expected_result, result)
def test_is_new_contributor_true(self):
"""
Test the is_new_contributor function when the contributor is new.
"""
username = "new_user"
returning_contributors = [
ContributorStats(
username="user1",
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=100,
commit_url="url1",
sponsor_info="",
),
ContributorStats(
username="user2",
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=200,
commit_url="url2",
sponsor_info="",
),
]
result = is_new_contributor(username, returning_contributors)
self.assertTrue(result)
def test_is_new_contributor_false(self):
"""
Test the is_new_contributor function when the contributor is not new.
"""
username = "user1"
returning_contributors = [
ContributorStats(
username="user1",
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=100,
commit_url="url1",
sponsor_info="",
),
ContributorStats(
username="user2",
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=200,
commit_url="url2",
sponsor_info="",
),
]
result = is_new_contributor(username, returning_contributors)
self.assertFalse(result)
@patch("requests.post")
def test_fetch_sponsor_info(self, mock_post):
"""
Test the get_sponsor_information function.
"""
# Mock response data
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"data": {"repositoryOwner": {"hasSponsorsListing": True}}
}
mock_post.return_value = mock_response
# Mock contributors
user = "user1"
returning_contributors = [
ContributorStats(
username=user,
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=100,
commit_url="url1",
sponsor_info="",
),
]
# Test parameters
ghe = ""
token = "token"
# Call the function
result = get_sponsor_information(returning_contributors, token, ghe)
# Assertions
self.assertEqual(result[0].sponsor_info, "https://github.com/sponsors/user1")
# Ensure the post request was called with the correct parameters
mock_post.assert_called_once_with(
"https://api.github.com/graphql",
json={
"query": """
query($username: String!){
repositoryOwner(login: $username) {
... on User {
hasSponsorsListing
}
}
}
""",
"variables": {"username": "user1"},
},
headers={"Authorization": "Bearer token"},
timeout=60,
)
@patch("requests.post")
def test_fetch_sponsor_info_raises_on_error(self, mock_post):
"""Test get_sponsor_information raises when the API response is invalid."""
mock_response = MagicMock()
mock_response.status_code = 500
mock_response.json.return_value = {"errors": [{"message": "fail"}]}
mock_post.return_value = mock_response
contributors = [
ContributorStats(
username="user1",
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count=100,
commit_url="url1",
sponsor_info="",
),
]
with self.assertRaises(ValueError):
get_sponsor_information(contributors, token="token", ghe="")
if __name__ == "__main__":
unittest.main()