FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-github-backup/tests/test_retrieve_data.py at master · dumpmemory/python-github-backup · GitHub
dumpmemory
/
python-github-backup
Public
forked from
josegonzalez/python-github-backup
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-github-backup
/
tests
/
test_retrieve_data.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
529 lines (433 loc) · 19.3 KB
Breadcrumbs
python-github-backup
/
tests
/
test_retrieve_data.py
Copy path
File metadata and controls
529 lines (433 loc) · 19.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
"""Tests for retrieve_data function."""
import
json
import
logging
import
socket
from
unittest
.
mock
import
Mock
,
patch
from
urllib
.
error
import
HTTPError
,
URLError
import
pytest
from
github_backup
import
github_backup
from
github_backup
.
github_backup
import
(
calculate_retry_delay
,
make_request_with_retry
,
)
# Default retry count used in tests (matches argparse default)
# With max_retries=5, total attempts = 6 (1 initial + 5 retries)
DEFAULT_MAX_RETRIES
=
5
class
TestCalculateRetryDelay
:
def
test_respects_retry_after_header
(
self
):
headers
=
{
"retry-after"
:
"30"
}
assert
calculate_retry_delay
(
0
,
headers
)
==
30
def
test_respects_rate_limit_reset
(
self
):
import
time
import
calendar
# Set reset time 60 seconds in the future
future_reset
=
calendar
.
timegm
(
time
.
gmtime
())
+
60
headers
=
{
"x-ratelimit-remaining"
:
"0"
,
"x-ratelimit-reset"
:
str
(
future_reset
)}
delay
=
calculate_retry_delay
(
0
,
headers
)
# Should be approximately 60 seconds (with some tolerance for execution time)
assert
55
<=
delay
<=
65
def
test_exponential_backoff
(
self
):
delay_0
=
calculate_retry_delay
(
0
, {})
delay_1
=
calculate_retry_delay
(
1
, {})
delay_2
=
calculate_retry_delay
(
2
, {})
# Base delay is 1s, so delays should be roughly 1, 2, 4 (plus jitter)
assert
0.9
<=
delay_0
<=
1.2
# ~1s + up to 10% jitter
assert
1.8
<=
delay_1
<=
2.4
# ~2s + up to 10% jitter
assert
3.6
<=
delay_2
<=
4.8
# ~4s + up to 10% jitter
def
test_max_delay_cap
(
self
):
# Very high attempt number should not exceed 120s + jitter
delay
=
calculate_retry_delay
(
100
, {})
assert
delay
<=
120
*
1.1
# 120s max + 10% jitter
def
test_minimum_rate_limit_delay
(
self
):
import
time
import
calendar
# Set reset time in the past (already reset)
past_reset
=
calendar
.
timegm
(
time
.
gmtime
())
-
100
headers
=
{
"x-ratelimit-remaining"
:
"0"
,
"x-ratelimit-reset"
:
str
(
past_reset
)}
delay
=
calculate_retry_delay
(
0
,
headers
)
# Should be minimum 10 seconds even if reset time is in past
assert
delay
>=
10
class
TestRetrieveDataRetry
:
"""Tests for retry behavior in retrieve_data."""
def
test_json_parse_error_retries_and_fails
(
self
,
create_args
):
"""HTTP 200 with invalid JSON should retry and eventually fail."""
args
=
create_args
(
token_classic
=
"fake_token"
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
200
mock_response
.
read
.
return_value
=
b"not valid json {"
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
}
call_count
=
0
def
mock_make_request
(
*
a
,
**
kw
):
nonlocal
call_count
call_count
+=
1
return
mock_response
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
side_effect
=
mock_make_request
,
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
# No delay in tests
with
pytest
.
raises
(
Exception
)
as
exc_info
:
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/repos/test/repo/issues"
)
assert
"Failed to read response after"
in
str
(
exc_info
.
value
)
assert
(
call_count
==
DEFAULT_MAX_RETRIES
+
1
)
# 1 initial + 5 retries = 6 attempts
def
test_json_parse_error_recovers_on_retry
(
self
,
create_args
):
"""HTTP 200 with invalid JSON should succeed if retry returns valid JSON."""
args
=
create_args
(
token_classic
=
"fake_token"
)
bad_response
=
Mock
()
bad_response
.
getcode
.
return_value
=
200
bad_response
.
read
.
return_value
=
b"not valid json {"
bad_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
}
good_response
=
Mock
()
good_response
.
getcode
.
return_value
=
200
good_response
.
read
.
return_value
=
json
.
dumps
([{
"id"
:
1
}]).
encode
(
"utf-8"
)
good_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
,
"Link"
:
""
}
responses
=
[
bad_response
,
bad_response
,
good_response
]
call_count
=
0
def
mock_make_request
(
*
a
,
**
kw
):
nonlocal
call_count
result
=
responses
[
call_count
]
call_count
+=
1
return
result
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
side_effect
=
mock_make_request
,
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
result
=
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/repos/test/repo/issues"
)
assert
result
==
[{
"id"
:
1
}]
assert
call_count
==
3
# Failed twice, succeeded on third
def
test_http_error_raises_exception
(
self
,
create_args
):
"""Non-success HTTP status codes should raise Exception."""
args
=
create_args
(
token_classic
=
"fake_token"
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
404
mock_response
.
read
.
return_value
=
b'{"message": "Not Found"}'
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
}
mock_response
.
reason
=
"Not Found"
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
return_value
=
mock_response
,
):
with
pytest
.
raises
(
Exception
)
as
exc_info
:
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/repos/test/notfound/issues"
)
assert
not
isinstance
(
exc_info
.
value
,
github_backup
.
RepositoryUnavailableError
)
assert
"404"
in
str
(
exc_info
.
value
)
class
TestMakeRequestWithRetry
:
"""Tests for HTTP error retry behavior in make_request_with_retry."""
def
test_502_error_retries_and_succeeds
(
self
):
"""HTTP 502 should retry and succeed if subsequent request works."""
good_response
=
Mock
()
good_response
.
read
.
return_value
=
b'{"ok": true}'
call_count
=
0
fail_count
=
DEFAULT_MAX_RETRIES
# Fail all retries, succeed on last attempt
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
if
call_count
<=
fail_count
:
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
502
,
msg
=
"Bad Gateway"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
return
good_response
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
result
=
make_request_with_retry
(
Mock
(),
None
)
assert
result
==
good_response
assert
(
call_count
==
DEFAULT_MAX_RETRIES
+
1
)
# 1 initial + 5 retries = 6 attempts
def
test_503_error_retries_until_exhausted
(
self
):
"""HTTP 503 should make 1 initial + DEFAULT_MAX_RETRIES retry attempts then raise."""
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
503
,
msg
=
"Service Unavailable"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
with
pytest
.
raises
(
HTTPError
)
as
exc_info
:
make_request_with_retry
(
Mock
(),
None
)
assert
exc_info
.
value
.
code
==
503
assert
(
call_count
==
DEFAULT_MAX_RETRIES
+
1
)
# 1 initial + 5 retries = 6 attempts
def
test_404_error_not_retried
(
self
):
"""HTTP 404 should not be retried - raise immediately."""
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
404
,
msg
=
"Not Found"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
pytest
.
raises
(
HTTPError
)
as
exc_info
:
make_request_with_retry
(
Mock
(),
None
)
assert
exc_info
.
value
.
code
==
404
assert
call_count
==
1
# No retries
def
test_rate_limit_403_retried_when_remaining_zero
(
self
):
"""HTTP 403 with x-ratelimit-remaining=0 should retry."""
good_response
=
Mock
()
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
if
call_count
==
1
:
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
403
,
msg
=
"Forbidden"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"0"
},
fp
=
None
,
)
return
good_response
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
result
=
make_request_with_retry
(
Mock
(),
None
)
assert
result
==
good_response
assert
call_count
==
2
def
test_403_not_retried_when_remaining_nonzero
(
self
):
"""HTTP 403 with x-ratelimit-remaining>0 should not retry (permission error)."""
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
403
,
msg
=
"Forbidden"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
pytest
.
raises
(
HTTPError
)
as
exc_info
:
make_request_with_retry
(
Mock
(),
None
)
assert
exc_info
.
value
.
code
==
403
assert
call_count
==
1
# No retries
def
test_451_error_not_retried
(
self
):
"""HTTP 451 should not be retried - raise immediately."""
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
451
,
msg
=
"Unavailable For Legal Reasons"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
pytest
.
raises
(
HTTPError
)
as
exc_info
:
make_request_with_retry
(
Mock
(),
None
)
assert
exc_info
.
value
.
code
==
451
assert
call_count
==
1
# No retries
def
test_connection_error_retries_and_succeeds
(
self
):
"""URLError (connection error) should retry and succeed if subsequent request works."""
good_response
=
Mock
()
call_count
=
0
fail_count
=
DEFAULT_MAX_RETRIES
# Fail all retries, succeed on last attempt
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
if
call_count
<=
fail_count
:
raise
URLError
(
"Connection refused"
)
return
good_response
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
result
=
make_request_with_retry
(
Mock
(),
None
)
assert
result
==
good_response
assert
(
call_count
==
DEFAULT_MAX_RETRIES
+
1
)
# 1 initial + 5 retries = 6 attempts
def
test_socket_error_retries_until_exhausted
(
self
):
"""socket.error should make 1 initial + DEFAULT_MAX_RETRIES retry attempts then raise."""
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
raise
socket
.
error
(
"Connection reset by peer"
)
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
with
pytest
.
raises
(
socket
.
error
):
make_request_with_retry
(
Mock
(),
None
)
assert
(
call_count
==
DEFAULT_MAX_RETRIES
+
1
)
# 1 initial + 5 retries = 6 attempts
class
TestRetrieveGraphqlDataLogging
:
"""Tests for GraphQL request logging."""
def
test_logs_graphql_context
(
self
,
create_args
,
caplog
):
args
=
create_args
(
token_classic
=
"fake_token"
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
200
mock_response
.
read
.
return_value
=
json
.
dumps
({
"data"
: {}}).
encode
(
"utf-8"
)
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
}
caplog
.
set_level
(
logging
.
INFO
,
logger
=
"github_backup.github_backup"
)
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
return_value
=
mock_response
,
):
github_backup
.
retrieve_graphql_data
(
args
,
"query { viewer { login } }"
,
log_context
=
"discussion owner/repo#1"
,
)
assert
(
"Requesting https://api.github.com/graphql (discussion owner/repo#1)"
in
caplog
.
text
)
class
TestRetrieveDataThrottling
:
"""Tests for throttling behavior in retrieve_data."""
def
test_throttling_pauses_when_rate_limit_low
(
self
,
create_args
):
"""Should pause when x-ratelimit-remaining is at or below throttle_limit."""
args
=
create_args
(
token_classic
=
"fake_token"
,
throttle_limit
=
10
,
throttle_pause
=
5
,
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
200
mock_response
.
read
.
return_value
=
json
.
dumps
([{
"id"
:
1
}]).
encode
(
"utf-8"
)
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5"
,
"Link"
:
""
,
}
# Below throttle_limit
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
return_value
=
mock_response
,
):
with
patch
(
"github_backup.github_backup.time.sleep"
)
as
mock_sleep
:
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/repos/test/repo/issues"
)
mock_sleep
.
assert_called_once_with
(
5
)
# throttle_pause value
class
TestRetrieveDataSingleItem
:
"""Tests for single item (dict) responses in retrieve_data."""
def
test_dict_response_returned_as_list
(
self
,
create_args
):
"""Single dict response should be returned as a list with one item."""
args
=
create_args
(
token_classic
=
"fake_token"
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
200
mock_response
.
read
.
return_value
=
json
.
dumps
(
{
"login"
:
"testuser"
,
"id"
:
123
}
).
encode
(
"utf-8"
)
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
,
"Link"
:
""
}
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
return_value
=
mock_response
,
):
result
=
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/user"
)
assert
result
==
[{
"login"
:
"testuser"
,
"id"
:
123
}]
class
TestRetriesCliArgument
:
"""Tests for --retries CLI argument validation and behavior."""
def
test_retries_argument_accepted
(
self
):
"""--retries flag should be accepted and parsed correctly."""
args
=
github_backup
.
parse_args
([
"--retries"
,
"3"
,
"testuser"
])
assert
args
.
max_retries
==
3
def
test_retries_default_value
(
self
):
"""--retries should default to 5 if not specified."""
args
=
github_backup
.
parse_args
([
"testuser"
])
assert
args
.
max_retries
==
5
def
test_retries_zero_is_valid
(
self
):
"""--retries 0 should be valid and mean 1 attempt (no retries)."""
args
=
github_backup
.
parse_args
([
"--retries"
,
"0"
,
"testuser"
])
assert
args
.
max_retries
==
0
def
test_retries_negative_rejected
(
self
):
"""--retries with negative value should be rejected by argparse."""
with
pytest
.
raises
(
SystemExit
):
github_backup
.
parse_args
([
"--retries"
,
"-1"
,
"testuser"
])
def
test_retries_non_integer_rejected
(
self
):
"""--retries with non-integer value should be rejected by argparse."""
with
pytest
.
raises
(
SystemExit
):
github_backup
.
parse_args
([
"--retries"
,
"abc"
,
"testuser"
])
def
test_retries_one_with_transient_error_succeeds
(
self
):
"""--retries 1 should allow one retry after initial failure."""
good_response
=
Mock
()
good_response
.
read
.
return_value
=
b'{"ok": true}'
call_count
=
0
def
mock_urlopen
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
if
call_count
==
1
:
raise
HTTPError
(
url
=
"https://api.github.com/test"
,
code
=
502
,
msg
=
"Bad Gateway"
,
hdrs
=
{
"x-ratelimit-remaining"
:
"5000"
},
fp
=
None
,
)
return
good_response
with
patch
(
"github_backup.github_backup.urlopen"
,
side_effect
=
mock_urlopen
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
result
=
make_request_with_retry
(
Mock
(),
None
,
max_retries
=
1
)
assert
result
==
good_response
assert
call_count
==
2
# 1 initial + 1 retry = 2 attempts
def
test_custom_retry_count_limits_attempts
(
self
,
create_args
):
"""Custom --retries value should limit actual retry attempts."""
args
=
create_args
(
token_classic
=
"fake_token"
,
max_retries
=
2
,
# 2 retries = 3 total attempts (1 initial + 2 retries)
)
mock_response
=
Mock
()
mock_response
.
getcode
.
return_value
=
200
mock_response
.
read
.
return_value
=
b"not valid json {"
mock_response
.
headers
=
{
"x-ratelimit-remaining"
:
"5000"
}
call_count
=
0
def
mock_make_request
(
*
args
,
**
kwargs
):
nonlocal
call_count
call_count
+=
1
return
mock_response
with
patch
(
"github_backup.github_backup.make_request_with_retry"
,
side_effect
=
mock_make_request
,
):
with
patch
(
"github_backup.github_backup.calculate_retry_delay"
,
return_value
=
0
):
with
pytest
.
raises
(
Exception
)
as
exc_info
:
github_backup
.
retrieve_data
(
args
,
"https://api.github.com/repos/test/repo/issues"
)
assert
"Failed to read response after 3 attempts"
in
str
(
exc_info
.
value
)
assert
call_count
==
3
# 1 initial + 2 retries = 3 attempts
Back
|
FazBrowse Home
|
New Git URL