Skip to content

Commit c86107c

Browse files
authored
Merge pull request #147 from golles/test-coverage
Add test coverage for missing lines
2 parents 324e494 + f361455 commit c86107c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/test_api.py

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Tests for knmi api."""
22

3+
import asyncio
4+
import socket
5+
6+
import aiohttp
37
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
48
from homeassistant.core import HomeAssistant
59
from homeassistant.helpers.aiohttp_client import async_get_clientsession
@@ -9,6 +13,7 @@
913
from custom_components.knmi.api import (
1014
KnmiApiClient,
1115
KnmiApiClientApiKeyError,
16+
KnmiApiClientCommunicationError,
1217
KnmiApiClientError,
1318
KnmiApiRateLimitError,
1419
)
@@ -53,6 +58,41 @@ async def test_api_error(
5358
await api.async_get_data()
5459

5560

61+
@pytest.mark.parametrize(
62+
"http_exception",
63+
[
64+
aiohttp.ClientError,
65+
socket.gaierror,
66+
asyncio.TimeoutError,
67+
],
68+
)
69+
async def test_connection_error(
70+
hass: HomeAssistant,
71+
aioclient_mock: AiohttpClientMocker,
72+
http_exception: Exception,
73+
):
74+
"""Test http exception."""
75+
76+
api = KnmiApiClient(
77+
MOCK_CONFIG[CONF_API_KEY],
78+
MOCK_CONFIG[CONF_LATITUDE],
79+
MOCK_CONFIG[CONF_LONGITUDE],
80+
async_get_clientsession(hass),
81+
)
82+
83+
aioclient_mock.get(
84+
API_ENDPOINT.format(
85+
MOCK_CONFIG[CONF_API_KEY],
86+
MOCK_CONFIG[CONF_LATITUDE],
87+
MOCK_CONFIG[CONF_LONGITUDE],
88+
),
89+
exc=http_exception,
90+
)
91+
92+
with pytest.raises(KnmiApiClientCommunicationError):
93+
await api.async_get_data()
94+
95+
5696
@pytest.mark.fixture("_.json")
5797
async def test_invalid_json_fix(hass: HomeAssistant, mocked_data):
5898
"""Test for fix https://github.com/golles/ha-knmi/issues/130."""

0 commit comments

Comments
 (0)