Fix flaky tests for HMIPC (#32806)

pull/32932/head
SukramJ 2020-03-14 19:35:15 +01:00 committed by Franck Nijhof
parent c2f615839d
commit 1e469b39ad
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
4 changed files with 45 additions and 15 deletions

View File

@ -1,5 +1,5 @@
"""Initializer helpers for HomematicIP fake server.""" """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.auth import AsyncAuth
from homematicip.aio.connection import AsyncConnection from homematicip.aio.connection import AsyncConnection
from homematicip.aio.home import AsyncHome from homematicip.aio.home import AsyncHome
@ -106,9 +106,10 @@ async def mock_hap_with_service_fixture(
@pytest.fixture(name="simple_mock_home") @pytest.fixture(name="simple_mock_home")
def simple_mock_home_fixture() -> AsyncHome: def simple_mock_home_fixture():
"""Return a simple AsyncHome Mock.""" """Return a simple mocked connection."""
return Mock(
mock_home = Mock(
spec=AsyncHome, spec=AsyncHome,
name="Demo", name="Demo",
devices=[], devices=[],
@ -120,6 +121,27 @@ def simple_mock_home_fixture() -> AsyncHome:
connected=True, 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") @pytest.fixture(name="simple_mock_auth")
def simple_mock_auth_fixture() -> AsyncAuth: def simple_mock_auth_fixture() -> AsyncAuth:

View File

@ -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"} 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.""" """Test config flow."""
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",
return_value=False, return_value=False,
), patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.get_auth",
return_value=True,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG HMIPC_DOMAIN, context={"source": "user"}, data=DEFAULT_CONFIG
@ -137,7 +140,7 @@ async def test_init_already_configured(hass):
assert result["reason"] == "already_configured" 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.""" """Test importing a host with an existing config file."""
with patch( with patch(
"homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton", "homeassistant.components.homematicip_cloud.hap.HomematicipAuth.async_checkbutton",

View File

@ -125,14 +125,11 @@ async def test_hap_create(hass, hmip_config_entry, simple_mock_home):
hass.config.components.add(HMIPC_DOMAIN) hass.config.components.add(HMIPC_DOMAIN)
hap = HomematicipHAP(hass, hmip_config_entry) hap = HomematicipHAP(hass, hmip_config_entry)
assert hap assert hap
with patch( with patch.object(hap, "async_connect"):
"homeassistant.components.homematicip_cloud.hap.AsyncHome",
return_value=simple_mock_home,
), patch.object(hap, "async_connect"):
assert await hap.async_setup() 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.""" """Mock AsyncHome to execute get_hap."""
hass.config.components.add(HMIPC_DOMAIN) hass.config.components.add(HMIPC_DOMAIN)

View File

@ -24,7 +24,9 @@ from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry 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.""" """Test that config for a accesspoint are loaded via config entry."""
entry_config = { 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) 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.""" """Test that an already registered accesspoint does not get imported."""
mock_config = {HMIPC_AUTHTOKEN: "123", HMIPC_HAPID: "ABC123", HMIPC_NAME: "name"} 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" 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.""" """Test load entry fails due to connection error."""
hmip_config_entry.add_to_hass(hass) 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 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.""" """Test load entry fails due to generic exception."""
hmip_config_entry.add_to_hass(hass) hmip_config_entry.add_to_hass(hass)