Use common mock fixture in Idasen Desk config flow tests (#133679)
parent
9c70ec4150
commit
954b6133cb
|
@ -10,10 +10,7 @@ rules:
|
|||
This integration does not use polling.
|
||||
brands: done
|
||||
common-modules: done
|
||||
config-flow-test-coverage:
|
||||
status: todo
|
||||
comment: |
|
||||
- use mock_desk_api
|
||||
config-flow-test-coverage: done
|
||||
config-flow: done
|
||||
dependency-transparency: done
|
||||
docs-actions:
|
||||
|
|
|
@ -19,9 +19,14 @@ def mock_bluetooth(enable_bluetooth: None) -> Generator[None]:
|
|||
@pytest.fixture(autouse=False)
|
||||
def mock_desk_api():
|
||||
"""Set up idasen desk API fixture."""
|
||||
with mock.patch(
|
||||
"homeassistant.components.idasen_desk.coordinator.Desk"
|
||||
) as desk_patched:
|
||||
with (
|
||||
mock.patch(
|
||||
"homeassistant.components.idasen_desk.coordinator.Desk"
|
||||
) as desk_patched,
|
||||
mock.patch(
|
||||
"homeassistant.components.idasen_desk.config_flow.Desk", new=desk_patched
|
||||
),
|
||||
):
|
||||
mock_desk = MagicMock()
|
||||
|
||||
def mock_init(
|
||||
|
@ -33,17 +38,20 @@ def mock_desk_api():
|
|||
|
||||
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.trigger_update_callback(None)
|
||||
if mock_desk.trigger_update_callback:
|
||||
mock_desk.trigger_update_callback(None)
|
||||
|
||||
async def mock_disconnect():
|
||||
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):
|
||||
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():
|
||||
await mock_move_to(100)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""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 idasen_ha.errors import AuthFailedError
|
||||
|
@ -17,7 +17,7 @@ from . import IDASEN_DISCOVERY_INFO, NOT_IDASEN_DISCOVERY_INFO
|
|||
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."""
|
||||
with patch(
|
||||
"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["errors"] == {}
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.connect"),
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"),
|
||||
patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
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(
|
||||
hass: HomeAssistant, exception: Exception, expected_error: str
|
||||
hass: HomeAssistant,
|
||||
mock_desk_api: MagicMock,
|
||||
exception: Exception,
|
||||
expected_error: str,
|
||||
) -> None:
|
||||
"""Test user step with a cannot connect error."""
|
||||
with patch(
|
||||
|
@ -113,33 +111,26 @@ async def test_user_step_cannot_connect(
|
|||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.idasen_desk.config_flow.Desk.connect",
|
||||
side_effect=exception,
|
||||
),
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
default_connect_side_effect = mock_desk_api.connect.side_effect
|
||||
mock_desk_api.connect.side_effect = exception
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ADDRESS: IDASEN_DISCOVERY_INFO.address,
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["step_id"] == "user"
|
||||
assert result2["errors"] == {"base": expected_error}
|
||||
|
||||
with (
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.connect"),
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"),
|
||||
patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
mock_desk_api.connect.side_effect = default_connect_side_effect
|
||||
with patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{
|
||||
|
@ -157,7 +148,9 @@ async def test_user_step_cannot_connect(
|
|||
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."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
|
@ -168,16 +161,10 @@ async def test_bluetooth_step_success(hass: HomeAssistant) -> None:
|
|||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.idasen_desk.config_flow.Desk.connect"
|
||||
) as desk_connect,
|
||||
patch("homeassistant.components.idasen_desk.config_flow.Desk.disconnect"),
|
||||
patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.idasen_desk.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
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 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)
|
||||
|
|
Loading…
Reference in New Issue