From 8220da7263e346a37c3e1a219dc374c4a43c7df5 Mon Sep 17 00:00:00 2001 From: "Michael \"Chishm\" Chisholm" Date: Tue, 5 Oct 2021 10:31:15 +1100 Subject: [PATCH] Consolidate config entry test cleanup into a single function --- tests/components/dlna_dmr/test_config_flow.py | 82 +++++++------------ 1 file changed, 29 insertions(+), 53 deletions(-) diff --git a/tests/components/dlna_dmr/test_config_flow.py b/tests/components/dlna_dmr/test_config_flow.py index f5699fc7fb0..361d23c6df1 100644 --- a/tests/components/dlna_dmr/test_config_flow.py +++ b/tests/components/dlna_dmr/test_config_flow.py @@ -61,6 +61,27 @@ MOCK_DISCOVERY = { } +async def cleanup_entry( + hass: HomeAssistant, entry: str | data_entry_flow.FlowResult +) -> None: + """Clean up a config entry to ensure all resources acquired are released. + + This works in conjunction with the post-conditions in domain_data_mock and + dmr_device_mock to find any resource leaks. + """ + # Wait for platform to finish setup + await hass.async_block_till_done() + + # Remove the config entry and device to clean up all resources + if isinstance(entry, dict): + entry_id: str = entry["result"].entry_id + else: + entry_id = entry + assert await hass.config_entries.async_remove(entry_id) == { + "require_restart": False + } + + async def test_user_flow(hass: HomeAssistant) -> None: """Test user-init'd config flow with user entering a valid URL.""" result = await hass.config_entries.flow.async_init( @@ -83,14 +104,7 @@ async def test_user_flow(hass: HomeAssistant) -> None: } assert result["options"] == {CONF_POLL_AVAILABILITY: True} - # Wait for platform to be fully setup - await hass.async_block_till_done() - - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_user_flow_uncontactable( @@ -151,14 +165,7 @@ async def test_user_flow_embedded_st( } assert result["options"] == {CONF_POLL_AVAILABILITY: True} - # Wait for platform to be fully setup - await hass.async_block_till_done() - - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_user_flow_wrong_st(hass: HomeAssistant, domain_data_mock: Mock) -> None: @@ -240,13 +247,7 @@ async def test_import_flow_ssdp_discovered( assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - # Wait for platform to be fully setup - await hass.async_block_till_done() - - # Remove the device to clean up all resources, completing its life cycle - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, entry_id) async def test_import_flow_direct_connect( @@ -286,10 +287,7 @@ async def test_import_flow_direct_connect( assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured" - # Remove the device to clean up all resources, completing its life cycle - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, entry_id) async def test_import_flow_offline( @@ -361,14 +359,7 @@ async def test_import_flow_offline( CONF_POLL_AVAILABILITY: True, } - # Wait for platform to be fully setup - await hass.async_block_till_done() - - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_import_flow_options( @@ -403,14 +394,7 @@ async def test_import_flow_options( CONF_POLL_AVAILABILITY: True, } - # Wait for platform to be fully setup - await hass.async_block_till_done() - - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_ssdp_flow_success(hass: HomeAssistant) -> None: @@ -437,11 +421,7 @@ async def test_ssdp_flow_success(hass: HomeAssistant) -> None: } assert result["options"] == {} - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_ssdp_flow_unavailable( @@ -476,11 +456,7 @@ async def test_ssdp_flow_unavailable( } assert result["options"] == {} - # Remove the device to clean up all resources, completing its life cycle - entry_id = result["result"].entry_id - assert await hass.config_entries.async_remove(entry_id) == { - "require_restart": False - } + await cleanup_entry(hass, result) async def test_ssdp_flow_existing(