Fix zeroconf mock and use it in CI group 1's tests (#55526)

* Fix zeroconf mock and use it in CI group 1's tests

* Mock HaAsyncServiceBrowser
This commit is contained in:
Erik Montnemery 2021-09-01 22:38:00 +02:00 committed by GitHub
parent e631671832
commit 7dbd0e5274
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 30 deletions

View file

@ -28,7 +28,7 @@ DISCOVERY_INFO = {
} }
async def test_form_user(hass): async def test_form_user(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -92,7 +92,7 @@ async def test_form_user(hass):
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_form_get_info_connection_error(hass): async def test_form_get_info_connection_error(hass, mock_zeroconf):
"""Test we handle connection error.""" """Test we handle connection error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -136,7 +136,7 @@ async def test_form_get_info_exception(hass):
assert result2["errors"] == {"base": "unknown"} assert result2["errors"] == {"base": "unknown"}
async def test_form_pairing_error(hass): async def test_form_pairing_error(hass, mock_zeroconf):
"""Test we handle pairing error.""" """Test we handle pairing error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -178,7 +178,7 @@ async def test_form_pairing_error(hass):
assert result3["errors"] == {"base": "pairing_failed"} assert result3["errors"] == {"base": "pairing_failed"}
async def test_form_user_invalid_auth(hass): async def test_form_user_invalid_auth(hass, mock_zeroconf):
"""Test we handle invalid auth.""" """Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -227,7 +227,7 @@ async def test_form_user_invalid_auth(hass):
assert result3["errors"] == {"base": "invalid_auth"} assert result3["errors"] == {"base": "invalid_auth"}
async def test_form_validate_connection_error(hass): async def test_form_validate_connection_error(hass, mock_zeroconf):
"""Test we handle connection error.""" """Test we handle connection error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -276,7 +276,7 @@ async def test_form_validate_connection_error(hass):
assert result3["errors"] == {"base": "cannot_connect"} assert result3["errors"] == {"base": "cannot_connect"}
async def test_form_validate_session_error(hass): async def test_form_validate_session_error(hass, mock_zeroconf):
"""Test we handle session error.""" """Test we handle session error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -325,7 +325,7 @@ async def test_form_validate_session_error(hass):
assert result3["errors"] == {"base": "session_error"} assert result3["errors"] == {"base": "session_error"}
async def test_form_validate_exception(hass): async def test_form_validate_exception(hass, mock_zeroconf):
"""Test we handle exception.""" """Test we handle exception."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -374,7 +374,7 @@ async def test_form_validate_exception(hass):
assert result3["errors"] == {"base": "unknown"} assert result3["errors"] == {"base": "unknown"}
async def test_form_already_configured(hass): async def test_form_already_configured(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry( entry = MockConfigEntry(
@ -410,7 +410,7 @@ async def test_form_already_configured(hass):
assert entry.data["host"] == "1.1.1.1" assert entry.data["host"] == "1.1.1.1"
async def test_zeroconf(hass): async def test_zeroconf(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
@ -479,7 +479,7 @@ async def test_zeroconf(hass):
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_zeroconf_already_configured(hass): async def test_zeroconf_already_configured(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
entry = MockConfigEntry( entry = MockConfigEntry(
@ -512,7 +512,7 @@ async def test_zeroconf_already_configured(hass):
assert entry.data["host"] == "1.1.1.1" assert entry.data["host"] == "1.1.1.1"
async def test_zeroconf_cannot_connect(hass): async def test_zeroconf_cannot_connect(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
with patch( with patch(
"boschshcpy.session.SHCSession.mdns_info", side_effect=SHCConnectionError "boschshcpy.session.SHCSession.mdns_info", side_effect=SHCConnectionError
@ -526,7 +526,7 @@ async def test_zeroconf_cannot_connect(hass):
assert result["reason"] == "cannot_connect" assert result["reason"] == "cannot_connect"
async def test_zeroconf_not_bosch_shc(hass): async def test_zeroconf_not_bosch_shc(hass, mock_zeroconf):
"""Test we filter out non-bosch_shc devices.""" """Test we filter out non-bosch_shc devices."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -537,7 +537,7 @@ async def test_zeroconf_not_bosch_shc(hass):
assert result["reason"] == "not_bosch_shc" assert result["reason"] == "not_bosch_shc"
async def test_reauth(hass): async def test_reauth(hass, mock_zeroconf):
"""Test we get the form.""" """Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {}) await setup.async_setup_component(hass, "persistent_notification", {})
mock_config = MockConfigEntry( mock_config = MockConfigEntry(

View file

@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant
from tests.components.devolo_home_control import configure_integration from tests.components.devolo_home_control import configure_integration
async def test_setup_entry(hass: HomeAssistant): async def test_setup_entry(hass: HomeAssistant, mock_zeroconf):
"""Test setup entry.""" """Test setup entry."""
entry = configure_integration(hass) entry = configure_integration(hass)
with patch("homeassistant.components.devolo_home_control.HomeControl"): with patch("homeassistant.components.devolo_home_control.HomeControl"):
@ -34,7 +34,7 @@ async def test_setup_entry_maintenance(hass: HomeAssistant):
assert entry.state is ConfigEntryState.SETUP_RETRY assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_setup_gateway_offline(hass: HomeAssistant): async def test_setup_gateway_offline(hass: HomeAssistant, mock_zeroconf):
"""Test setup entry fails on gateway offline.""" """Test setup entry fails on gateway offline."""
entry = configure_integration(hass) entry = configure_integration(hass)
with patch( with patch(

View file

@ -55,7 +55,7 @@ def mock_setup_entry():
yield yield
async def test_user_connection_works(hass, mock_client): async def test_user_connection_works(hass, mock_client, mock_zeroconf):
"""Test we can finish a config flow.""" """Test we can finish a config flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"esphome", "esphome",
@ -86,7 +86,9 @@ async def test_user_connection_works(hass, mock_client):
assert mock_client.password == "" assert mock_client.password == ""
async def test_user_resolve_error(hass, mock_api_connection_error, mock_client): async def test_user_resolve_error(
hass, mock_api_connection_error, mock_client, mock_zeroconf
):
"""Test user step with IP resolve error.""" """Test user step with IP resolve error."""
class MockResolveError(mock_api_connection_error): class MockResolveError(mock_api_connection_error):
@ -116,7 +118,9 @@ async def test_user_resolve_error(hass, mock_api_connection_error, mock_client):
assert len(mock_client.disconnect.mock_calls) == 1 assert len(mock_client.disconnect.mock_calls) == 1
async def test_user_connection_error(hass, mock_api_connection_error, mock_client): async def test_user_connection_error(
hass, mock_api_connection_error, mock_client, mock_zeroconf
):
"""Test user step with connection error.""" """Test user step with connection error."""
mock_client.device_info.side_effect = mock_api_connection_error mock_client.device_info.side_effect = mock_api_connection_error
@ -135,7 +139,7 @@ async def test_user_connection_error(hass, mock_api_connection_error, mock_clien
assert len(mock_client.disconnect.mock_calls) == 1 assert len(mock_client.disconnect.mock_calls) == 1
async def test_user_with_password(hass, mock_client): async def test_user_with_password(hass, mock_client, mock_zeroconf):
"""Test user step with password.""" """Test user step with password."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test")) mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
@ -161,7 +165,9 @@ async def test_user_with_password(hass, mock_client):
assert mock_client.password == "password1" assert mock_client.password == "password1"
async def test_user_invalid_password(hass, mock_api_connection_error, mock_client): async def test_user_invalid_password(
hass, mock_api_connection_error, mock_client, mock_zeroconf
):
"""Test user step with invalid password.""" """Test user step with invalid password."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test")) mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(True, "test"))
@ -185,7 +191,7 @@ async def test_user_invalid_password(hass, mock_api_connection_error, mock_clien
assert result["errors"] == {"base": "invalid_auth"} assert result["errors"] == {"base": "invalid_auth"}
async def test_discovery_initiation(hass, mock_client): async def test_discovery_initiation(hass, mock_client, mock_zeroconf):
"""Test discovery importing works.""" """Test discovery importing works."""
mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266")) mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266"))

View file

@ -11,13 +11,6 @@ import homeassistant.util.dt as dt_util
from tests.components.light.conftest import mock_light_profiles # noqa: F401 from tests.components.light.conftest import mock_light_profiles # noqa: F401
@pytest.fixture(autouse=True)
def mock_zeroconf():
"""Mock zeroconf."""
with mock.patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc:
yield mock_zc.return_value
@pytest.fixture @pytest.fixture
def utcnow(request): def utcnow(request):
"""Freeze time at a known point.""" """Freeze time at a known point."""

View file

@ -480,8 +480,10 @@ async def mqtt_mock(hass, mqtt_client_mock, mqtt_config):
@pytest.fixture @pytest.fixture
def mock_zeroconf(): def mock_zeroconf():
"""Mock zeroconf.""" """Mock zeroconf."""
with patch("homeassistant.components.zeroconf.models.HaZeroconf") as mock_zc: with patch("homeassistant.components.zeroconf.HaZeroconf", autospec=True), patch(
yield mock_zc.return_value "homeassistant.components.zeroconf.HaAsyncServiceBrowser", autospec=True
):
yield
@pytest.fixture @pytest.fixture