Improve the reliability of tests in Tessie (#118596)
This commit is contained in:
parent
00f78dc522
commit
e4be3d8435
5 changed files with 89 additions and 59 deletions
|
@ -93,13 +93,9 @@ class TessieConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
except ClientConnectionError:
|
except ClientConnectionError:
|
||||||
errors["base"] = "cannot_connect"
|
errors["base"] = "cannot_connect"
|
||||||
else:
|
else:
|
||||||
self.hass.config_entries.async_update_entry(
|
return self.async_update_reload_and_abort(
|
||||||
self._reauth_entry, data=user_input
|
self._reauth_entry, data=user_input
|
||||||
)
|
)
|
||||||
self.hass.async_create_task(
|
|
||||||
self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="reauth_confirm",
|
step_id="reauth_confirm",
|
||||||
|
|
|
@ -7,6 +7,7 @@ from aiohttp import ClientConnectionError, ClientResponseError
|
||||||
from aiohttp.client import RequestInfo
|
from aiohttp.client import RequestInfo
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.components.tessie import PLATFORMS
|
||||||
from homeassistant.components.tessie.const import DOMAIN, TessieStatus
|
from homeassistant.components.tessie.const import DOMAIN, TessieStatus
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
@ -47,7 +48,7 @@ ERROR_CONNECTION = ClientConnectionError()
|
||||||
|
|
||||||
|
|
||||||
async def setup_platform(
|
async def setup_platform(
|
||||||
hass: HomeAssistant, platforms: list[Platform] = [], side_effect=None
|
hass: HomeAssistant, platforms: list[Platform] = PLATFORMS
|
||||||
) -> MockConfigEntry:
|
) -> MockConfigEntry:
|
||||||
"""Set up the Tessie platform."""
|
"""Set up the Tessie platform."""
|
||||||
|
|
||||||
|
@ -57,14 +58,7 @@ async def setup_platform(
|
||||||
)
|
)
|
||||||
mock_entry.add_to_hass(hass)
|
mock_entry.add_to_hass(hass)
|
||||||
|
|
||||||
with (
|
with patch("homeassistant.components.tessie.PLATFORMS", platforms):
|
||||||
patch(
|
|
||||||
"homeassistant.components.tessie.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
side_effect=side_effect,
|
|
||||||
),
|
|
||||||
patch("homeassistant.components.tessie.PLATFORMS", platforms),
|
|
||||||
):
|
|
||||||
await hass.config_entries.async_setup(mock_entry.entry_id)
|
await hass.config_entries.async_setup(mock_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ from .common import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(autouse=True)
|
||||||
def mock_get_state():
|
def mock_get_state():
|
||||||
"""Mock get_state function."""
|
"""Mock get_state function."""
|
||||||
with patch(
|
with patch(
|
||||||
|
@ -23,7 +23,7 @@ def mock_get_state():
|
||||||
yield mock_get_state
|
yield mock_get_state
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(autouse=True)
|
||||||
def mock_get_status():
|
def mock_get_status():
|
||||||
"""Mock get_status function."""
|
"""Mock get_status function."""
|
||||||
with patch(
|
with patch(
|
||||||
|
@ -33,11 +33,11 @@ def mock_get_status():
|
||||||
yield mock_get_status
|
yield mock_get_status
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture(autouse=True)
|
||||||
def mock_get_state_of_all_vehicles():
|
def mock_get_state_of_all_vehicles():
|
||||||
"""Mock get_state_of_all_vehicles function."""
|
"""Mock get_state_of_all_vehicles function."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
"homeassistant.components.tessie.get_state_of_all_vehicles",
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
||||||
) as mock_get_state_of_all_vehicles:
|
) as mock_get_state_of_all_vehicles:
|
||||||
yield mock_get_state_of_all_vehicles
|
yield mock_get_state_of_all_vehicles
|
||||||
|
|
|
@ -6,7 +6,7 @@ import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.tessie.const import DOMAIN
|
from homeassistant.components.tessie.const import DOMAIN
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
|
||||||
|
@ -15,13 +15,37 @@ from .common import (
|
||||||
ERROR_CONNECTION,
|
ERROR_CONNECTION,
|
||||||
ERROR_UNKNOWN,
|
ERROR_UNKNOWN,
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
setup_platform,
|
TEST_STATE_OF_ALL_VEHICLES,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None:
|
@pytest.fixture(autouse=True)
|
||||||
|
def mock_config_flow_get_state_of_all_vehicles():
|
||||||
|
"""Mock get_state_of_all_vehicles in config flow."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
||||||
|
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
||||||
|
) as mock_config_flow_get_state_of_all_vehicles:
|
||||||
|
yield mock_config_flow_get_state_of_all_vehicles
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def mock_async_setup_entry():
|
||||||
|
"""Mock async_setup_entry."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tessie.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
) as mock_async_setup_entry:
|
||||||
|
yield mock_async_setup_entry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_form(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_flow_get_state_of_all_vehicles,
|
||||||
|
mock_async_setup_entry,
|
||||||
|
) -> None:
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
|
|
||||||
result1 = await hass.config_entries.flow.async_init(
|
result1 = await hass.config_entries.flow.async_init(
|
||||||
|
@ -30,17 +54,13 @@ async def test_form(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None
|
||||||
assert result1["type"] is FlowResultType.FORM
|
assert result1["type"] is FlowResultType.FORM
|
||||||
assert not result1["errors"]
|
assert not result1["errors"]
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.tessie.async_setup_entry",
|
result1["flow_id"],
|
||||||
return_value=True,
|
TEST_CONFIG,
|
||||||
) as mock_setup_entry:
|
)
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
await hass.async_block_till_done()
|
||||||
result1["flow_id"],
|
assert len(mock_async_setup_entry.mock_calls) == 1
|
||||||
TEST_CONFIG,
|
assert len(mock_config_flow_get_state_of_all_vehicles.mock_calls) == 1
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
assert len(mock_get_state_of_all_vehicles.mock_calls) == 1
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result2["title"] == "Tessie"
|
assert result2["title"] == "Tessie"
|
||||||
|
@ -56,7 +76,7 @@ async def test_form(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_form_errors(
|
async def test_form_errors(
|
||||||
hass: HomeAssistant, side_effect, error, mock_get_state_of_all_vehicles
|
hass: HomeAssistant, side_effect, error, mock_config_flow_get_state_of_all_vehicles
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test errors are handled."""
|
"""Test errors are handled."""
|
||||||
|
|
||||||
|
@ -64,7 +84,7 @@ async def test_form_errors(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
mock_get_state_of_all_vehicles.side_effect = side_effect
|
mock_config_flow_get_state_of_all_vehicles.side_effect = side_effect
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result1["flow_id"],
|
result1["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
|
@ -74,15 +94,20 @@ async def test_form_errors(
|
||||||
assert result2["errors"] == error
|
assert result2["errors"] == error
|
||||||
|
|
||||||
# Complete the flow
|
# Complete the flow
|
||||||
mock_get_state_of_all_vehicles.side_effect = None
|
mock_config_flow_get_state_of_all_vehicles.side_effect = None
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
result2["flow_id"],
|
result2["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
)
|
)
|
||||||
|
assert "errors" not in result3
|
||||||
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
assert result3["type"] is FlowResultType.CREATE_ENTRY
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None:
|
async def test_reauth(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_config_flow_get_state_of_all_vehicles,
|
||||||
|
mock_async_setup_entry,
|
||||||
|
) -> None:
|
||||||
"""Test reauth flow."""
|
"""Test reauth flow."""
|
||||||
|
|
||||||
mock_entry = MockConfigEntry(
|
mock_entry = MockConfigEntry(
|
||||||
|
@ -104,17 +129,13 @@ async def test_reauth(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> No
|
||||||
assert result1["step_id"] == "reauth_confirm"
|
assert result1["step_id"] == "reauth_confirm"
|
||||||
assert not result1["errors"]
|
assert not result1["errors"]
|
||||||
|
|
||||||
with patch(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
"homeassistant.components.tessie.async_setup_entry",
|
result1["flow_id"],
|
||||||
return_value=True,
|
TEST_CONFIG,
|
||||||
) as mock_setup_entry:
|
)
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
await hass.async_block_till_done()
|
||||||
result1["flow_id"],
|
assert len(mock_async_setup_entry.mock_calls) == 1
|
||||||
TEST_CONFIG,
|
assert len(mock_config_flow_get_state_of_all_vehicles.mock_calls) == 1
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
assert len(mock_setup_entry.mock_calls) == 1
|
|
||||||
assert len(mock_get_state_of_all_vehicles.mock_calls) == 1
|
|
||||||
|
|
||||||
assert result2["type"] is FlowResultType.ABORT
|
assert result2["type"] is FlowResultType.ABORT
|
||||||
assert result2["reason"] == "reauth_successful"
|
assert result2["reason"] == "reauth_successful"
|
||||||
|
@ -130,14 +151,23 @@ async def test_reauth(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> No
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_reauth_errors(
|
async def test_reauth_errors(
|
||||||
hass: HomeAssistant, mock_get_state_of_all_vehicles, side_effect, error
|
hass: HomeAssistant,
|
||||||
|
mock_config_flow_get_state_of_all_vehicles,
|
||||||
|
mock_async_setup_entry,
|
||||||
|
side_effect,
|
||||||
|
error,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test reauth flows that fail."""
|
"""Test reauth flows that fail."""
|
||||||
|
|
||||||
mock_entry = await setup_platform(hass, [Platform.BINARY_SENSOR])
|
mock_config_flow_get_state_of_all_vehicles.side_effect = side_effect
|
||||||
mock_get_state_of_all_vehicles.side_effect = side_effect
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
mock_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data=TEST_CONFIG,
|
||||||
|
)
|
||||||
|
mock_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
result1 = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={
|
context={
|
||||||
"source": config_entries.SOURCE_REAUTH,
|
"source": config_entries.SOURCE_REAUTH,
|
||||||
|
@ -148,7 +178,7 @@ async def test_reauth_errors(
|
||||||
)
|
)
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result1["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -157,7 +187,7 @@ async def test_reauth_errors(
|
||||||
assert result2["errors"] == error
|
assert result2["errors"] == error
|
||||||
|
|
||||||
# Complete the flow
|
# Complete the flow
|
||||||
mock_get_state_of_all_vehicles.side_effect = None
|
mock_config_flow_get_state_of_all_vehicles.side_effect = None
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
result2["flow_id"],
|
result2["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
|
@ -166,3 +196,4 @@ async def test_reauth_errors(
|
||||||
assert result3["type"] is FlowResultType.ABORT
|
assert result3["type"] is FlowResultType.ABORT
|
||||||
assert result3["reason"] == "reauth_successful"
|
assert result3["reason"] == "reauth_successful"
|
||||||
assert mock_entry.data == TEST_CONFIG
|
assert mock_entry.data == TEST_CONFIG
|
||||||
|
assert len(mock_async_setup_entry.mock_calls) == 1
|
||||||
|
|
|
@ -16,22 +16,31 @@ async def test_load_unload(hass: HomeAssistant) -> None:
|
||||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||||
|
|
||||||
|
|
||||||
async def test_auth_failure(hass: HomeAssistant) -> None:
|
async def test_auth_failure(
|
||||||
|
hass: HomeAssistant, mock_get_state_of_all_vehicles
|
||||||
|
) -> None:
|
||||||
"""Test init with an authentication error."""
|
"""Test init with an authentication error."""
|
||||||
|
|
||||||
entry = await setup_platform(hass, side_effect=ERROR_AUTH)
|
mock_get_state_of_all_vehicles.side_effect = ERROR_AUTH
|
||||||
|
entry = await setup_platform(hass)
|
||||||
assert entry.state is ConfigEntryState.SETUP_ERROR
|
assert entry.state is ConfigEntryState.SETUP_ERROR
|
||||||
|
|
||||||
|
|
||||||
async def test_unknown_failure(hass: HomeAssistant) -> None:
|
async def test_unknown_failure(
|
||||||
|
hass: HomeAssistant, mock_get_state_of_all_vehicles
|
||||||
|
) -> None:
|
||||||
"""Test init with an client response error."""
|
"""Test init with an client response error."""
|
||||||
|
|
||||||
entry = await setup_platform(hass, side_effect=ERROR_UNKNOWN)
|
mock_get_state_of_all_vehicles.side_effect = ERROR_UNKNOWN
|
||||||
|
entry = await setup_platform(hass)
|
||||||
assert entry.state is ConfigEntryState.SETUP_ERROR
|
assert entry.state is ConfigEntryState.SETUP_ERROR
|
||||||
|
|
||||||
|
|
||||||
async def test_connection_failure(hass: HomeAssistant) -> None:
|
async def test_connection_failure(
|
||||||
|
hass: HomeAssistant, mock_get_state_of_all_vehicles
|
||||||
|
) -> None:
|
||||||
"""Test init with a network connection error."""
|
"""Test init with a network connection error."""
|
||||||
|
|
||||||
entry = await setup_platform(hass, side_effect=ERROR_CONNECTION)
|
mock_get_state_of_all_vehicles.side_effect = ERROR_CONNECTION
|
||||||
|
entry = await setup_platform(hass)
|
||||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue