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
pull/28064/head^2
Yuki Ueda 2020-02-27 20:19:54 +09:00 committed by GitHub
parent df04fe3258
commit 0eb5ca67cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

View File

@ -6,12 +6,12 @@ import pytest
from transmissionrpc.error import TransmissionError
from homeassistant import data_entry_flow
from homeassistant.components import transmission
from homeassistant.components.transmission import config_flow
from homeassistant.components.transmission.const import (
DEFAULT_NAME,
DEFAULT_PORT,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
)
from homeassistant.const import (
CONF_HOST,
@ -73,6 +73,15 @@ def mock_api_unknown_error():
yield
@pytest.fixture(name="transmission_setup", autouse=True)
def transmission_setup_fixture():
"""Mock transmission entry setup."""
with patch(
"homeassistant.components.transmission.async_setup_entry", return_value=True
):
yield
def init_config_flow(hass):
"""Init a configuration flow."""
flow = config_flow.TransmissionFlowHandler()
@ -80,17 +89,21 @@ def init_config_flow(hass):
return flow
async def test_flow_works(hass, api):
async def test_flow_user_config(hass, api):
"""Test user config."""
flow = init_config_flow(hass)
result = await flow.async_step_user()
result = await hass.config_entries.flow.async_init(
transmission.DOMAIN, context={"source": "user"}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"
# test with required fields only
result = await flow.async_step_user(
{CONF_NAME: NAME, CONF_HOST: HOST, CONF_PORT: PORT}
async def test_flow_required_fields(hass, api):
"""Test with required fields only."""
result = await hass.config_entries.flow.async_init(
transmission.DOMAIN,
context={"source": "user"},
data={CONF_NAME: NAME, CONF_HOST: HOST, CONF_PORT: PORT},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
@ -99,8 +112,12 @@ async def test_flow_works(hass, api):
assert result["data"][CONF_HOST] == HOST
assert result["data"][CONF_PORT] == PORT
# test with all provided
result = await flow.async_step_user(MOCK_ENTRY)
async def test_flow_all_provided(hass, api):
"""Test with all provided."""
result = await hass.config_entries.flow.async_init(
transmission.DOMAIN, context={"source": "user"}, data=MOCK_ENTRY
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == NAME
@ -114,7 +131,7 @@ async def test_flow_works(hass, api):
async def test_options(hass):
"""Test updating options."""
entry = MockConfigEntry(
domain=DOMAIN,
domain=transmission.DOMAIN,
title=CONF_NAME,
data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
@ -174,13 +191,14 @@ async def test_import(hass, api):
async def test_host_already_configured(hass, api):
"""Test host is already configured."""
entry = MockConfigEntry(
domain=DOMAIN,
domain=transmission.DOMAIN,
data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
)
entry.add_to_hass(hass)
flow = init_config_flow(hass)
result = await flow.async_step_user(MOCK_ENTRY)
result = await hass.config_entries.flow.async_init(
transmission.DOMAIN, context={"source": "user"}, data=MOCK_ENTRY
)
assert result["type"] == "abort"
assert result["reason"] == "already_configured"
@ -189,7 +207,7 @@ async def test_host_already_configured(hass, api):
async def test_name_already_configured(hass, api):
"""Test name is already configured."""
entry = MockConfigEntry(
domain=DOMAIN,
domain=transmission.DOMAIN,
data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
)
@ -197,8 +215,9 @@ async def test_name_already_configured(hass, api):
mock_entry = MOCK_ENTRY.copy()
mock_entry[CONF_HOST] = "0.0.0.0"
flow = init_config_flow(hass)
result = await flow.async_step_user(mock_entry)
result = await hass.config_entries.flow.async_init(
transmission.DOMAIN, context={"source": "user"}, data=mock_entry
)
assert result["type"] == "form"
assert result["errors"] == {CONF_NAME: "name_exists"}