Skip to content

Commit

Permalink
Add tests and fixture for GridNet client error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas committed Nov 10, 2024
1 parent 749ca92 commit 6b99757
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 73 deletions.
18 changes: 18 additions & 0 deletions tests/conftest.py
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
57 changes: 57 additions & 0 deletions tests/test_exceptions.py
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")
79 changes: 6 additions & 73 deletions tests/test_gridnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
from aresponses import Response, ResponsesMockServer

from gridnet import GridNet
from gridnet.exceptions import GridNetConnectionError, GridNetError
from gridnet.exceptions import GridNetConnectionError

from . import load_fixtures


async def test_json_request(aresponses: ResponsesMockServer) -> None:
async def test_json_request(
aresponses: ResponsesMockServer, gridnet_client: GridNet
) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
"127.0.0.1",
Expand All @@ -26,10 +28,8 @@ async def test_json_request(aresponses: ResponsesMockServer) -> None:
text='{"status": "ok"}',
),
)
async with ClientSession() as session:
client = GridNet("127.0.0.1", session=session)
await client._request("test")
await client.close()
await gridnet_client._request("test")
await gridnet_client.close()


async def test_internal_session(aresponses: ResponsesMockServer) -> None:
Expand Down Expand Up @@ -80,70 +80,3 @@ async def test_client_error() -> None:
pytest.raises(GridNetConnectionError),
):
assert await client._request("test")


@pytest.mark.parametrize("status", [401, 403])
async def test_http_error401(aresponses: ResponsesMockServer, status: int) -> None:
"""Test HTTP 401 response handling."""
aresponses.add(
"127.0.0.1",
"/meter/now",
"GET",
aresponses.Response(text="Give me energy!", status=status),
)

async with ClientSession() as session:
client = GridNet(host="127.0.0.1", session=session)
with pytest.raises(GridNetError):
assert await client._request("test")


async def test_http_error400(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 404 response handling."""
aresponses.add(
"127.0.0.1",
"/meter/now",
"GET",
aresponses.Response(text="Give me energy!", status=404),
)

async with ClientSession() as session:
client = GridNet(host="127.0.0.1", session=session)
with pytest.raises(GridNetError):
assert await client._request("test")


async def test_http_error500(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 500 response handling."""
aresponses.add(
"127.0.0.1",
"/meter/now",
"GET",
aresponses.Response(
body=b'{"status":"nok"}',
status=500,
),
)

async with ClientSession() as session:
client = GridNet(host="127.0.0.1", session=session)
with pytest.raises(GridNetError):
assert await client._request("test")


async def test_no_success(aresponses: ResponsesMockServer) -> 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"}',
),
)

async with ClientSession() as session:
client = GridNet(host="127.0.0.1", session=session)
with pytest.raises(GridNetError):
assert await client._request("test")

0 comments on commit 6b99757

Please sign in to comment.