Make heos and transmission config flow tests more robust (#31783)

* async_step_user to async_configure

* fix for lint

* fix for pylint

* fix isort and black

* fix miss-fixed black

* fixing for the python37 coverage

* fix transmission definition

* fix for Black formatting

* fix type to abort

* clean up

* clean up for the test

* fix for the test

* refactor

* split test_flow_works to three tests

* revert the assert

* remove whitespaces for flake8

* apply patch function

* fix for the patch

* fix for the black

* remove mock_coro

* fix for the black

* hue to heos

* try to fix import

* fix for the black

* revert try to fix import

* Add a pytest fixture

* fix for the black
This commit is contained in:
Yuki Ueda
2020-02-27 20:19:54 +09:00
committed by GitHub
parent df04fe3258
commit 0eb5ca67cd
2 changed files with 63 additions and 41 deletions

View File

@@ -1,12 +1,13 @@
"""Tests for the Heos config flow module."""
from urllib.parse import urlparse
from asynctest import patch
from pyheos import HeosError
from homeassistant import data_entry_flow
from homeassistant.components import ssdp
from homeassistant.components import heos, ssdp
from homeassistant.components.heos.config_flow import HeosFlowHandler
from homeassistant.components.heos.const import DATA_DISCOVERED_HOSTS, DOMAIN
from homeassistant.components.heos.const import DATA_DISCOVERED_HOSTS
from homeassistant.const import CONF_HOST
@@ -32,10 +33,10 @@ async def test_no_host_shows_form(hass):
async def test_cannot_connect_shows_error_form(hass, controller):
"""Test form is shown with error when cannot connect."""
flow = HeosFlowHandler()
flow.hass = hass
controller.connect.side_effect = HeosError()
result = await flow.async_step_user({CONF_HOST: "127.0.0.1"})
result = await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": "user"}, data={CONF_HOST: "127.0.0.1"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"][CONF_HOST] == "connection_failure"
@@ -47,36 +48,38 @@ async def test_cannot_connect_shows_error_form(hass, controller):
async def test_create_entry_when_host_valid(hass, controller):
"""Test result type is create entry when host is valid."""
flow = HeosFlowHandler()
flow.hass = hass
data = {CONF_HOST: "127.0.0.1"}
result = await flow.async_step_user(data)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == data
assert controller.connect.call_count == 1
assert controller.disconnect.call_count == 1
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": "user"}, data=data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == data
assert controller.connect.call_count == 1
assert controller.disconnect.call_count == 1
async def test_create_entry_when_friendly_name_valid(hass, controller):
"""Test result type is create entry when friendly name is valid."""
hass.data[DATA_DISCOVERED_HOSTS] = {"Office (127.0.0.1)": "127.0.0.1"}
flow = HeosFlowHandler()
flow.hass = hass
data = {CONF_HOST: "Office (127.0.0.1)"}
result = await flow.async_step_user(data)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == {CONF_HOST: "127.0.0.1"}
assert controller.connect.call_count == 1
assert controller.disconnect.call_count == 1
assert DATA_DISCOVERED_HOSTS not in hass.data
with patch("homeassistant.components.heos.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
heos.DOMAIN, context={"source": "user"}, data=data
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == {CONF_HOST: "127.0.0.1"}
assert controller.connect.call_count == 1
assert controller.disconnect.call_count == 1
assert DATA_DISCOVERED_HOSTS not in hass.data
async def test_discovery_shows_create_form(hass, controller, discovery_data):
"""Test discovery shows form to confirm setup and subsequent abort."""
await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "ssdp"}, data=discovery_data
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
)
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1
@@ -86,7 +89,7 @@ async def test_discovery_shows_create_form(hass, controller, discovery_data):
discovery_data[ssdp.ATTR_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "ssdp"}, data=discovery_data
heos.DOMAIN, context={"source": "ssdp"}, data=discovery_data
)
await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1