|
1 | 1 | """Tests for knmi api."""
|
2 | 2 |
|
| 3 | +import asyncio |
| 4 | +import socket |
| 5 | + |
| 6 | +import aiohttp |
3 | 7 | from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
4 | 8 | from homeassistant.core import HomeAssistant
|
5 | 9 | from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
9 | 13 | from custom_components.knmi.api import (
|
10 | 14 | KnmiApiClient,
|
11 | 15 | KnmiApiClientApiKeyError,
|
| 16 | + KnmiApiClientCommunicationError, |
12 | 17 | KnmiApiClientError,
|
13 | 18 | KnmiApiRateLimitError,
|
14 | 19 | )
|
@@ -53,6 +58,41 @@ async def test_api_error(
|
53 | 58 | await api.async_get_data()
|
54 | 59 |
|
55 | 60 |
|
| 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 | + |
56 | 96 | @pytest.mark.fixture("_.json")
|
57 | 97 | async def test_invalid_json_fix(hass: HomeAssistant, mocked_data):
|
58 | 98 | """Test for fix https://github.com/golles/ha-knmi/issues/130."""
|
|
0 commit comments