forked from TheAlgorithms/algorithms-keeper
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_check_runs.py
228 lines (223 loc) · 7.78 KB
/
test_check_runs.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
220
221
222
223
224
225
226
227
228
from urllib.parse import quote
import pytest
from gidgethub.sansio import Event
from algorithms_keeper.constants import Label
from algorithms_keeper.event.check_run import check_run_router
from .utils import (
ExpectedData,
MockGitHubAPI,
check_run_url,
labels_url,
parametrize_id,
repository,
search_url,
sha,
)
# Reminder: ``Event.delivery_id`` is used as a short description for the respective
# test case and as a way to id the specific test case in the parametrized group.
@pytest.mark.asyncio()
@pytest.mark.parametrize(
"event, gh, expected",
(
# Check run completed from a commit which does not belong to a pull request.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="commit_not_from_pr",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 0,
"items": [],
}
}
),
ExpectedData(getitem_url=[search_url]),
),
# Check run completed but some of the other checks are in progress.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="completed_some_in_progress",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 1,
"items": [{"labels": [{"name": Label.REVIEW}]}],
},
check_run_url: {
"total_count": 2,
"check_runs": [
{"status": "completed", "conclusion": "cancelled"},
{"status": "in_progress", "conclusion": None},
],
},
}
),
ExpectedData(getitem_url=[search_url, check_run_url]),
),
# Check run completed and it's a success, there are no ``FAILED_TEST`` label on
# the pull request, so no action taken.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="completed_passing_no_label",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 1,
"items": [{"labels": [{"name": Label.REVIEW}]}],
},
check_run_url: {
"total_count": 2,
"check_runs": [
{"status": "completed", "conclusion": "success"},
{"status": "completed", "conclusion": "skipped"},
],
},
}
),
ExpectedData(getitem_url=[search_url, check_run_url]),
),
# Check run completed and it's a success, there is the ``FAILED_TEST`` label on
# the pull request, so remove it.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="completed_passing_with_label",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 1,
"items": [
{
"labels": [{"name": Label.FAILED_TEST}],
"labels_url": labels_url,
}
],
},
check_run_url: {
"total_count": 2,
"check_runs": [
{"status": "completed", "conclusion": "action_required"},
{"status": "completed", "conclusion": "success"},
],
},
}
),
ExpectedData(
getitem_url=[search_url, check_run_url],
delete_url=[f"{labels_url}/{quote(Label.FAILED_TEST)}"],
),
),
# Check run completed and it's a failure, there is no ``FAILED_TEST`` label on
# the pull request, so add it.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="completed_failing_no_label",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 1,
"items": [
{
"labels": [{"name": Label.REVIEW}],
"labels_url": labels_url,
}
],
},
check_run_url: {
"total_count": 2,
"check_runs": [
{"status": "completed", "conclusion": "success"},
{"status": "completed", "conclusion": "failure"},
],
},
}
),
ExpectedData(
getitem_url=[search_url, check_run_url],
post_url=[labels_url],
post_data=[{"labels": [Label.FAILED_TEST]}],
),
),
# Check run completed and it's a failure, there is ``FAILED_TEST`` label on
# the pull request, so don't do anything.
(
Event(
data={
"action": "completed",
"check_run": {
"head_sha": sha,
},
"repository": {"full_name": repository},
},
event="check_run",
delivery_id="completed_failing_with_label",
),
MockGitHubAPI(
getitem={
search_url: {
"total_count": 1,
"items": [{"labels": [{"name": Label.FAILED_TEST}]}],
},
check_run_url: {
"total_count": 2,
"check_runs": [
{"status": "completed", "conclusion": "timed_out"},
{"status": "completed", "conclusion": "success"},
],
},
}
),
ExpectedData(getitem_url=[search_url, check_run_url]),
),
),
ids=parametrize_id,
)
async def test_check_run(
event: Event, gh: MockGitHubAPI, expected: ExpectedData
) -> None:
await check_run_router.dispatch(event, gh)
assert gh == expected