From 1e469b39ad159b9a34210c800322861c48eeee4c Mon Sep 17 00:00:00 2001 From: SukramJ Date: Sat, 14 Mar 2020 19:35:15 +0100 Subject: [PATCH] Fix flaky tests for HMIPC (#32806) --- .../components/homematicip_cloud/conftest.py | 30 ++++++++++++++++--- .../homematicip_cloud/test_config_flow.py | 7 +++-- .../components/homematicip_cloud/test_hap.py | 7 ++--- .../components/homematicip_cloud/test_init.py | 16 +++++++--- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/tests/components/homematicip_cloud/conftest.py b/tests/components/homematicip_cloud/conftest.py index 502e9d1b73e..927690d881f 100644 --- a/tests/components/homematicip_cloud/conftest.py +++ b/tests/components/homematicip_cloud/conftest.py @@ -1,5 +1,5 @@ """Initializer helpers for HomematicIP fake server.""" -from asynctest import CoroutineMock, MagicMock, Mock +from asynctest import CoroutineMock, MagicMock, Mock, patch from homematicip.aio.auth import AsyncAuth from homematicip.aio.connection import AsyncConnection from homematicip.aio.home import AsyncHome @@ -106,9 +106,10 @@ async def mock_hap_with_service_fixture( @pytest.fixture(name="simple_mock_home") -def simple_mock_home_fixture() -> AsyncHome: - """Return a simple AsyncHome Mock.""" - return Mock( +def simple_mock_home_fixture(): + """Return a simple mocked connection.""" + + mock_home = Mock( spec=AsyncHome, name="Demo", devices=[], @@ -120,6 +121,27 @@ def simple_mock_home_fixture() -> AsyncHome: connected=True, ) + with patch( + "homeassistant.components.homematicip_cloud.hap.AsyncHome", + autospec=True, + return_value=mock_home, + ): + yield + + +@pytest.fixture(name="mock_connection_init") +def mock_connection_init_fixture(): + """Return a simple mocked connection.""" + + with patch( + "homeassistant.components.homematicip_cloud.hap.AsyncHome.init", + return_value=None, + ), patch( + "homeassistant.components.homematicip_cloud.hap.AsyncAuth.init", + return_value=None, + ): + yield + @pytest.fixture(name="simple_mock_auth") def simple_mock_auth_fixture() -> AsyncAuth: diff --git a/tests/components/homematicip_cloud/test_config_flow.py b/tests/components/homematicip_cloud/test_config_flow.py index 01e820e7565..6436433a147 100644 --- a/tests/components/homematicip_cloud/test_config_flow.py +++ b/tests/components/homematicip_cloud/test_config_flow.py @@ -16,12 +16,15 @@ DEFAULT_CONFIG = {HMIPC_HAPID: "ABC123", HMIPC_PIN: "123", HMIPC_NAME: "hmip"} IMPORT_CONFIG = {HMIPC_HAPID: "ABC123", HMIPC_AUTHTOKEN: "123", HMIPC_NAME: "hmip"} -async def test_flow_works(hass): +async def test_flow_works(hass, simple_mock_home): """Test config flow.""" with patch( "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", return_value=False, + ), patch( + "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.get_auth", + return_value=True, ): result = await hass.config_entries.flow.async_init( HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG @@ -137,7 +140,7 @@ async def test_init_already_configured(hass): assert result["reason"] == "already_configured" -async def test_import_config(hass): +async def test_import_config(hass, simple_mock_home): """Test importing a host with an existing config file.""" with patch( "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", diff --git a/tests/components/homematicip_cloud/test_hap.py b/tests/components/homematicip_cloud/test_hap.py index 1dd5b2fc789..e6e143973f3 100644 --- a/tests/components/homematicip_cloud/test_hap.py +++ b/tests/components/homematicip_cloud/test_hap.py @@ -125,14 +125,11 @@ async def test_hap_create(hass, hmip_config_entry, simple_mock_home): hass.config.components.add(HMIPC_DOMAIN) hap = HomematicipHAP(hass, hmip_config_entry) assert hap - with patch( - "homeassistant.components.homematicip_cloud.hap.AsyncHome", - return_value=simple_mock_home, - ), patch.object(hap, "async_connect"): + with patch.object(hap, "async_connect"): assert await hap.async_setup() -async def test_hap_create_exception(hass, hmip_config_entry): +async def test_hap_create_exception(hass, hmip_config_entry, mock_connection_init): """Mock AsyncHome to execute get_hap.""" hass.config.components.add(HMIPC_DOMAIN) diff --git a/tests/components/homematicip_cloud/test_init.py b/tests/components/homematicip_cloud/test_init.py index ef7f5fa24ae..f97e7114b94 100644 --- a/tests/components/homematicip_cloud/test_init.py +++ b/tests/components/homematicip_cloud/test_init.py @@ -24,7 +24,9 @@ from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry -async def test_config_with_accesspoint_passed_to_config_entry(hass): +async def test_config_with_accesspoint_passed_to_config_entry( + hass, mock_connection, simple_mock_home +): """Test that config for a accesspoint are loaded via config entry.""" entry_config = { @@ -51,7 +53,9 @@ async def test_config_with_accesspoint_passed_to_config_entry(hass): assert isinstance(hass.data[HMIPC_DOMAIN]["ABC123"], HomematicipHAP) -async def test_config_already_registered_not_passed_to_config_entry(hass): +async def test_config_already_registered_not_passed_to_config_entry( + hass, simple_mock_home +): """Test that an already registered accesspoint does not get imported.""" mock_config = {HMIPC_AUTHTOKEN: "123", HMIPC_HAPID: "ABC123", HMIPC_NAME: "name"} @@ -87,7 +91,9 @@ async def test_config_already_registered_not_passed_to_config_entry(hass): assert config_entries[0].unique_id == "ABC123" -async def test_load_entry_fails_due_to_connection_error(hass, hmip_config_entry): +async def test_load_entry_fails_due_to_connection_error( + hass, hmip_config_entry, mock_connection_init +): """Test load entry fails due to connection error.""" hmip_config_entry.add_to_hass(hass) @@ -101,7 +107,9 @@ async def test_load_entry_fails_due_to_connection_error(hass, hmip_config_entry) assert hmip_config_entry.state == ENTRY_STATE_SETUP_RETRY -async def test_load_entry_fails_due_to_generic_exception(hass, hmip_config_entry): +async def test_load_entry_fails_due_to_generic_exception( + hass, hmip_config_entry, simple_mock_home +): """Test load entry fails due to generic exception.""" hmip_config_entry.add_to_hass(hass)