-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_comment.py
More file actions
121 lines (92 loc) · 3.88 KB
/
Copy pathtest_comment.py
File metadata and controls
121 lines (92 loc) · 3.88 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
"""Unit tests for the PR commenter using a fake HTTP transport (no network)."""
from __future__ import annotations
import json
import pytest
from profile_diff import MARKER
from profile_diff.comment import (
CommentError,
GitHubClient,
find_existing_comment,
post_or_update,
)
class FakeTransport:
"""Records requests and returns canned responses.
``existing`` is the list of comments the GET call reports.
"""
def __init__(self, existing):
self.existing = existing
self.calls = [] # list of (method, url, payload-dict-or-None)
def __call__(self, method, url, headers, body):
payload = json.loads(body.decode("utf-8")) if body else None
self.calls.append((method, url, payload))
if method == "GET":
return 200, json.dumps(self.existing)
if method == "POST":
return 201, json.dumps({"id": 999, "body": payload["body"]})
if method == "PATCH":
return 200, json.dumps({"id": 42, "body": payload["body"]})
raise AssertionError(f"unexpected method {method}") # pragma: no cover
def make_client(existing):
transport = FakeTransport(existing)
client = GitHubClient(repo="octo/repo", token="t0ken", transport=transport)
return client, transport
def test_creates_comment_when_none_exists():
client, transport = make_client(existing=[])
result = post_or_update(client, 7, "Hello body")
methods = [c[0] for c in transport.calls]
assert methods == ["GET", "POST"]
assert result["_action"] == "created"
# Marker was auto-prepended.
post_payload = transport.calls[-1][2]
assert post_payload["body"].startswith(MARKER)
assert "/issues/7/comments" in transport.calls[-1][1]
def test_updates_comment_when_marker_present():
existing = [
{"id": 1, "body": "unrelated chatter"},
{"id": 42, "body": f"{MARKER}\nold diff"},
]
client, transport = make_client(existing)
result = post_or_update(client, 7, f"{MARKER}\nnew diff")
methods = [c[0] for c in transport.calls]
assert methods == ["GET", "PATCH"]
assert result["_action"] == "updated"
# PATCH targets the existing comment id, not a new POST.
assert "/issues/comments/42" in transport.calls[-1][1]
assert transport.calls[-1][2]["body"] == f"{MARKER}\nnew diff"
def test_marker_not_duplicated_when_already_present():
client, transport = make_client(existing=[])
body = f"{MARKER}\nalready marked"
post_or_update(client, 1, body)
assert transport.calls[-1][2]["body"] == body # unchanged, single marker
def test_find_existing_comment_helper():
comments = [
{"id": 1, "body": "nope"},
{"id": 2, "body": None},
{"id": 3, "body": f"prefix {MARKER} suffix"},
]
assert find_existing_comment(comments) == 3
assert find_existing_comment([{"id": 9, "body": "nothing"}]) is None
def test_pagination_is_followed():
# Two full pages then a short page.
page1 = [{"id": i, "body": "x"} for i in range(100)]
page2 = [{"id": 100 + i, "body": "y"} for i in range(100)]
page3 = [{"id": 250, "body": f"{MARKER} here"}]
class Paging:
def __init__(self):
self.pages = {1: page1, 2: page2, 3: page3}
self.calls = []
def __call__(self, method, url, headers, body):
self.calls.append((method, url))
page = int(url.split("&page=")[1])
return 200, json.dumps(self.pages.get(page, []))
transport = Paging()
client = GitHubClient(repo="o/r", token="t", transport=transport)
comments = client.list_comments(5)
assert len(comments) == 201
assert find_existing_comment(comments) == 250
def test_http_error_raises():
def failing(method, url, headers, body):
return 500, "boom"
client = GitHubClient(repo="o/r", token="t", transport=failing)
with pytest.raises(CommentError):
client.list_comments(1)