Refactor bond unit tests to reduce boilerplate (#38177)

* Refactor bond unit tests to reduce boilerplate

* Refactor bond unit tests to reduce boilerplate (PR feedback)

* Refactor bond unit tests to reduce boilerplate (PR feedback, nullcontext)
This commit is contained in:
Eugene Prystupa authored and GitHub committed 2020-07-26 12:15:21 -05:00
1 parent 34ac4e78af
commit 2d6eb5c05d
3 files changed
+122 -80

No files matched your search

+17 -18
View File
@@ -6,6 +6,8 @@ from homeassistant import config_entries, core, setup
from homeassistant.components.bond.const import DOMAIN
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from .common import patch_bond_device_ids
from tests.async_mock import Mock, patch
@@ -18,21 +20,20 @@ async def test_form(hass: core.HomeAssistant):
assert result["type"] == "form"
assert result["errors"] == {}
with patch(
"homeassistant.components.bond.config_flow.Bond.devices", return_value=[],
), patch(
with patch_bond_device_ids(), patch(
"homeassistant.components.bond.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.bond.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "1.1.1.1"
assert result2["title"] == "some host"
assert result2["data"] == {
CONF_HOST: "1.1.1.1",
CONF_HOST: "some host",
CONF_ACCESS_TOKEN: "test-token",
}
await hass.async_block_till_done()
@@ -46,12 +47,12 @@ async def test_form_invalid_auth(hass: core.HomeAssistant):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.bond.config_flow.Bond.devices",
with patch_bond_device_ids(
side_effect=ClientResponseError(Mock(), Mock(), status=401),
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "form"
@@ -64,12 +65,10 @@ async def test_form_cannot_connect(hass: core.HomeAssistant):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.bond.config_flow.Bond.devices",
side_effect=ClientConnectionError(),
):
with patch_bond_device_ids(side_effect=ClientConnectionError()):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "form"
@@ -82,12 +81,12 @@ async def test_form_unexpected_error(hass: core.HomeAssistant):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.bond.config_flow.Bond.devices",
side_effect=ClientResponseError(Mock(), Mock(), status=500),
with patch_bond_device_ids(
side_effect=ClientResponseError(Mock(), Mock(), status=500)
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
result["flow_id"],
{CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
)
assert result2["type"] == "form"