Use common mock fixture in Idasen Desk config flow tests (#133679)

This commit is contained in:
Abílio Costa 2024-12-21 08:35:47 +00:00 committed by GitHub
parent 9c70ec4150
commit 954b6133cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 56 deletions

View File

@ -10,10 +10,7 @@ rules:
This integration does not use polling. This integration does not use polling.
brands: done brands: done
common-modules: done common-modules: done
config-flow-test-coverage: config-flow-test-coverage: done
status: todo
comment: |
- use mock_desk_api
config-flow: done config-flow: done
dependency-transparency: done dependency-transparency: done
docs-actions: docs-actions:

View File

@ -19,9 +19,14 @@ def mock_bluetooth(enable_bluetooth: None) -> Generator[None]:
@pytest.fixture(autouse=False) @pytest.fixture(autouse=False)
def mock_desk_api(): def mock_desk_api():
"""Set up idasen desk API fixture.""" """Set up idasen desk API fixture."""
with mock.patch( with (
"homeassistant.components.idasen_desk.coordinator.Desk" mock.patch(
) as desk_patched: "homeassistant.components.idasen_desk.coordinator.Desk"
) as desk_patched,
mock.patch(
"homeassistant.components.idasen_desk.config_flow.Desk", new=desk_patched
),
):
mock_desk = MagicMock() mock_desk = MagicMock()
def mock_init( def mock_init(
@ -33,17 +38,20 @@ def mock_desk_api():
desk_patched.side_effect = mock_init desk_patched.side_effect = mock_init
async def mock_connect(ble_device): async def mock_connect(ble_device, retry: bool = True):
mock_desk.is_connected = True mock_desk.is_connected = True
mock_desk.trigger_update_callback(None) if mock_desk.trigger_update_callback:
mock_desk.trigger_update_callback(None)
async def mock_disconnect(): async def mock_disconnect():
mock_desk.is_connected = False mock_desk.is_connected = False
mock_desk.trigger_update_callback(None) if mock_desk.trigger_update_callback:
mock_desk.trigger_update_callback(None)
async def mock_move_to(height: float): async def mock_move_to(height: float):
mock_desk.height_percent = height mock_desk.height_percent = height
mock_desk.trigger_update_callback(height) if mock_desk.trigger_update_callback:
mock_desk.trigger_update_callback(height)
async def mock_move_up(): async def mock_move_up():
await mock_move_to(100) await mock_move_to(100)

View File

@ -1,6 +1,6 @@
"""Test the IKEA Idasen Desk config flow.""" """Test the IKEA Idasen Desk config flow."""
from unittest.mock import ANY, patch from unittest.mock import ANY, MagicMock, patch
from bleak.exc import BleakError from bleak.exc import BleakError
from idasen_ha.errors import AuthFailedError from idasen_ha.errors import AuthFailedError
@ -17,7 +17,7 @@ from . import IDASEN_DISCOVERY_INFO, NOT_IDASEN_DISCOVERY_INFO
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_user_step_success(hass: HomeAssistant) -> None: async def test_user_step_success(hass: HomeAssistant, mock_desk_api: MagicMock) -> None:
"""Test user step success path.""" """Test user step success path."""
with patch( with patch(
"homeassistant.components.idasen_desk.config_flow.async_discovered_service_info", "homeassistant.components.idasen_desk.config_flow.async_discovered_service_info",
@ -30,14 +30,9 @@ async def test_user_step_success(hass: HomeAssistant) -> None:
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"] == {} assert result["errors"] == {}
with ( with patch(
patch("homeassistant.components.idasen_desk.config_flow.Desk.connect"), "homeassistant.components.idasen_desk.async_setup_entry", return_value=True
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"), ) as mock_setup_entry:
patch(
"homeassistant.components.idasen_desk.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
@ -99,7 +94,10 @@ async def test_user_step_no_new_devices_found(hass: HomeAssistant) -> None:
], ],
) )
async def test_user_step_cannot_connect( async def test_user_step_cannot_connect(
hass: HomeAssistant, exception: Exception, expected_error: str hass: HomeAssistant,
mock_desk_api: MagicMock,
exception: Exception,
expected_error: str,
) -> None: ) -> None:
"""Test user step with a cannot connect error.""" """Test user step with a cannot connect error."""
with patch( with patch(
@ -113,33 +111,26 @@ async def test_user_step_cannot_connect(
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"] == {} assert result["errors"] == {}
with ( default_connect_side_effect = mock_desk_api.connect.side_effect
patch( mock_desk_api.connect.side_effect = exception
"homeassistant.components.idasen_desk.config_flow.Desk.connect",
side_effect=exception, result2 = await hass.config_entries.flow.async_configure(
), result["flow_id"],
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"), {
): CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address,
result2 = await hass.config_entries.flow.async_configure( },
result["flow_id"], )
{ await hass.async_block_till_done()
CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address,
},
)
await hass.async_block_till_done()
assert result2["type"] is FlowResultType.FORM assert result2["type"] is FlowResultType.FORM
assert result2["step_id"] == "user" assert result2["step_id"] == "user"
assert result2["errors"] == {"base": expected_error} assert result2["errors"] == {"base": expected_error}
with ( mock_desk_api.connect.side_effect = default_connect_side_effect
patch("homeassistant.components.idasen_desk.config_flow.Desk.connect"), with patch(
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"), "homeassistant.components.idasen_desk.async_setup_entry",
patch( return_value=True,
"homeassistant.components.idasen_desk.async_setup_entry", ) as mock_setup_entry:
return_value=True,
) as mock_setup_entry,
):
result3 = await hass.config_entries.flow.async_configure( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
{ {
@ -157,7 +148,9 @@ async def test_user_step_cannot_connect(
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
async def test_bluetooth_step_success(hass: HomeAssistant) -> None: async def test_bluetooth_step_success(
hass: HomeAssistant, mock_desk_api: MagicMock
) -> None:
"""Test bluetooth step success path.""" """Test bluetooth step success path."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -168,16 +161,10 @@ async def test_bluetooth_step_success(hass: HomeAssistant) -> None:
assert result["step_id"] == "user" assert result["step_id"] == "user"
assert result["errors"] == {} assert result["errors"] == {}
with ( with patch(
patch( "homeassistant.components.idasen_desk.async_setup_entry",
"homeassistant.components.idasen_desk.config_flow.Desk.connect" return_value=True,
) as desk_connect, ) as mock_setup_entry:
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"),
patch(
"homeassistant.components.idasen_desk.async_setup_entry",
return_value=True,
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
{ {
@ -193,4 +180,4 @@ async def test_bluetooth_step_success(hass: HomeAssistant) -> None:
} }
assert result2["result"].unique_id == IDASEN_DISCOVERY_INFO.address assert result2["result"].unique_id == IDASEN_DISCOVERY_INFO.address
assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1
desk_connect.assert_called_with(ANY, retry=False) mock_desk_api.connect.assert_called_with(ANY, retry=False)