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.""" """Tests for the Heos config flow module."""
from urllib.parse import urlparse from urllib.parse import urlparse
from asynctest import patch
from pyheos import HeosError from pyheos import HeosError
from homeassistant import data_entry_flow 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.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 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): async def test_cannot_connect_shows_error_form(hass, controller):
"""Test form is shown with error when cannot connect.""" """Test form is shown with error when cannot connect."""
flow = HeosFlowHandler()
flow.hass = hass
controller.connect.side_effect = HeosError() 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["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"][CONF_HOST] == "connection_failure" assert result["errors"][CONF_HOST] == "connection_failure"
@ -47,10 +48,11 @@ async def test_cannot_connect_shows_error_form(hass, controller):
async def test_create_entry_when_host_valid(hass, controller): async def test_create_entry_when_host_valid(hass, controller):
"""Test result type is create entry when host is valid.""" """Test result type is create entry when host is valid."""
flow = HeosFlowHandler()
flow.hass = hass
data = {CONF_HOST: "127.0.0.1"} data = {CONF_HOST: "127.0.0.1"}
result = await flow.async_step_user(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["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)" assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == data assert result["data"] == data
@ -61,10 +63,11 @@ async def test_create_entry_when_host_valid(hass, controller):
async def test_create_entry_when_friendly_name_valid(hass, controller): async def test_create_entry_when_friendly_name_valid(hass, controller):
"""Test result type is create entry when friendly name is valid.""" """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"} 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)"} data = {CONF_HOST: "Office (127.0.0.1)"}
result = await flow.async_step_user(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["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "Controller (127.0.0.1)" assert result["title"] == "Controller (127.0.0.1)"
assert result["data"] == {CONF_HOST: "127.0.0.1"} assert result["data"] == {CONF_HOST: "127.0.0.1"}
@ -76,7 +79,7 @@ async def test_create_entry_when_friendly_name_valid(hass, controller):
async def test_discovery_shows_create_form(hass, controller, discovery_data): async def test_discovery_shows_create_form(hass, controller, discovery_data):
"""Test discovery shows form to confirm setup and subsequent abort.""" """Test discovery shows form to confirm setup and subsequent abort."""
await hass.config_entries.flow.async_init( 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() await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1 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_SSDP_LOCATION] = f"http://127.0.0.2:{port}/"
discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom" discovery_data[ssdp.ATTR_UPNP_FRIENDLY_NAME] = "Bedroom"
await hass.config_entries.flow.async_init( 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() await hass.async_block_till_done()
assert len(hass.config_entries.flow.async_progress()) == 1 assert len(hass.config_entries.flow.async_progress()) == 1

View File

@ -6,12 +6,12 @@ import pytest
from transmissionrpc.error import TransmissionError from transmissionrpc.error import TransmissionError
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components import transmission
from homeassistant.components.transmission import config_flow from homeassistant.components.transmission import config_flow
from homeassistant.components.transmission.const import ( from homeassistant.components.transmission.const import (
DEFAULT_NAME, DEFAULT_NAME,
DEFAULT_PORT, DEFAULT_PORT,
DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL,
DOMAIN,
) )
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
@ -73,6 +73,15 @@ def mock_api_unknown_error():
yield 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): def init_config_flow(hass):
"""Init a configuration flow.""" """Init a configuration flow."""
flow = config_flow.TransmissionFlowHandler() flow = config_flow.TransmissionFlowHandler()
@ -80,17 +89,21 @@ def init_config_flow(hass):
return flow return flow
async def test_flow_works(hass, api): async def test_flow_user_config(hass, api):
"""Test user config.""" """Test user config."""
flow = init_config_flow(hass) result = await hass.config_entries.flow.async_init(
transmission.DOMAIN, context={"source": "user"}
result = await flow.async_step_user() )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user" assert result["step_id"] == "user"
# test with required fields only
result = await flow.async_step_user( async def test_flow_required_fields(hass, api):
{CONF_NAME: NAME, CONF_HOST: HOST, CONF_PORT: PORT} """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 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_HOST] == HOST
assert result["data"][CONF_PORT] == PORT 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["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == NAME assert result["title"] == NAME
@ -114,7 +131,7 @@ async def test_flow_works(hass, api):
async def test_options(hass): async def test_options(hass):
"""Test updating options.""" """Test updating options."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=transmission.DOMAIN,
title=CONF_NAME, title=CONF_NAME,
data=MOCK_ENTRY, data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}, 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): async def test_host_already_configured(hass, api):
"""Test host is already configured.""" """Test host is already configured."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=transmission.DOMAIN,
data=MOCK_ENTRY, data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}, options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL},
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
flow = init_config_flow(hass) result = await hass.config_entries.flow.async_init(
result = await flow.async_step_user(MOCK_ENTRY) transmission.DOMAIN, context={"source": "user"}, data=MOCK_ENTRY
)
assert result["type"] == "abort" assert result["type"] == "abort"
assert result["reason"] == "already_configured" 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): async def test_name_already_configured(hass, api):
"""Test name is already configured.""" """Test name is already configured."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=transmission.DOMAIN,
data=MOCK_ENTRY, data=MOCK_ENTRY,
options={CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}, 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 = MOCK_ENTRY.copy()
mock_entry[CONF_HOST] = "0.0.0.0" mock_entry[CONF_HOST] = "0.0.0.0"
flow = init_config_flow(hass) result = await hass.config_entries.flow.async_init(
result = await flow.async_step_user(mock_entry) transmission.DOMAIN, context={"source": "user"}, data=mock_entry
)
assert result["type"] == "form" assert result["type"] == "form"
assert result["errors"] == {CONF_NAME: "name_exists"} assert result["errors"] == {CONF_NAME: "name_exists"}