Make met config flow tests robust (#30689)

* Make met config flow tests more robust

* Fix test with default form values

* Remove not needed test

* Clean create entry test

* Clean double test

* Clean last tests

* Fix docstring
This commit is contained in:
Martin Hjelmare 2020-01-12 05:00:02 +01:00 committed by Paulus Schoutsen
parent 117efb5a04
commit 91f738127d

View File

@ -1,32 +1,24 @@
"""Tests for Met.no config flow.""" """Tests for Met.no config flow."""
from unittest.mock import Mock, patch from asynctest import patch
import pytest
from homeassistant.components.met import config_flow from homeassistant.components.met.const import DOMAIN, HOME_LOCATION_NAME
from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_ELEVATION, CONF_LATITUDE, CONF_LONGITUDE
from tests.common import MockConfigEntry, mock_coro from tests.common import MockConfigEntry
async def test_show_config_form(): @pytest.fixture(name="met_setup", autouse=True)
def met_setup_fixture():
"""Patch met setup entry."""
with patch("homeassistant.components.met.async_setup_entry", return_value=True):
yield
async def test_show_config_form(hass):
"""Test show configuration form.""" """Test show configuration form."""
hass = Mock() result = await hass.config_entries.flow.async_init(
flow = config_flow.MetFlowHandler() DOMAIN, context={"source": "user"}
flow.hass = hass
result = await flow._show_config_form()
assert result["type"] == "form"
assert result["step_id"] == "user"
async def test_show_config_form_default_values():
"""Test show configuration form."""
hass = Mock()
flow = config_flow.MetFlowHandler()
flow.hass = hass
result = await flow._show_config_form(
name="test", latitude="0", longitude="0", elevation="0"
) )
assert result["type"] == "form" assert result["type"] == "form"
@ -34,117 +26,81 @@ async def test_show_config_form_default_values():
async def test_flow_with_home_location(hass): async def test_flow_with_home_location(hass):
"""Test config flow . """Test config flow.
Tests the flow when a default location is configured Test the flow when a default location is configured.
then it should return a form with default values Then it should return a form with default values.
""" """
flow = config_flow.MetFlowHandler()
flow.hass = hass
hass.config.location_name = "Home"
hass.config.latitude = 1 hass.config.latitude = 1
hass.config.longitude = 1 hass.config.longitude = 2
hass.config.elevation = 1 hass.config.elevation = 3
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "user"}
)
result = await flow.async_step_user()
assert result["type"] == "form" assert result["type"] == "form"
assert result["step_id"] == "user" assert result["step_id"] == "user"
default_data = result["data_schema"]({})
async def test_flow_show_form(): assert default_data["name"] == HOME_LOCATION_NAME
"""Test show form scenarios first time. assert default_data["latitude"] == 1
assert default_data["longitude"] == 2
Test when the form should show when no configurations exists assert default_data["elevation"] == 3
"""
hass = Mock()
flow = config_flow.MetFlowHandler()
flow.hass = hass
with patch.object(
flow, "_show_config_form", return_value=mock_coro()
) as config_form:
await flow.async_step_user()
assert len(config_form.mock_calls) == 1
async def test_flow_entry_created_from_user_input(): async def test_create_entry(hass):
"""Test that create data from user input. """Test create entry from user input."""
Test when the form should show when no configurations exists
"""
hass = Mock()
flow = config_flow.MetFlowHandler()
flow.hass = hass
test_data = { test_data = {
"name": "home", "name": "home",
CONF_LONGITUDE: "0", CONF_LONGITUDE: 0,
CONF_LATITUDE: "0", CONF_LATITUDE: 0,
CONF_ELEVATION: "0", CONF_ELEVATION: 0,
} }
# Test that entry created when user_input name not exists result = await hass.config_entries.flow.async_init(
with patch.object( DOMAIN, context={"source": "user"}, data=test_data
flow, "_show_config_form", return_value=mock_coro() )
) as config_form, patch.object(
flow.hass.config_entries, "async_entries", return_value=mock_coro()
) as config_entries:
result = await flow.async_step_user(user_input=test_data) assert result["type"] == "create_entry"
assert result["title"] == "home"
assert result["type"] == "create_entry" assert result["data"] == test_data
assert result["data"] == test_data
assert len(config_entries.mock_calls) == 1
assert not config_form.mock_calls
async def test_flow_entry_config_entry_already_exists(): async def test_flow_entry_already_exists(hass):
"""Test that create data from user input and config_entry already exists. """Test user input for config_entry that already exists.
Test when the form should show when user puts existing location Test when the form should show when user puts existing location
in the config gui. Then the form should show with error in the config gui. Then the form should show with error.
""" """
hass = Mock()
flow = config_flow.MetFlowHandler()
flow.hass = hass
first_entry = MockConfigEntry(domain="met") first_entry = MockConfigEntry(domain="met")
first_entry.data["name"] = "home" first_entry.data["name"] = "home"
first_entry.data[CONF_LONGITUDE] = "0" first_entry.data[CONF_LONGITUDE] = 0
first_entry.data[CONF_LATITUDE] = "0" first_entry.data[CONF_LATITUDE] = 0
first_entry.data[CONF_ELEVATION] = 0
first_entry.add_to_hass(hass) first_entry.add_to_hass(hass)
test_data = { test_data = {
"name": "home", "name": "home",
CONF_LONGITUDE: "0", CONF_LONGITUDE: 0,
CONF_LATITUDE: "0", CONF_LATITUDE: 0,
CONF_ELEVATION: "0", CONF_ELEVATION: 0,
} }
with patch.object( result = await hass.config_entries.flow.async_init(
flow, "_show_config_form", return_value=mock_coro() DOMAIN, context={"source": "user"}, data=test_data
) as config_form, patch.object( )
flow.hass.config_entries, "async_entries", return_value=[first_entry]
) as config_entries:
await flow.async_step_user(user_input=test_data) assert result["type"] == "form"
assert result["errors"]["name"] == "name_exists"
assert len(config_form.mock_calls) == 1
assert len(config_entries.mock_calls) == 1
assert len(flow._errors) == 1
async def test_onboarding_step(hass, mock_weather): async def test_onboarding_step(hass):
"""Test initializing via onboarding step.""" """Test initializing via onboarding step."""
hass = Mock() result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "onboarding"}, data={}
flow = config_flow.MetFlowHandler() )
flow.hass = hass
result = await flow.async_step_onboarding({})
assert result["type"] == "create_entry" assert result["type"] == "create_entry"
assert result["title"] == "Home" assert result["title"] == HOME_LOCATION_NAME
assert result["data"] == {"track_home": True} assert result["data"] == {"track_home": True}