-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_contributor_stats.py
219 lines (196 loc) · 6.23 KB
/
test_contributor_stats.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
"""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",
False,
"https://avatars.githubusercontent.com/u/29484535?v=4",
1261,
"commit_url5",
"",
)
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_merge_contributors(self):
"""
Test the merge_contributors function.
"""
contributor1 = ContributorStats(
"user1",
False,
"https://avatars.githubusercontent.com/u/29484535?v=4",
100,
"commit_url1",
"",
)
contributor2 = ContributorStats(
"user2",
False,
"https://avatars.githubusercontent.com/u/29484535?v=4",
200,
"commit_url2",
"",
)
contributor3 = ContributorStats(
"user1",
False,
"https://avatars.githubusercontent.com/u/29484535?v=4",
150,
"commit_url3",
"",
)
all_contributors = [
[
contributor1,
contributor2,
contributor3,
]
]
expected_result = [
ContributorStats(
"user1",
False,
"https://avatars.githubusercontent.com/u/29484535?v=4",
250,
"commit_url1, commit_url3",
"",
),
ContributorStats(
"user2",
False,
"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",
new_contributor=False,
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count="100",
commit_url="url1",
sponsor_info="",
),
ContributorStats(
username="user2",
new_contributor=False,
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",
new_contributor=False,
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count="100",
commit_url="url1",
sponsor_info="",
),
ContributorStats(
username="user2",
new_contributor=False,
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,
new_contributor=False,
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,
)
if __name__ == "__main__":
unittest.main()