Skip to content

Commit

Permalink
Fix apsystemsapi test
Browse files Browse the repository at this point in the history
  • Loading branch information
mawoka-myblock committed Apr 28, 2024
1 parent f0b738f commit 9eb565b
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions tests/components/apsystemsapi_local/test_config_flow.py
Expand Up @@ -10,11 +10,10 @@
from homeassistant.data_entry_flow import FlowResultType


async def test_form_cannot_connect(hass: HomeAssistant, mock_setup_entry) -> None:
async def test_form_cannot_connect_and_recover(
hass: HomeAssistant, mock_setup_entry
) -> None:
"""Test we handle cannot connect error."""
# aioclient_mock.get(
# "http://127.0.0.2:8050/getDeviceInfo", exc=aiohttp.ClientConnectionError
# )
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
Expand Down Expand Up @@ -43,7 +42,7 @@ async def test_form_cannot_connect(hass: HomeAssistant, mock_setup_entry) -> Non
"homeassistant.components.apsystemsapi_local.config_flow.APsystemsEZ1M",
return_value=AsyncMock(),
) as mock_api:
mock_api.return_value.get_device_info = True
mock_api.return_value.get_device_info = AsyncMock(True)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
Expand All @@ -58,8 +57,51 @@ async def test_form_cannot_connect(hass: HomeAssistant, mock_setup_entry) -> Non
assert result2["data"].get(CONF_IP_ADDRESS) == "127.0.0.1"
assert result2["data"].get(CONF_NAME) == "Solar"
assert result2["data"].get("update_interval") == 15
# aioclient_mock.get(
# "http://127.0.0.1:8050/getDeviceInfo",
# json=SUCCESS_DEVICE_INFO_DATA,
# headers={"Content-Type": "application/json"},
# )


async def test_form_cannot_connect(hass: HomeAssistant, mock_setup_entry) -> None:
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.apsystemsapi_local.config_flow.APsystemsEZ1M",
return_value=AsyncMock(),
) as mock_api:
mock_api.side_effect = TimeoutError
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_IP_ADDRESS: "127.0.0.2",
CONF_NAME: "Solar",
"update_interval": 15,
},
)

assert result["type"] == FlowResultType.FORM
assert result["errors"] == {"base": "connection_refused"}


async def test_form_create_success(hass: HomeAssistant, mock_setup_entry) -> None:
"""Test we handle creatinw with success."""
with patch(
"homeassistant.components.apsystemsapi_local.config_flow.APsystemsEZ1M",
return_value=AsyncMock(),
) as mock_api:
mock_api.return_value.get_device_info = AsyncMock(True)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={
CONF_IP_ADDRESS: "127.0.0.1",
CONF_NAME: "Solar",
"update_interval": 15,
},
)
# AiohttpClientMockResponse does not have .ok
# So, I check if it's coming that far and fail if that's not the case
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert result["data"].get(CONF_IP_ADDRESS) == "127.0.0.1"
assert result["data"].get(CONF_NAME) == "Solar"
assert result["data"].get("update_interval") == 15

0 comments on commit 9eb565b

Please sign in to comment.