mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Merge Whirlpool tests into a parameterized test (#136490)
* Use fixtures in config flow tests for Whirlpool * Keep old tests; new one will go to separate PR * Merge Whirlpool tests into a parameterized test * Address review comments * Remove uneeded block wait calls
This commit is contained in:
parent
e0ea5bfc51
commit
5a53ed9e5b
@ -20,9 +20,22 @@ CONFIG_INPUT = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="mock_whirlpool_setup_entry")
|
||||||
|
def fixture_mock_whirlpool_setup_entry():
|
||||||
|
"""Set up async_setup_entry fixture."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
|
||||||
|
) as mock_setup_entry:
|
||||||
|
yield mock_setup_entry
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
|
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
|
||||||
async def test_form(
|
async def test_form(
|
||||||
hass: HomeAssistant, region, brand, mock_backend_selector_api: MagicMock
|
hass: HomeAssistant,
|
||||||
|
region,
|
||||||
|
brand,
|
||||||
|
mock_backend_selector_api: MagicMock,
|
||||||
|
mock_whirlpool_setup_entry: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -32,14 +45,10 @@ async def test_form(
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == config_entries.SOURCE_USER
|
assert result["step_id"] == config_entries.SOURCE_USER
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
|
result["flow_id"],
|
||||||
) as mock_setup_entry:
|
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
)
|
||||||
result["flow_id"],
|
|
||||||
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2["title"] == "test-username"
|
assert result2["title"] == "test-username"
|
||||||
@ -49,7 +58,7 @@ async def test_form(
|
|||||||
"region": region[0],
|
"region": region[0],
|
||||||
"brand": brand[0],
|
"brand": brand[0],
|
||||||
}
|
}
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_whirlpool_setup_entry.mock_calls) == 1
|
||||||
mock_backend_selector_api.assert_called_once_with(brand[1], region[1])
|
mock_backend_selector_api.assert_called_once_with(brand[1], region[1])
|
||||||
|
|
||||||
|
|
||||||
@ -70,19 +79,31 @@ async def test_form_invalid_auth(
|
|||||||
assert result2["errors"] == {"base": "invalid_auth"}
|
assert result2["errors"] == {"base": "invalid_auth"}
|
||||||
|
|
||||||
|
|
||||||
async def test_form_cannot_connect(
|
@pytest.mark.usefixtures("mock_appliances_manager_api")
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("exception", "expected_error"),
|
||||||
|
[
|
||||||
|
(aiohttp.ClientConnectionError, "cannot_connect"),
|
||||||
|
(TimeoutError, "cannot_connect"),
|
||||||
|
(Exception, "unknown"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_form_auth_error(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
exception: Exception,
|
||||||
|
expected_error: str,
|
||||||
region,
|
region,
|
||||||
brand,
|
brand,
|
||||||
mock_auth_api: MagicMock,
|
mock_auth_api: MagicMock,
|
||||||
|
mock_whirlpool_setup_entry: MagicMock,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle cannot connect error."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_auth_api.return_value.do_auth.side_effect = aiohttp.ClientConnectionError
|
mock_auth_api.return_value.do_auth.side_effect = exception
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
CONFIG_INPUT
|
CONFIG_INPUT
|
||||||
| {
|
| {
|
||||||
@ -90,56 +111,25 @@ async def test_form_cannot_connect(
|
|||||||
"brand": brand[0],
|
"brand": brand[0],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
assert result["errors"] == {"base": expected_error}
|
||||||
|
|
||||||
|
# Test that it succeeds after the error is cleared
|
||||||
async def test_form_auth_timeout(
|
mock_auth_api.return_value.do_auth.side_effect = None
|
||||||
hass: HomeAssistant,
|
result = await hass.config_entries.flow.async_configure(
|
||||||
region,
|
|
||||||
brand,
|
|
||||||
mock_auth_api: MagicMock,
|
|
||||||
) -> None:
|
|
||||||
"""Test we handle auth timeout error."""
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
|
|
||||||
mock_auth_api.return_value.do_auth.side_effect = TimeoutError
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
CONFIG_INPUT
|
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
|
||||||
| {
|
|
||||||
"region": region[0],
|
|
||||||
"brand": brand[0],
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_generic_auth_exception(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
region,
|
|
||||||
brand,
|
|
||||||
mock_auth_api: MagicMock,
|
|
||||||
) -> None:
|
|
||||||
"""Test we handle cannot connect error."""
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_auth_api.return_value.do_auth.side_effect = Exception
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
assert result["title"] == "test-username"
|
||||||
result["flow_id"],
|
assert result["data"] == {
|
||||||
CONFIG_INPUT
|
"username": "test-username",
|
||||||
| {
|
"password": "test-password",
|
||||||
"region": region[0],
|
"region": region[0],
|
||||||
"brand": brand[0],
|
"brand": brand[0],
|
||||||
},
|
}
|
||||||
)
|
assert len(mock_whirlpool_setup_entry.mock_calls) == 1
|
||||||
assert result2["type"] is FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "unknown"}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
|
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
|
||||||
@ -167,7 +157,6 @@ async def test_form_already_configured(hass: HomeAssistant, region, brand) -> No
|
|||||||
"brand": brand[0],
|
"brand": brand[0],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.ABORT
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
assert result2["reason"] == "already_configured"
|
assert result2["reason"] == "already_configured"
|
||||||
@ -191,13 +180,14 @@ async def test_no_appliances_flow(
|
|||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
|
CONFIG_INPUT | {"region": region[0], "brand": brand[0]},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result2["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "no_appliances"}
|
assert result2["errors"] == {"base": "no_appliances"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_auth_api", "mock_appliances_manager_api")
|
@pytest.mark.usefixtures(
|
||||||
|
"mock_auth_api", "mock_appliances_manager_api", "mock_whirlpool_setup_entry"
|
||||||
|
)
|
||||||
async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
|
async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
|
||||||
"""Test a successful reauth flow."""
|
"""Test a successful reauth flow."""
|
||||||
mock_entry = MockConfigEntry(
|
mock_entry = MockConfigEntry(
|
||||||
@ -213,14 +203,10 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
|
result["flow_id"],
|
||||||
):
|
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
)
|
||||||
result["flow_id"],
|
|
||||||
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.ABORT
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
assert result2["reason"] == "reauth_successful"
|
assert result2["reason"] == "reauth_successful"
|
||||||
@ -232,7 +218,7 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_appliances_manager_api")
|
@pytest.mark.usefixtures("mock_appliances_manager_api", "mock_whirlpool_setup_entry")
|
||||||
async def test_reauth_flow_auth_error(
|
async def test_reauth_flow_auth_error(
|
||||||
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
|
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -251,20 +237,16 @@ async def test_reauth_flow_auth_error(
|
|||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
mock_auth_api.return_value.is_access_token_valid.return_value = False
|
mock_auth_api.return_value.is_access_token_valid.return_value = False
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
|
result["flow_id"],
|
||||||
):
|
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
)
|
||||||
result["flow_id"],
|
|
||||||
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result2["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "invalid_auth"}
|
assert result2["errors"] == {"base": "invalid_auth"}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("mock_appliances_manager_api")
|
@pytest.mark.usefixtures("mock_appliances_manager_api", "mock_whirlpool_setup_entry")
|
||||||
async def test_reauth_flow_connnection_error(
|
async def test_reauth_flow_connnection_error(
|
||||||
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
|
hass: HomeAssistant, region, brand, mock_auth_api: MagicMock
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -284,13 +266,10 @@ async def test_reauth_flow_connnection_error(
|
|||||||
assert result["errors"] == {}
|
assert result["errors"] == {}
|
||||||
|
|
||||||
mock_auth_api.return_value.do_auth.side_effect = ClientConnectionError
|
mock_auth_api.return_value.do_auth.side_effect = ClientConnectionError
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.whirlpool.async_setup_entry", return_value=True
|
result["flow_id"],
|
||||||
):
|
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
)
|
||||||
result["flow_id"],
|
|
||||||
{CONF_PASSWORD: "new-password", CONF_BRAND: brand[0]},
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
assert result2["type"] is FlowResultType.FORM
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
assert result2["errors"] == {"base": "cannot_connect"}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user