-
-
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.
Add tests and fixture for GridNet client error handling
- Loading branch information
1 parent
749ca92
commit 6b99757
Showing
3 changed files
with
81 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Fixture for the Gridnet tests.""" | ||
|
||
from collections.abc import AsyncGenerator | ||
|
||
import pytest | ||
from aiohttp import ClientSession | ||
|
||
from gridnet import GridNet | ||
|
||
|
||
@pytest.fixture(name="gridnet_client") | ||
async def client() -> AsyncGenerator[GridNet, None]: | ||
"""Fixture for the GridNet client.""" | ||
async with ( | ||
ClientSession() as session, | ||
GridNet(host="127.0.0.1", session=session) as gridnet_client, | ||
): | ||
yield gridnet_client |
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,57 @@ | ||
"""Test exceptions for GridNet package.""" | ||
|
||
# pylint: disable=protected-access | ||
import pytest | ||
from aresponses import ResponsesMockServer | ||
|
||
from gridnet import GridNet | ||
from gridnet.exceptions import GridNetError | ||
|
||
|
||
@pytest.mark.parametrize("status", [401, 403, 404]) | ||
async def test_http_error400( | ||
aresponses: ResponsesMockServer, status: int, gridnet_client: GridNet | ||
) -> None: | ||
"""Test HTTP 40X response handling.""" | ||
aresponses.add( | ||
"127.0.0.1", | ||
"/meter/now", | ||
"GET", | ||
aresponses.Response(text="Give me energy!", status=status), | ||
) | ||
with pytest.raises(GridNetError): | ||
assert await gridnet_client._request("test") | ||
|
||
|
||
async def test_http_error500( | ||
aresponses: ResponsesMockServer, gridnet_client: GridNet | ||
) -> None: | ||
"""Test HTTP 500 response handling.""" | ||
aresponses.add( | ||
"127.0.0.1", | ||
"/meter/now", | ||
"GET", | ||
aresponses.Response( | ||
body=b'{"status":"nok"}', | ||
status=500, | ||
), | ||
) | ||
with pytest.raises(GridNetError): | ||
assert await gridnet_client._request("test") | ||
|
||
|
||
async def test_no_success( | ||
aresponses: ResponsesMockServer, gridnet_client: GridNet | ||
) -> None: | ||
"""Test a message without a success message throws.""" | ||
aresponses.add( | ||
"127.0.0.1", | ||
"/meter/now", | ||
"GET", | ||
aresponses.Response( | ||
status=200, | ||
text='{"message": "no success"}', | ||
), | ||
) | ||
with pytest.raises(GridNetError): | ||
assert await gridnet_client._request("test") |
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