mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
AccuWeather tests refactoring (#116923)
* Add mock_accuweather_client * Improve tests * Fix exceptions * Remove unneeded update_listener() * Fix arguments for fixtures --------- Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Define tests for the AccuWeather config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from accuweather import ApiError, InvalidApiKeyError, RequestsExceededError
|
||||
|
||||
@@ -10,7 +10,7 @@ from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
VALID_CONFIG = {
|
||||
CONF_NAME: "abcd",
|
||||
@@ -48,95 +48,90 @@ async def test_api_key_too_short(hass: HomeAssistant) -> None:
|
||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
|
||||
|
||||
async def test_invalid_api_key(hass: HomeAssistant) -> None:
|
||||
async def test_invalid_api_key(
|
||||
hass: HomeAssistant, mock_accuweather_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test that errors are shown when API key is invalid."""
|
||||
with patch(
|
||||
"homeassistant.components.accuweather.AccuWeather._async_get_data",
|
||||
side_effect=InvalidApiKeyError("Invalid API key"),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
mock_accuweather_client.async_get_location.side_effect = InvalidApiKeyError(
|
||||
"Invalid API key"
|
||||
)
|
||||
|
||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
|
||||
assert result["errors"] == {CONF_API_KEY: "invalid_api_key"}
|
||||
|
||||
|
||||
async def test_api_error(hass: HomeAssistant) -> None:
|
||||
async def test_api_error(
|
||||
hass: HomeAssistant, mock_accuweather_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test API error."""
|
||||
with patch(
|
||||
"homeassistant.components.accuweather.AccuWeather._async_get_data",
|
||||
side_effect=ApiError("Invalid response from AccuWeather API"),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
mock_accuweather_client.async_get_location.side_effect = ApiError(
|
||||
"Invalid response from AccuWeather API"
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_requests_exceeded_error(hass: HomeAssistant) -> None:
|
||||
async def test_requests_exceeded_error(
|
||||
hass: HomeAssistant, mock_accuweather_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test requests exceeded error."""
|
||||
with patch(
|
||||
"homeassistant.components.accuweather.AccuWeather._async_get_data",
|
||||
side_effect=RequestsExceededError(
|
||||
"The allowed number of requests has been exceeded"
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
mock_accuweather_client.async_get_location.side_effect = RequestsExceededError(
|
||||
"The allowed number of requests has been exceeded"
|
||||
)
|
||||
|
||||
assert result["errors"] == {CONF_API_KEY: "requests_exceeded"}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
|
||||
assert result["errors"] == {CONF_API_KEY: "requests_exceeded"}
|
||||
|
||||
|
||||
async def test_integration_already_exists(hass: HomeAssistant) -> None:
|
||||
async def test_integration_already_exists(
|
||||
hass: HomeAssistant, mock_accuweather_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test we only allow a single config flow."""
|
||||
with patch(
|
||||
"homeassistant.components.accuweather.AccuWeather._async_get_data",
|
||||
return_value=load_json_object_fixture("accuweather/location_data.json"),
|
||||
):
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="123456",
|
||||
data=VALID_CONFIG,
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id="123456",
|
||||
data=VALID_CONFIG,
|
||||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "single_instance_allowed"
|
||||
|
||||
|
||||
async def test_create_entry(hass: HomeAssistant) -> None:
|
||||
async def test_create_entry(
|
||||
hass: HomeAssistant, mock_accuweather_client: AsyncMock
|
||||
) -> None:
|
||||
"""Test that the user step works."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.accuweather.AccuWeather._async_get_data",
|
||||
return_value=load_json_object_fixture("accuweather/location_data.json"),
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.accuweather.async_setup_entry", return_value=True
|
||||
),
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=VALID_CONFIG,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "abcd"
|
||||
assert result["data"][CONF_NAME] == "abcd"
|
||||
assert result["data"][CONF_LATITUDE] == 55.55
|
||||
assert result["data"][CONF_LONGITUDE] == 122.12
|
||||
assert result["data"][CONF_API_KEY] == "32-character-string-1234567890qw"
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "abcd"
|
||||
assert result["data"][CONF_NAME] == "abcd"
|
||||
assert result["data"][CONF_LATITUDE] == 55.55
|
||||
assert result["data"][CONF_LONGITUDE] == 122.12
|
||||
assert result["data"][CONF_API_KEY] == "32-character-string-1234567890qw"
|
||||
|
||||
Reference in New Issue
Block a user