Skip to content

Commit

Permalink
Fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Feb 28, 2021
1 parent 7a8a22c commit 0317ad3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
5 changes: 5 additions & 0 deletions custom_components/integration_blueprint/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,23 @@ async def api_wrapper(self, method: str, url: str, data=None, headers=None) -> d
url,
exception,
)
raise exception

except (KeyError, TypeError) as exception:
_LOGGER.error(
"Error parsing information from %s - %s",
url,
exception,
)
raise exception

except (aiohttp.ClientError, socket.gaierror) as exception:
_LOGGER.error(
"Error fetching information from %s - %s",
url,
exception,
)
raise exception

except Exception as exception: # pylint: disable=broad-except
_LOGGER.error("Something really wrong happened! - %s", exception)
14 changes: 6 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import pytest

# pylint: disable=invalid-name
pytest_plugins = "pytest_homeassistant_custom_component"
from custom_components.integration_blueprint import IntegrationBlueprintApiClient

pytest_plugins = "pytest_homeassistant_custom_component" # pylint: disable=invalid-name


# This fixture is used to prevent HomeAssistant from attempting to create and dismiss persistent
Expand All @@ -39,9 +40,7 @@ def skip_notifications_fixture():
@pytest.fixture(name="bypass_get_data")
def bypass_get_data_fixture():
"""Skip calls to get data from API."""
with patch(
"custom_components.integration_blueprint.IntegrationBlueprintApiClient.async_get_data"
):
with patch.object(IntegrationBlueprintApiClient, "async_get_data"):
yield


Expand All @@ -50,8 +49,7 @@ def bypass_get_data_fixture():
@pytest.fixture(name="error_on_get_data")
def error_get_data_fixture():
"""Simulate error when retrieving data from API."""
with patch(
"custom_components.integration_blueprint.IntegrationBlueprintApiClient.async_get_data",
side_effect=Exception,
with patch.object(
IntegrationBlueprintApiClient, "async_get_data", side_effect=Exception
):
yield
45 changes: 24 additions & 21 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import aiohttp
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from pytest import raises

from custom_components.integration_blueprint.api import IntegrationBlueprintApiClient

Expand All @@ -13,7 +14,7 @@ async def test_api(hass, aioclient_mock, caplog):
# To test the api submodule, we first create an instance of our API client
api = IntegrationBlueprintApiClient("test", "test", async_get_clientsession(hass))

# Use aioclient_mock which is provided by `pytest_homeassistant_custom_components`
# Use aioclient_mock which is provided by `pytest_homeassistant_custom_component`
# to mock responses to aiohttp requests. In this case we are telling the mock to
# return {"test": "test"} when a `GET` call is made to the specified URL. We then
# call `async_get_data` which will make that `GET` request.
Expand All @@ -26,6 +27,8 @@ async def test_api(hass, aioclient_mock, caplog):
# between the previous step and this one. We use `patch` here instead of `get`
# because we know that `async_set_title` calls `api_wrapper` with `patch` as the
# first parameter
aioclient_mock.clear_requests()
#
aioclient_mock.patch("https://jsonplaceholder.typicode.com/posts/1")
assert await api.async_set_title("test") is None

Expand All @@ -38,49 +41,49 @@ async def test_api(hass, aioclient_mock, caplog):
# useful during exception handling testing since often the only action as part of
# exception handling is a logging statement
caplog.clear()
aioclient_mock.clear_requests()
#
aioclient_mock.put(
"https://jsonplaceholder.typicode.com/posts/1", exc=asyncio.TimeoutError
)
assert (
with raises(asyncio.TimeoutError):
await api.api_wrapper("put", "https://jsonplaceholder.typicode.com/posts/1")
is None
)
assert (
len(caplog.record_tuples) == 1
and "Timeout error fetching information from" in caplog.record_tuples[0][2]
)

caplog.clear()
aioclient_mock.clear_requests()
#
aioclient_mock.post("https://jsonplaceholder.typicode.com/posts/3", exc=TypeError)
with raises(TypeError):
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/3")
assert (
len(caplog.record_tuples) == 1
and "Error parsing information from" in caplog.record_tuples[0][2]
)

caplog.clear()
aioclient_mock.clear_requests()
#
aioclient_mock.post(
"https://jsonplaceholder.typicode.com/posts/1", exc=aiohttp.ClientError
)
assert (
with raises(aiohttp.ClientError):
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/1")
is None
)
assert (
len(caplog.record_tuples) == 1
and "Error fetching information from" in caplog.record_tuples[0][2]
)

caplog.clear()
aioclient_mock.clear_requests()
#
aioclient_mock.post("https://jsonplaceholder.typicode.com/posts/2", exc=Exception)
assert (
with raises(Exception):
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/2")
is None
)
assert (
len(caplog.record_tuples) == 1
and "Something really wrong happened!" in caplog.record_tuples[0][2]
)

caplog.clear()
aioclient_mock.post("https://jsonplaceholder.typicode.com/posts/3", exc=TypeError)
assert (
await api.api_wrapper("post", "https://jsonplaceholder.typicode.com/posts/3")
is None
)
assert (
len(caplog.record_tuples) == 1
and "Error parsing information from" in caplog.record_tuples[0][2]
)

0 comments on commit 0317ad3

Please sign in to comment.