-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix multiple calls to task_done (#19)
Co-authored-by: ctmbl <[email protected]>
- Loading branch information
Showing
2 changed files
with
61 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import pytest | ||
|
||
from api.rate_limiter import DEFAULT_MAX_RETRY, RateLimiter | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_default_max_retry(monkeypatch): | ||
monkeypatch.delenv("MAX_API_RETRY", raising=False) | ||
rate_limiter = RateLimiter() | ||
|
||
# pylint: disable-next=protected-access | ||
assert rate_limiter._max_retry == DEFAULT_MAX_RETRY | ||
|
||
rate_limiter.task.cancel() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_env_max_retry(monkeypatch): | ||
monkeypatch.setenv("MAX_API_RETRY", "42") | ||
rate_limiter = RateLimiter() | ||
|
||
# pylint: disable-next=protected-access | ||
assert rate_limiter._max_retry == 42 | ||
|
||
rate_limiter.task.cancel() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_request_passes(monkeypatch, mocker): | ||
mocked_get = mocker.MagicMock() | ||
resp = mocker.MagicMock() | ||
|
||
data = {"some data": "when request returns"} | ||
resp.status_code = 200 | ||
resp.json.return_value = data | ||
mocked_get.return_value = resp | ||
|
||
monkeypatch.setattr("requests.get", mocked_get) | ||
|
||
# Trigger test | ||
rate_limiter = RateLimiter() | ||
|
||
url = "url" | ||
cookies = {"cookie": "dummy"} | ||
result = await rate_limiter.make_request(url, cookies, "GET") | ||
|
||
mocked_get.assert_called_once_with(url, cookies=cookies) | ||
assert result is data | ||
|
||
# Clean task properly | ||
rate_limiter.task.cancel() |