1
- import typing
1
+ from typing import Any , Dict , Generator , List
2
2
3
3
import attr
4
4
import pytest
5
5
import responses
6
6
7
7
from labels .github import Label
8
8
9
- ResponseLabel = typing . Dict [str , typing . Any ]
10
- ResponseLabels = typing . List [ResponseLabel ]
9
+ ResponseLabel = Dict [str , Any ]
10
+ ResponseLabels = List [ResponseLabel ]
11
11
12
12
13
13
@pytest .fixture (name = "username" , scope = "session" )
@@ -34,6 +34,12 @@ def fixture_repo_name() -> str:
34
34
return "turtle"
35
35
36
36
37
+ @pytest .fixture (name = "repo_id" , scope = "session" )
38
+ def fixture_repo_id () -> int :
39
+ """Return a repository ID."""
40
+ return 102909380
41
+
42
+
37
43
@attr .s (auto_attribs = True , frozen = True , kw_only = True )
38
44
class FakeProc :
39
45
"""Fake for a CompletedProcess instance."""
@@ -43,7 +49,7 @@ class FakeProc:
43
49
44
50
45
51
@pytest .fixture (name = "mock_repo_info" )
46
- def fixture_mock_repo_info (mocker : typing . Any , remote_url : str ) -> typing . Any :
52
+ def fixture_mock_repo_info (mocker : Any , remote_url : str ) -> Any :
47
53
"""Patch the subprocess call to git remote get-url."""
48
54
49
55
return mocker .patch (
@@ -54,7 +60,7 @@ def fixture_mock_repo_info(mocker: typing.Any, remote_url: str) -> typing.Any:
54
60
55
61
56
62
@pytest .fixture (name = "mock_repo_info_error" )
57
- def fixture_mock_repo_info_error (mocker : typing . Any ) -> typing . Any :
63
+ def fixture_mock_repo_info_error (mocker : Any ) -> Any :
58
64
"""Patch the subprocess call to git remote get-url with an error."""
59
65
60
66
return mocker .patch (
@@ -65,7 +71,7 @@ def fixture_mock_repo_info_error(mocker: typing.Any) -> typing.Any:
65
71
66
72
67
73
@pytest .fixture (name = "mock_repo_info_bad_url" )
68
- def fixture_mock_repo_info_bad_url (mocker : typing . Any ) -> typing . Any :
74
+ def fixture_mock_repo_info_bad_url (mocker : Any ) -> Any :
69
75
"""Patch the subprocess call to git remote get-url with a bad URL."""
70
76
71
77
return mocker .patch (
@@ -148,7 +154,7 @@ def fixture_response_list_labels(
148
154
@pytest .fixture (name = "mock_list_labels" )
149
155
def fixture_mock_list_labels (
150
156
base_url : str , repo_owner : str , repo_name : str , response_list_labels : ResponseLabels
151
- ) -> None :
157
+ ) -> Generator :
152
158
"""Mock requests for list labels."""
153
159
with responses .RequestsMock () as rsps :
154
160
rsps .add (
@@ -161,10 +167,55 @@ def fixture_mock_list_labels(
161
167
yield
162
168
163
169
170
+ @pytest .fixture (name = "mock_list_labels_paginated" )
171
+ def fixture_mock_list_labels_paginated (
172
+ base_url : str ,
173
+ repo_owner : str ,
174
+ repo_name : str ,
175
+ repo_id : int ,
176
+ response_get_infra : ResponseLabel ,
177
+ response_get_docs : ResponseLabel ,
178
+ response_get_bug : ResponseLabel ,
179
+ ) -> Generator :
180
+ """Mock requests for list labels with pagination."""
181
+
182
+ with responses .RequestsMock () as rsps :
183
+
184
+ rsps .add (
185
+ responses .GET ,
186
+ f"{ base_url } /repos/{ repo_owner } /{ repo_name } /labels" ,
187
+ json = [response_get_bug , response_get_docs ],
188
+ status = 200 ,
189
+ content_type = "application/json" ,
190
+ headers = {
191
+ "Link" : (
192
+ f'<{ base_url } /repositories/{ repo_id } /labels?page=2>; rel="next", '
193
+ f'<{ base_url } /repositories/{ repo_id } /labels?page=2>; rel="last"'
194
+ )
195
+ },
196
+ )
197
+
198
+ rsps .add (
199
+ responses .GET ,
200
+ f"{ base_url } /repositories/{ repo_id } /labels?page=2" ,
201
+ json = [response_get_infra ],
202
+ status = 200 ,
203
+ content_type = "application/json" ,
204
+ headers = {
205
+ "Link" : (
206
+ f'<{ base_url } /repositories/{ repo_id } /labels?page=1>; rel="prev", '
207
+ f'<{ base_url } /repositories/{ repo_id } /labels?page=1>; rel="first"'
208
+ )
209
+ },
210
+ )
211
+
212
+ yield
213
+
214
+
164
215
@pytest .fixture (name = "mock_get_label" )
165
216
def fixture_mock_get_label (
166
217
base_url : str , repo_owner : str , repo_name : str , response_get_bug : ResponseLabel
167
- ) -> None :
218
+ ) -> Generator :
168
219
"""Mock requests for get label."""
169
220
with responses .RequestsMock () as rsps :
170
221
rsps .add (
@@ -180,7 +231,7 @@ def fixture_mock_get_label(
180
231
@pytest .fixture (name = "mock_edit_label" )
181
232
def fixture_mock_edit_label (
182
233
base_url : str , repo_owner : str , repo_name : str , response_get_bug : ResponseLabel
183
- ) -> None :
234
+ ) -> Generator :
184
235
"""Mock requests for edit label."""
185
236
with responses .RequestsMock () as rsps :
186
237
rsps .add (
@@ -208,7 +259,7 @@ def fixture_mock_create_label(
208
259
repo_name : str ,
209
260
label : Label ,
210
261
response_get_bug : ResponseLabel ,
211
- ) -> None :
262
+ ) -> Generator :
212
263
"""Mock requests for create label."""
213
264
with responses .RequestsMock () as rsps :
214
265
rsps .add (
@@ -222,7 +273,9 @@ def fixture_mock_create_label(
222
273
223
274
224
275
@pytest .fixture (name = "mock_delete_label" )
225
- def fixture_mock_delete_label (base_url : str , repo_owner : str , repo_name : str ) -> None :
276
+ def fixture_mock_delete_label (
277
+ base_url : str , repo_owner : str , repo_name : str
278
+ ) -> Generator :
226
279
"""Mock requests for delete label."""
227
280
with responses .RequestsMock () as rsps :
228
281
rsps .add (
@@ -236,7 +289,7 @@ def fixture_mock_delete_label(base_url: str, repo_owner: str, repo_name: str) ->
236
289
@pytest .fixture (name = "mock_sync" )
237
290
def fixture_mock_sync (
238
291
base_url : str , repo_owner : str , repo_name : str , response_list_labels : ResponseLabels
239
- ) -> None :
292
+ ) -> Generator :
240
293
with responses .RequestsMock () as rsps :
241
294
# Response mock for when sync requests the existing remote labels
242
295
rsps .add (
@@ -292,7 +345,7 @@ def fixture_mock_sync(
292
345
293
346
294
347
@pytest .fixture (name = "labels" )
295
- def fixture_labels () -> typing . List [Label ]:
348
+ def fixture_labels () -> List [Label ]:
296
349
"""Return a list of Label instances."""
297
350
return [
298
351
Label (
@@ -334,7 +387,7 @@ def fixture_labels() -> typing.List[Label]:
334
387
335
388
336
389
@pytest .fixture (name = "labels_file_dict" )
337
- def fixture_labels_file_content () -> typing . Dict [str , typing . Any ]:
390
+ def fixture_labels_file_content () -> Dict [str , Any ]:
338
391
"""Return a mapping from label names to dicts representing Labels."""
339
392
return {
340
393
"bug" : {
@@ -386,7 +439,7 @@ def fixture_labels_file_content() -> typing.Dict[str, typing.Any]:
386
439
387
440
388
441
@pytest .fixture (name = "labels_file_write" )
389
- def fixture_labels_file_write (tmpdir : typing . Any ) -> str :
442
+ def fixture_labels_file_write (tmpdir : Any ) -> str :
390
443
"""Return a filepath to a temporary file."""
391
444
labels_file = tmpdir .join ("labels.toml" )
392
445
return str (labels_file )
@@ -399,6 +452,6 @@ def fixture_labels_file_load() -> str:
399
452
400
453
401
454
@pytest .fixture (name = "labels_file_sync" )
402
- def fixture_labels_file_sync (tmpdir : typing . Any ) -> str :
455
+ def fixture_labels_file_sync (tmpdir : Any ) -> str :
403
456
"""Return a filepath to an existing labels file for the sync test."""
404
457
return "tests/sync.toml"
0 commit comments