diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f9ec6ca --- /dev/null +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py new file mode 100644 index 0000000..36c8501 --- /dev/null +++ b/tests/test_exceptions.py @@ -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") diff --git a/tests/test_gridnet.py b/tests/test_gridnet.py index e311434..556f126 100644 --- a/tests/test_gridnet.py +++ b/tests/test_gridnet.py @@ -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", @@ -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: @@ -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")