Repository navigation
Expand file tree
/
Copy pathtest_discussions.py
More file actions
344 lines (295 loc) · 13.1 KB
/
Copy pathtest_discussions.py
File metadata and controls
344 lines (295 loc) · 13.1 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
"""A module containing unit tests for the get_discussions function in the discussions module.
Classes:
TestGetDiscussions: A class to test the get_discussions function with mock GraphQL responses.
"""
import unittest
from unittest.mock import MagicMock
from github import GithubException
from discussions import get_discussions
class TestGetDiscussions(unittest.TestCase):
"""A class to test the get_discussions function in the discussions module."""
def _create_mock_response(
self, discussions, has_next_page=False, end_cursor="cursor123"
):
"""Helper method to create a mock GraphQL response body."""
return {
"data": {
"search": {
"edges": [{"node": discussion} for discussion in discussions],
"pageInfo": {"hasNextPage": has_next_page, "endCursor": end_cursor},
}
}
}
def test_get_discussions_single_page(self):
"""Test the get_discussions function with a single page of results."""
# Mock data for two discussions
mock_discussions = [
{
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"comments": {"nodes": [{"createdAt": "2021-01-01T00:01:00Z"}]},
"answerChosenAt": None,
"closedAt": None,
},
{
"title": "Discussion 2",
"url": "https://github.com/user/repo/discussions/2",
"createdAt": "2021-01-02T00:00:00Z",
"comments": {"nodes": [{"createdAt": "2021-01-02T00:01:00Z"}]},
"answerChosenAt": "2021-01-03T00:00:00Z",
"closedAt": "2021-01-04T00:00:00Z",
},
]
github_connection = MagicMock()
# graphql_query returns (headers, response_json)
github_connection.requester.graphql_query.return_value = (
{},
self._create_mock_response(mock_discussions, has_next_page=False),
)
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query"
)
# Check that the function returns the expected discussions
self.assertEqual(len(discussions), 2)
self.assertEqual(discussions[0]["title"], "Discussion 1")
self.assertEqual(discussions[1]["title"], "Discussion 2")
# Verify only one API call was made
self.assertEqual(github_connection.requester.graphql_query.call_count, 1)
def test_get_discussions_multiple_pages(self):
"""Test the get_discussions function with multiple pages of results."""
# Mock data for pagination
page1_discussions = [
{
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"comments": {"nodes": [{"createdAt": "2021-01-01T00:01:00Z"}]},
"answerChosenAt": None,
"closedAt": None,
}
]
page2_discussions = [
{
"title": "Discussion 2",
"url": "https://github.com/user/repo/discussions/2",
"createdAt": "2021-01-02T00:00:00Z",
"comments": {"nodes": [{"createdAt": "2021-01-02T00:01:00Z"}]},
"answerChosenAt": None,
"closedAt": None,
}
]
github_connection = MagicMock()
# Return a different page for each call
github_connection.requester.graphql_query.side_effect = [
(
{},
self._create_mock_response(
page1_discussions, has_next_page=True, end_cursor="cursor123"
),
),
({}, self._create_mock_response(page2_discussions, has_next_page=False)),
]
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query"
)
# Check that all discussions were returned
self.assertEqual(len(discussions), 2)
self.assertEqual(discussions[0]["title"], "Discussion 1")
self.assertEqual(discussions[1]["title"], "Discussion 2")
# Verify that two API calls were made
self.assertEqual(github_connection.requester.graphql_query.call_count, 2)
# Verify the second call paginated using the first page's end cursor
second_call_variables = (
github_connection.requester.graphql_query.call_args_list[1].args[1]
)
self.assertEqual(second_call_variables["cursor"], "cursor123")
def test_get_discussions_propagates_github_exception(self):
"""A GithubException raised by graphql_query propagates to the caller."""
github_connection = MagicMock()
github_connection.requester.graphql_query.side_effect = GithubException(
500, {"message": "server error"}, None
)
with self.assertRaises(GithubException):
get_discussions(github_connection, "repo:user/repo type:discussions query")
def test_get_discussions_paginates_comments(self):
"""Comments beyond the first page are fetched when max_comments allows."""
def comment(index):
return {
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": f"user{index}", "__typename": "User"},
}
first_page = [comment(index) for index in range(100)]
second_page = [comment(index) for index in range(100, 150)]
discussion = {
"id": "D_kwDO",
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "author", "__typename": "User"},
"comments": {
"nodes": first_page,
"pageInfo": {"hasNextPage": True, "endCursor": "comment100"},
},
"answerChosenAt": None,
"closedAt": None,
}
github_connection = MagicMock()
github_connection.requester.graphql_query.side_effect = [
({}, self._create_mock_response([discussion], has_next_page=False)),
(
{},
{
"data": {
"node": {
"comments": {
"nodes": second_page,
"pageInfo": {
"hasNextPage": False,
"endCursor": "comment150",
},
}
}
}
},
),
]
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query", max_comments=150
)
# All 150 comments are collected, not just the first page of 100
self.assertEqual(len(discussions[0]["comments"]["nodes"]), 150)
# A follow-up query was made for the remaining comments
self.assertEqual(github_connection.requester.graphql_query.call_count, 2)
comment_call_variables = (
github_connection.requester.graphql_query.call_args_list[1].args[1]
)
self.assertEqual(comment_call_variables["id"], "D_kwDO")
self.assertEqual(comment_call_variables["cursor"], "comment100")
self.assertEqual(comment_call_variables["pageSize"], 50)
def test_get_discussions_stops_paginating_comments_at_max_comments(self):
"""No extra request is made once max_comments comments are collected."""
discussion = {
"id": "D_kwDO",
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "author", "__typename": "User"},
"comments": {
"nodes": [
{
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "user1", "__typename": "User"},
}
],
"pageInfo": {"hasNextPage": True, "endCursor": "comment1"},
},
"answerChosenAt": None,
"closedAt": None,
}
github_connection = MagicMock()
github_connection.requester.graphql_query.return_value = (
{},
self._create_mock_response([discussion], has_next_page=False),
)
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query", max_comments=1
)
self.assertEqual(len(discussions[0]["comments"]["nodes"]), 1)
self.assertEqual(github_connection.requester.graphql_query.call_count, 1)
def test_get_discussions_with_no_comments(self):
"""A discussion without a comments connection triggers no extra request."""
discussion = {
"id": "D_kwDO",
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "author", "__typename": "User"},
"comments": None,
"answerChosenAt": None,
"closedAt": None,
}
github_connection = MagicMock()
github_connection.requester.graphql_query.return_value = (
{},
self._create_mock_response([discussion], has_next_page=False),
)
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query", max_comments=150
)
self.assertIsNone(discussions[0]["comments"])
self.assertEqual(github_connection.requester.graphql_query.call_count, 1)
def test_get_discussions_stops_when_followup_page_has_no_nodes(self):
"""An empty follow-up page bounds the loop instead of spinning forever."""
discussion = {
"id": "D_kwDO",
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "author", "__typename": "User"},
"comments": {
"nodes": [{"createdAt": "2021-01-01T00:01:00Z"}],
"pageInfo": {"hasNextPage": True, "endCursor": "comment1"},
},
"answerChosenAt": None,
"closedAt": None,
}
github_connection = MagicMock()
github_connection.requester.graphql_query.side_effect = [
({}, self._create_mock_response([discussion], has_next_page=False)),
(
{},
{
"data": {
"node": {
"comments": {
"nodes": None,
"pageInfo": {
"hasNextPage": True,
"endCursor": "comment1",
},
}
}
}
},
),
]
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query", max_comments=150
)
# The single first-page comment is kept; the empty page broke the loop
self.assertEqual(len(discussions[0]["comments"]["nodes"]), 1)
self.assertEqual(github_connection.requester.graphql_query.call_count, 2)
def test_get_discussions_tolerates_deleted_discussion_during_pagination(self):
"""A discussion deleted mid-run degrades to its first page of comments."""
def comment(index):
return {
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": f"user{index}", "__typename": "User"},
}
first_page = [comment(index) for index in range(100)]
discussion = {
"id": "D_kwDO",
"title": "Discussion 1",
"url": "https://github.com/user/repo/discussions/1",
"createdAt": "2021-01-01T00:00:00Z",
"author": {"login": "author", "__typename": "User"},
"comments": {
"nodes": first_page,
"pageInfo": {"hasNextPage": True, "endCursor": "comment100"},
},
"answerChosenAt": None,
"closedAt": None,
}
github_connection = MagicMock()
github_connection.requester.graphql_query.side_effect = [
({}, self._create_mock_response([discussion], has_next_page=False)),
({}, {"data": {"node": None}}),
]
discussions = get_discussions(
github_connection, "repo:user/repo type:discussions query", max_comments=150
)
# The run is not aborted; the discussion keeps its first 100 comments
self.assertEqual(len(discussions), 1)
self.assertEqual(len(discussions[0]["comments"]["nodes"]), 100)
self.assertEqual(github_connection.requester.graphql_query.call_count, 2)