-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add HyScoresAsyncClient; update requirements
- Split HyScoresClient into _HSClientBase (for common things) and HyScoresClient (for non-async implementation). - Add HyScoresAsyncClient - an asynchronous implementation of hscp. - Update required packages to latest versions.
- Loading branch information
Showing
4 changed files
with
247 additions
and
33 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "hscp" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
description = "Client library for HyScores." | ||
authors = ["moonburnt <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -9,12 +9,15 @@ homepage = "https://github.com/moonburnt/hscp" | |
|
||
[tool.poetry.dependencies] | ||
python = "^3.9" | ||
requests = "2.26.0" | ||
requests = "2.27.1" | ||
aiohttp = "3.8.1" | ||
|
||
[tool.poetry.dev-dependencies] | ||
black = "21.10-beta.0" | ||
pytest = "6.2.5" | ||
black = "22.3.0" | ||
pytest = "7.1.2" | ||
requests-mock = "1.9.3" | ||
aioresponses = "0.7.2" | ||
pytest-asyncio = "0.18.3" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|
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,109 @@ | ||
import asyncio | ||
import pytest | ||
import pytest_asyncio | ||
|
||
import hscp | ||
|
||
from aioresponses import aioresponses | ||
|
||
url = "http://example.com" | ||
app = "hyscores" | ||
login = "asda" | ||
pw = "352354300n00" | ||
token = "324234efs42bt9ffon032r0frnd0fn" | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def client() -> hscp.HyScoresAsyncClient: | ||
client = hscp.HyScoresAsyncClient( | ||
url=url, | ||
app=app, | ||
) | ||
yield client | ||
await client.session.close() | ||
|
||
|
||
@pytest.fixture | ||
def authorized_client(client): | ||
client.token = token | ||
return client | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_client(): | ||
client = hscp.HyScoresAsyncClient( | ||
url=url, | ||
app=app, | ||
) | ||
assert client.url == url | ||
assert client.app == app | ||
await client.session.close() | ||
|
||
user_agent = "pytest_client" | ||
client = hscp.HyScoresAsyncClient(url=url, app=app, user_agent=user_agent) | ||
|
||
assert client.session.headers["user-agent"] == user_agent | ||
await client.session.close() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_token_fail(client): | ||
with pytest.raises(hscp.TokenUnavailable): | ||
await client.get_scores() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_register(client): | ||
with aioresponses() as m: | ||
m.post(url + "/register", payload={"result": True}) | ||
resp = await client.register(login, pw) | ||
|
||
assert resp is True | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_login(client): | ||
with aioresponses() as m: | ||
m.post(url + "/login", payload={"result": {"token": token}}) | ||
await client.login(login, pw) | ||
|
||
assert client.token is not None | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_scores(authorized_client): | ||
with aioresponses() as m: | ||
m.get(url + "/scores", payload={"result": []}) | ||
data = await authorized_client.get_scores() | ||
assert isinstance(data, list) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_score(authorized_client): | ||
with aioresponses() as m: | ||
m.get(url + "/score", payload={"result": {"sadam": 36}}) | ||
data = await authorized_client.get_score("sadam") | ||
assert isinstance(data, dict) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_score_fail(authorized_client): | ||
with aioresponses() as m: | ||
m.get(url + "/score", payload={"result": "Invalid Name"}) | ||
with pytest.raises(hscp.InvalidName): | ||
data = await authorized_client.get_score("your mom") | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_score_uploader(authorized_client): | ||
loop = asyncio.get_event_loop_policy().new_event_loop() | ||
|
||
with aioresponses() as m: | ||
m.post(url + "/score", payload={"result": True}) | ||
data = await authorized_client.post_score("sadam", 69) | ||
assert data is True | ||
|
||
|
||
def test_logout(authorized_client): | ||
authorized_client.logout() | ||
assert authorized_client.token is None |
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