Use mock_setup_entry fixture in melnor (#89226)

This commit is contained in:
epenet 2023-03-06 12:28:40 +01:00 committed by GitHub
parent b572ecc62d
commit 14a17b1028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 48 deletions

View File

@ -1,6 +1,7 @@
"""Tests for the melnor integration.""" """Tests for the melnor integration."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Generator
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from bleak.backends.device import BLEDevice from bleak.backends.device import BLEDevice
@ -141,12 +142,13 @@ def mock_melnor_device():
return device return device
def patch_async_setup_entry(return_value=True): @pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Patch async setup entry to return True.""" """Patch async setup entry to return True."""
return patch( with patch(
"homeassistant.components.melnor.async_setup_entry", "homeassistant.components.melnor.async_setup_entry", return_value=True
return_value=return_value, ) as mock_setup:
) yield mock_setup
# pylint: disable=dangerous-default-value # pylint: disable=dangerous-default-value

View File

@ -1,4 +1,6 @@
"""Test the melnor config flow.""" """Test the melnor config flow."""
from unittest.mock import AsyncMock
import pytest import pytest
import voluptuous as vol import voluptuous as vol
@ -13,15 +15,14 @@ from .conftest import (
FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_1,
FAKE_SERVICE_INFO_2, FAKE_SERVICE_INFO_2,
patch_async_discovered_service_info, patch_async_discovered_service_info,
patch_async_setup_entry,
) )
async def test_user_step_no_devices(hass: HomeAssistant) -> None: async def test_user_step_no_devices(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we handle no devices found.""" """Test we handle no devices found."""
with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info( with patch_async_discovered_service_info([]):
[]
):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_USER}, context={"source": config_entries.SOURCE_USER},
@ -30,13 +31,15 @@ async def test_user_step_no_devices(hass: HomeAssistant) -> None:
assert result["type"] == FlowResultType.ABORT assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "no_devices_found" assert result["reason"] == "no_devices_found"
assert len(mock_setup_entry.mock_calls) == 0 mock_setup_entry.assert_not_called()
async def test_user_step_discovered_devices(hass: HomeAssistant) -> None: async def test_user_step_discovered_devices(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we properly handle device picking.""" """Test we properly handle device picking."""
with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info(): with patch_async_discovered_service_info():
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_USER}, context={"source": config_entries.SOURCE_USER},
@ -57,13 +60,15 @@ async def test_user_step_discovered_devices(hass: HomeAssistant) -> None:
assert result2["type"] == FlowResultType.CREATE_ENTRY assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1} assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1}
assert len(mock_setup_entry.mock_calls) == 1 mock_setup_entry.assert_called_once()
async def test_user_step_with_existing_device(hass: HomeAssistant) -> None: async def test_user_step_with_existing_device(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we properly handle device picking.""" """Test we properly handle device picking."""
with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info( with patch_async_discovered_service_info(
[FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_2] [FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_2]
): ):
# Create the config flow # Create the config flow
@ -95,48 +100,50 @@ async def test_user_step_with_existing_device(hass: HomeAssistant) -> None:
result["flow_id"], user_input={CONF_ADDRESS: FAKE_ADDRESS_1} result["flow_id"], user_input={CONF_ADDRESS: FAKE_ADDRESS_1}
) )
assert len(mock_setup_entry.mock_calls) == 0 mock_setup_entry.assert_not_called()
async def test_bluetooth_discovered(hass: HomeAssistant) -> None: async def test_bluetooth_discovered(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we short circuit to config entry creation.""" """Test we short circuit to config entry creation."""
with patch_async_setup_entry() as mock_setup_entry: result = await hass.config_entries.flow.async_init(
result = await hass.config_entries.flow.async_init( DOMAIN,
DOMAIN, context={"source": config_entries.SOURCE_BLUETOOTH},
context={"source": config_entries.SOURCE_BLUETOOTH}, data=FAKE_SERVICE_INFO_1,
data=FAKE_SERVICE_INFO_1, )
)
assert result["type"] == FlowResultType.FORM assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "bluetooth_confirm" assert result["step_id"] == "bluetooth_confirm"
assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1} assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1}
assert len(mock_setup_entry.mock_calls) == 0 mock_setup_entry.assert_not_called()
async def test_bluetooth_confirm(hass: HomeAssistant) -> None: async def test_bluetooth_confirm(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we short circuit to config entry creation.""" """Test we short circuit to config entry creation."""
with patch_async_setup_entry() as mock_setup_entry: # Create the config flow
# Create the config flow result = await hass.config_entries.flow.async_init(
result = await hass.config_entries.flow.async_init( DOMAIN,
DOMAIN, context={
context={ "source": config_entries.SOURCE_BLUETOOTH,
"source": config_entries.SOURCE_BLUETOOTH, "step_id": "bluetooth_confirm",
"step_id": "bluetooth_confirm", "user_input": {CONF_MAC: FAKE_ADDRESS_1},
"user_input": {CONF_MAC: FAKE_ADDRESS_1}, },
}, data=FAKE_SERVICE_INFO_1,
data=FAKE_SERVICE_INFO_1, )
)
# Interact with it like a user would # Interact with it like a user would
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={} result["flow_id"], user_input={}
) )
assert result2["type"] == FlowResultType.CREATE_ENTRY assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == FAKE_ADDRESS_1 assert result2["title"] == FAKE_ADDRESS_1
assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1} assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1}
assert len(mock_setup_entry.mock_calls) == 1 mock_setup_entry.assert_called_once()