mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Improve type hints in data_entry_flow tests (#119877)
This commit is contained in:
parent
6b27e9a745
commit
041746a50b
@ -19,18 +19,19 @@ from .common import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
class MockFlowManager(data_entry_flow.FlowManager):
|
||||||
def manager():
|
|
||||||
"""Return a flow manager."""
|
|
||||||
handlers = Registry()
|
|
||||||
entries = []
|
|
||||||
|
|
||||||
class FlowManager(data_entry_flow.FlowManager):
|
|
||||||
"""Test flow manager."""
|
"""Test flow manager."""
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
|
"""Initialize the flow manager."""
|
||||||
|
super().__init__(None)
|
||||||
|
self._handlers = Registry()
|
||||||
|
self.mock_reg_handler = self._handlers.register
|
||||||
|
self.mock_created_entries = []
|
||||||
|
|
||||||
async def async_create_flow(self, handler_key, *, context, data):
|
async def async_create_flow(self, handler_key, *, context, data):
|
||||||
"""Test create flow."""
|
"""Test create flow."""
|
||||||
handler = handlers.get(handler_key)
|
handler = self._handlers.get(handler_key)
|
||||||
|
|
||||||
if handler is None:
|
if handler is None:
|
||||||
raise data_entry_flow.UnknownHandler
|
raise data_entry_flow.UnknownHandler
|
||||||
@ -43,18 +44,17 @@ def manager():
|
|||||||
"""Test finish flow."""
|
"""Test finish flow."""
|
||||||
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
|
if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
|
||||||
result["source"] = flow.context.get("source")
|
result["source"] = flow.context.get("source")
|
||||||
entries.append(result)
|
self.mock_created_entries.append(result)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
mgr = FlowManager(None)
|
|
||||||
# pylint: disable-next=attribute-defined-outside-init
|
@pytest.fixture
|
||||||
mgr.mock_created_entries = entries
|
def manager() -> MockFlowManager:
|
||||||
# pylint: disable-next=attribute-defined-outside-init
|
"""Return a flow manager."""
|
||||||
mgr.mock_reg_handler = handlers.register
|
return MockFlowManager()
|
||||||
return mgr
|
|
||||||
|
|
||||||
|
|
||||||
async def test_configure_reuses_handler_instance(manager) -> None:
|
async def test_configure_reuses_handler_instance(manager: MockFlowManager) -> None:
|
||||||
"""Test that we reuse instances."""
|
"""Test that we reuse instances."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -82,7 +82,7 @@ async def test_configure_reuses_handler_instance(manager) -> None:
|
|||||||
assert len(manager.mock_created_entries) == 0
|
assert len(manager.mock_created_entries) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_configure_two_steps(manager: data_entry_flow.FlowManager) -> None:
|
async def test_configure_two_steps(manager: MockFlowManager) -> None:
|
||||||
"""Test that we reuse instances."""
|
"""Test that we reuse instances."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -117,7 +117,7 @@ async def test_configure_two_steps(manager: data_entry_flow.FlowManager) -> None
|
|||||||
assert result["data"] == ["INIT-DATA", "SECOND-DATA"]
|
assert result["data"] == ["INIT-DATA", "SECOND-DATA"]
|
||||||
|
|
||||||
|
|
||||||
async def test_show_form(manager) -> None:
|
async def test_show_form(manager: MockFlowManager) -> None:
|
||||||
"""Test that we can show a form."""
|
"""Test that we can show a form."""
|
||||||
schema = vol.Schema({vol.Required("username"): str, vol.Required("password"): str})
|
schema = vol.Schema({vol.Required("username"): str, vol.Required("password"): str})
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ async def test_show_form(manager) -> None:
|
|||||||
assert form["errors"] == {"username": "Should be unique."}
|
assert form["errors"] == {"username": "Should be unique."}
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_removes_instance(manager) -> None:
|
async def test_abort_removes_instance(manager: MockFlowManager) -> None:
|
||||||
"""Test that abort removes the flow from progress."""
|
"""Test that abort removes the flow from progress."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -158,7 +158,7 @@ async def test_abort_removes_instance(manager) -> None:
|
|||||||
assert len(manager.mock_created_entries) == 0
|
assert len(manager.mock_created_entries) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_calls_async_remove(manager) -> None:
|
async def test_abort_calls_async_remove(manager: MockFlowManager) -> None:
|
||||||
"""Test abort calling the async_remove FlowHandler method."""
|
"""Test abort calling the async_remove FlowHandler method."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -177,7 +177,7 @@ async def test_abort_calls_async_remove(manager) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def test_abort_calls_async_remove_with_exception(
|
async def test_abort_calls_async_remove_with_exception(
|
||||||
manager, caplog: pytest.LogCaptureFixture
|
manager: MockFlowManager, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test abort calling the async_remove FlowHandler method, with an exception."""
|
"""Test abort calling the async_remove FlowHandler method, with an exception."""
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ async def test_abort_calls_async_remove_with_exception(
|
|||||||
assert len(manager.mock_created_entries) == 0
|
assert len(manager.mock_created_entries) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_create_saves_data(manager) -> None:
|
async def test_create_saves_data(manager: MockFlowManager) -> None:
|
||||||
"""Test creating a config entry."""
|
"""Test creating a config entry."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -220,7 +220,7 @@ async def test_create_saves_data(manager) -> None:
|
|||||||
assert entry["source"] is None
|
assert entry["source"] is None
|
||||||
|
|
||||||
|
|
||||||
async def test_discovery_init_flow(manager) -> None:
|
async def test_discovery_init_flow(manager: MockFlowManager) -> None:
|
||||||
"""Test a flow initialized by discovery."""
|
"""Test a flow initialized by discovery."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -290,7 +290,7 @@ async def test_finish_callback_change_result_type(hass: HomeAssistant) -> None:
|
|||||||
assert result["result"] == 2
|
assert result["result"] == 2
|
||||||
|
|
||||||
|
|
||||||
async def test_external_step(hass: HomeAssistant, manager) -> None:
|
async def test_external_step(hass: HomeAssistant, manager: MockFlowManager) -> None:
|
||||||
"""Test external step logic."""
|
"""Test external step logic."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
|
|
||||||
@ -340,7 +340,7 @@ async def test_external_step(hass: HomeAssistant, manager) -> None:
|
|||||||
assert result["title"] == "Hello"
|
assert result["title"] == "Hello"
|
||||||
|
|
||||||
|
|
||||||
async def test_show_progress(hass: HomeAssistant, manager) -> None:
|
async def test_show_progress(hass: HomeAssistant, manager: MockFlowManager) -> None:
|
||||||
"""Test show progress logic."""
|
"""Test show progress logic."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
events = []
|
events = []
|
||||||
@ -443,7 +443,9 @@ async def test_show_progress(hass: HomeAssistant, manager) -> None:
|
|||||||
assert result["title"] == "Hello"
|
assert result["title"] == "Hello"
|
||||||
|
|
||||||
|
|
||||||
async def test_show_progress_error(hass: HomeAssistant, manager) -> None:
|
async def test_show_progress_error(
|
||||||
|
hass: HomeAssistant, manager: MockFlowManager
|
||||||
|
) -> None:
|
||||||
"""Test show progress logic."""
|
"""Test show progress logic."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
events = []
|
events = []
|
||||||
@ -506,7 +508,9 @@ async def test_show_progress_error(hass: HomeAssistant, manager) -> None:
|
|||||||
assert result["reason"] == "error"
|
assert result["reason"] == "error"
|
||||||
|
|
||||||
|
|
||||||
async def test_show_progress_hidden_from_frontend(hass: HomeAssistant, manager) -> None:
|
async def test_show_progress_hidden_from_frontend(
|
||||||
|
hass: HomeAssistant, manager: MockFlowManager
|
||||||
|
) -> None:
|
||||||
"""Test show progress done is not sent to frontend."""
|
"""Test show progress done is not sent to frontend."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
async_show_progress_done_called = False
|
async_show_progress_done_called = False
|
||||||
@ -557,7 +561,7 @@ async def test_show_progress_hidden_from_frontend(hass: HomeAssistant, manager)
|
|||||||
|
|
||||||
|
|
||||||
async def test_show_progress_legacy(
|
async def test_show_progress_legacy(
|
||||||
hass: HomeAssistant, manager, caplog: pytest.LogCaptureFixture
|
hass: HomeAssistant, manager: MockFlowManager, caplog: pytest.LogCaptureFixture
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test show progress logic.
|
"""Test show progress logic.
|
||||||
|
|
||||||
@ -659,7 +663,7 @@ async def test_show_progress_legacy(
|
|||||||
|
|
||||||
|
|
||||||
async def test_show_progress_fires_only_when_changed(
|
async def test_show_progress_fires_only_when_changed(
|
||||||
hass: HomeAssistant, manager
|
hass: HomeAssistant, manager: MockFlowManager
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test show progress change logic."""
|
"""Test show progress change logic."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
@ -745,7 +749,7 @@ async def test_show_progress_fires_only_when_changed(
|
|||||||
) # change (description placeholder)
|
) # change (description placeholder)
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_flow_exception(manager) -> None:
|
async def test_abort_flow_exception(manager: MockFlowManager) -> None:
|
||||||
"""Test that the AbortFlow exception works."""
|
"""Test that the AbortFlow exception works."""
|
||||||
|
|
||||||
@manager.mock_reg_handler("test")
|
@manager.mock_reg_handler("test")
|
||||||
@ -759,7 +763,7 @@ async def test_abort_flow_exception(manager) -> None:
|
|||||||
assert form["description_placeholders"] == {"placeholder": "yo"}
|
assert form["description_placeholders"] == {"placeholder": "yo"}
|
||||||
|
|
||||||
|
|
||||||
async def test_init_unknown_flow(manager) -> None:
|
async def test_init_unknown_flow(manager: MockFlowManager) -> None:
|
||||||
"""Test that UnknownFlow is raised when async_create_flow returns None."""
|
"""Test that UnknownFlow is raised when async_create_flow returns None."""
|
||||||
|
|
||||||
with (
|
with (
|
||||||
@ -769,7 +773,7 @@ async def test_init_unknown_flow(manager) -> None:
|
|||||||
await manager.async_init("test")
|
await manager.async_init("test")
|
||||||
|
|
||||||
|
|
||||||
async def test_async_get_unknown_flow(manager) -> None:
|
async def test_async_get_unknown_flow(manager: MockFlowManager) -> None:
|
||||||
"""Test that UnknownFlow is raised when async_get is called with a flow_id that does not exist."""
|
"""Test that UnknownFlow is raised when async_get is called with a flow_id that does not exist."""
|
||||||
|
|
||||||
with pytest.raises(data_entry_flow.UnknownFlow):
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
||||||
@ -777,7 +781,7 @@ async def test_async_get_unknown_flow(manager) -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def test_async_has_matching_flow(
|
async def test_async_has_matching_flow(
|
||||||
hass: HomeAssistant, manager: data_entry_flow.FlowManager
|
hass: HomeAssistant, manager: MockFlowManager
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we can check for matching flows."""
|
"""Test we can check for matching flows."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
@ -854,7 +858,7 @@ async def test_async_has_matching_flow(
|
|||||||
|
|
||||||
|
|
||||||
async def test_move_to_unknown_step_raises_and_removes_from_in_progress(
|
async def test_move_to_unknown_step_raises_and_removes_from_in_progress(
|
||||||
manager,
|
manager: MockFlowManager,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
||||||
|
|
||||||
@ -880,7 +884,7 @@ async def test_move_to_unknown_step_raises_and_removes_from_in_progress(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_next_step_unknown_step_raises_and_removes_from_in_progress(
|
async def test_next_step_unknown_step_raises_and_removes_from_in_progress(
|
||||||
manager, result_type: str, params: dict[str, str]
|
manager: MockFlowManager, result_type: str, params: dict[str, str]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
"""Test that moving to an unknown step raises and removes the flow from in progress."""
|
||||||
|
|
||||||
@ -897,13 +901,17 @@ async def test_next_step_unknown_step_raises_and_removes_from_in_progress(
|
|||||||
assert manager.async_progress() == []
|
assert manager.async_progress() == []
|
||||||
|
|
||||||
|
|
||||||
async def test_configure_raises_unknown_flow_if_not_in_progress(manager) -> None:
|
async def test_configure_raises_unknown_flow_if_not_in_progress(
|
||||||
|
manager: MockFlowManager,
|
||||||
|
) -> None:
|
||||||
"""Test configure raises UnknownFlow if the flow is not in progress."""
|
"""Test configure raises UnknownFlow if the flow is not in progress."""
|
||||||
with pytest.raises(data_entry_flow.UnknownFlow):
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
||||||
await manager.async_configure("wrong_flow_id")
|
await manager.async_configure("wrong_flow_id")
|
||||||
|
|
||||||
|
|
||||||
async def test_abort_raises_unknown_flow_if_not_in_progress(manager) -> None:
|
async def test_abort_raises_unknown_flow_if_not_in_progress(
|
||||||
|
manager: MockFlowManager,
|
||||||
|
) -> None:
|
||||||
"""Test abort raises UnknownFlow if the flow is not in progress."""
|
"""Test abort raises UnknownFlow if the flow is not in progress."""
|
||||||
with pytest.raises(data_entry_flow.UnknownFlow):
|
with pytest.raises(data_entry_flow.UnknownFlow):
|
||||||
await manager.async_abort("wrong_flow_id")
|
await manager.async_abort("wrong_flow_id")
|
||||||
@ -913,7 +921,11 @@ async def test_abort_raises_unknown_flow_if_not_in_progress(manager) -> None:
|
|||||||
"menu_options",
|
"menu_options",
|
||||||
[["target1", "target2"], {"target1": "Target 1", "target2": "Target 2"}],
|
[["target1", "target2"], {"target1": "Target 1", "target2": "Target 2"}],
|
||||||
)
|
)
|
||||||
async def test_show_menu(hass: HomeAssistant, manager, menu_options) -> None:
|
async def test_show_menu(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
manager: MockFlowManager,
|
||||||
|
menu_options: list[str] | dict[str, str],
|
||||||
|
) -> None:
|
||||||
"""Test show menu."""
|
"""Test show menu."""
|
||||||
manager.hass = hass
|
manager.hass = hass
|
||||||
|
|
||||||
@ -952,9 +964,7 @@ async def test_show_menu(hass: HomeAssistant, manager, menu_options) -> None:
|
|||||||
assert result["step_id"] == "target1"
|
assert result["step_id"] == "target1"
|
||||||
|
|
||||||
|
|
||||||
async def test_find_flows_by_init_data_type(
|
async def test_find_flows_by_init_data_type(manager: MockFlowManager) -> None:
|
||||||
manager: data_entry_flow.FlowManager,
|
|
||||||
) -> None:
|
|
||||||
"""Test we can find flows by init data type."""
|
"""Test we can find flows by init data type."""
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user