Skip to content

Commit 72b320e

Browse files
committed
Test GraphQL pagination in pull comments retrieval
1 parent 1d69f8b commit 72b320e

1 file changed

Lines changed: 50 additions & 11 deletions

File tree

‎tests/test_temporary_private_forks.py‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ def _repo(name, updated_at, pushed_at=None):
2626
}
2727

2828

29+
def _pull_comments_page(comment_nodes, total_count, has_next=False):
30+
return {
31+
"repository": {
32+
"pullRequest": {
33+
"comments": {
34+
"totalCount": total_count,
35+
"nodes": comment_nodes,
36+
"pageInfo": {
37+
"hasNextPage": has_next,
38+
"endCursor": "COMMENTS2" if has_next else None,
39+
},
40+
},
41+
}
42+
}
43+
}
44+
45+
2946
# This is an end-to-end test of a full repository backup that's supposed to ensure that
3047
# everything in the process can handle most of the REST API endpoints returning 404s
3148
# without raising errors.
@@ -46,17 +63,7 @@ def test_temporary_private_fork_backup_handles_limited_api_endpoints_with_no_err
4663

4764
def fake_retrieve_graphql_data(args, query, variables=None, log_context=None):
4865
if query == github_backup.PULL_COMMENTS_QUERY:
49-
return {
50-
"repository": {
51-
"pullRequest": {
52-
"comments": {
53-
"totalCount": 1,
54-
"nodes": regular_comments,
55-
"pageInfo": {"hasNextPage": False, "endCursor": None},
56-
},
57-
},
58-
},
59-
}
66+
return _pull_comments_page(regular_comments, len(regular_comments))
6067
else:
6168
raise AssertionError(f"request made with unknown query: {query}")
6269

@@ -95,3 +102,35 @@ def gen():
95102
assert pr_data["comment_regular_data"] == regular_comments
96103
assert pr_data["comment_data"] == review_comments
97104
assert pr_data["commit_data"] == commits
105+
106+
107+
def test_get_pull_comments_with_graphql_paginates(create_args):
108+
args = create_args()
109+
repository = {"full_name": "owner/repo"}
110+
111+
comment_1 = {"id": "comment-1", "body": "first comment"}
112+
comment_2 = {"id": "comment-2", "body": "second comment"}
113+
comment_3 = {"id": "comment-3", "body": "third comment"}
114+
total_count = 3
115+
116+
responses = [
117+
_pull_comments_page([comment_1, comment_2], total_count, has_next=True),
118+
_pull_comments_page([comment_3], total_count, has_next=False),
119+
]
120+
121+
with patch(
122+
"github_backup.github_backup.retrieve_graphql_data", side_effect=responses
123+
) as mock_retrieve:
124+
comment_data = github_backup.get_pull_comments_with_graphql(args, repository, 42)
125+
126+
assert len(comment_data) == total_count
127+
assert comment_data[0]["body"] == "first comment"
128+
assert comment_data[1]["body"] == "second comment"
129+
assert comment_data[2]["body"] == "third comment"
130+
assert mock_retrieve.call_count == 2
131+
assert [
132+
call.kwargs["log_context"] for call in mock_retrieve.call_args_list
133+
] == [
134+
"pull request owner/repo#42 comments page 1",
135+
"pull request owner/repo#42 comments page 2",
136+
]

0 commit comments

Comments
 (0)