From 075a0ad7801a2b49850a30a4218c708828609de4 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 9 Apr 2025 15:17:54 +0200 Subject: [PATCH] Add tests of behavior when completing an aborted data entry flow (#142590) --- tests/test_data_entry_flow.py | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 86ba5257001..994d37dcd65 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -210,6 +210,21 @@ async def test_abort_removes_instance(manager: MockFlowManager) -> None: assert len(manager.mock_created_entries) == 0 +async def test_abort_aborted_flow(manager: MockFlowManager) -> None: + """Test return abort from aborted flow.""" + + @manager.mock_reg_handler("test") + class TestFlow(data_entry_flow.FlowHandler): + async def async_step_init(self, user_input=None): + manager.async_abort(self.flow_id) + return self.async_abort(reason="blah") + + with pytest.raises(data_entry_flow.UnknownFlow): + await manager.async_init("test") + assert len(manager.async_progress()) == 0 + assert len(manager.mock_created_entries) == 0 + + async def test_abort_calls_async_remove(manager: MockFlowManager) -> None: """Test abort calling the async_remove FlowHandler method.""" @@ -272,6 +287,37 @@ async def test_create_saves_data(manager: MockFlowManager) -> None: assert entry["source"] is None +async def test_create_aborted_flow(manager: MockFlowManager) -> None: + """Test return create_entry from aborted flow. + + Note: The entry is created even if the flow is already aborted, then the + flow raises an UnknownFlow exception. This behavior is not logical, and + we should consider changing it to not create the entry if the flow is + aborted. + """ + + @manager.mock_reg_handler("test") + class TestFlow(data_entry_flow.FlowHandler): + VERSION = 5 + + async def async_step_init(self, user_input=None): + manager.async_abort(self.flow_id) + return self.async_create_entry(title="Test Title", data="Test Data") + + with pytest.raises(data_entry_flow.UnknownFlow): + await manager.async_init("test") + assert len(manager.async_progress()) == 0 + + # The entry is created even if the flow is aborted + assert len(manager.mock_created_entries) == 1 + + entry = manager.mock_created_entries[0] + assert entry["handler"] == "test" + assert entry["title"] == "Test Title" + assert entry["data"] == "Test Data" + assert entry["source"] is None + + async def test_discovery_init_flow(manager: MockFlowManager) -> None: """Test a flow initialized by discovery."""