-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_github_rate_limiter.py
77 lines (64 loc) · 2.11 KB
/
test_github_rate_limiter.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
import responses
from time import time
from unittest import TestCase
import src
from src.github_rate_limiter import GithubRateLimiter, GithubLimitsError
from src.enums import HttpStatusCode
from tests.get_mock import get_mock_content
GITHUB_API_URL = "https://api.github.com"
GITHUB_ACCESS_TOKEN = "secret"
github_url = f"{GITHUB_API_URL}/rate_limit"
response_args_mock = {
"method": responses.GET,
"url": github_url,
"headers": {
"Authorization": f"token {GITHUB_ACCESS_TOKEN}"
}
}
src.github_rate_limiter.GithubRateLimiter.GITHUB_API_URL = GITHUB_API_URL
src.github_rate_limiter.GithubRateLimiter.GITHUB_ACCESS_TOKEN = GITHUB_ACCESS_TOKEN
class TestState(TestCase):
@responses.activate
def test_init_limits(self):
for _ in range(2):
responses.add(
**response_args_mock,
status=HttpStatusCode.OK.value,
json=get_mock_content("github_rate_limiter.json")
)
responses.add(
**response_args_mock,
status=HttpStatusCode.BAD_REQUEST.value
)
github_rate_limiter = GithubRateLimiter()
github_rate_limiter.init_limits()
self.assertEqual(
github_rate_limiter.reset_timestamp,
1616274801
)
self.assertEqual(
github_rate_limiter.remaining,
4754
)
with self.assertRaises(GithubLimitsError):
github_rate_limiter.init_limits()
@responses.activate
def test_update_remaining(self):
responses.add(
**response_args_mock,
status=HttpStatusCode.OK.value,
json=get_mock_content("github_rate_limiter.json")
)
github_rate_limiter = GithubRateLimiter()
github_rate_limiter.update_remaining()
self.assertEqual(
github_rate_limiter.remaining,
4754 - 1,
)
github_rate_limiter.remaining = 0
github_rate_limiter.reset_timestamp = time()
github_rate_limiter.update_remaining()
self.assertEqual(
github_rate_limiter.remaining,
4754,
)