Use `mock_setup_entry` fixture for APCUPSD (#150392)
parent
69ace08c01
commit
79cfea3fea
|
@ -0,0 +1,15 @@
|
|||
"""Common fixtures for the APC UPS Daemon (APCUPSD) tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -17,13 +17,6 @@ from . import CONF_DATA, MOCK_MINIMAL_STATUS, MOCK_STATUS
|
|||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
def _patch_setup():
|
||||
return patch(
|
||||
"homeassistant.components.apcupsd.async_setup_entry",
|
||||
return_value=True,
|
||||
)
|
||||
|
||||
|
||||
async def test_config_flow_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test config flow setup with connection error."""
|
||||
with patch(
|
||||
|
@ -40,7 +33,9 @@ async def test_config_flow_cannot_connect(hass: HomeAssistant) -> None:
|
|||
assert result["errors"]["base"] == "cannot_connect"
|
||||
|
||||
|
||||
async def test_config_flow_duplicate_host_port(hass: HomeAssistant) -> None:
|
||||
async def test_config_flow_duplicate_host_port(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test duplicate config flow setup with the same host / port."""
|
||||
# First add an existing config entry to hass.
|
||||
mock_entry = MockConfigEntry(
|
||||
|
@ -53,12 +48,9 @@ async def test_config_flow_duplicate_host_port(hass: HomeAssistant) -> None:
|
|||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status,
|
||||
_patch_setup(),
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status:
|
||||
# Assign the same host and port, which we should reject since the entry already exists.
|
||||
mock_request_status.return_value = MOCK_STATUS
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -81,7 +73,9 @@ async def test_config_flow_duplicate_host_port(hass: HomeAssistant) -> None:
|
|||
assert result["data"] == another_host
|
||||
|
||||
|
||||
async def test_config_flow_duplicate_serial_number(hass: HomeAssistant) -> None:
|
||||
async def test_config_flow_duplicate_serial_number(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test duplicate config flow setup with different host but the same serial number."""
|
||||
# First add an existing config entry to hass.
|
||||
mock_entry = MockConfigEntry(
|
||||
|
@ -94,12 +88,9 @@ async def test_config_flow_duplicate_serial_number(hass: HomeAssistant) -> None:
|
|||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status,
|
||||
_patch_setup(),
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status:
|
||||
# Assign the different host and port, but we should still reject the creation since the
|
||||
# serial number is the same as the existing entry.
|
||||
mock_request_status.return_value = MOCK_STATUS
|
||||
|
@ -123,14 +114,11 @@ async def test_config_flow_duplicate_serial_number(hass: HomeAssistant) -> None:
|
|||
assert result["data"] == another_host
|
||||
|
||||
|
||||
async def test_flow_works(hass: HomeAssistant) -> None:
|
||||
async def test_flow_works(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
||||
"""Test successful creation of config entries via user configuration."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
),
|
||||
_patch_setup() as mock_setup,
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
|
@ -147,7 +135,7 @@ async def test_flow_works(hass: HomeAssistant) -> None:
|
|||
assert result["data"] == CONF_DATA
|
||||
assert result["result"].unique_id == MOCK_STATUS["SERIALNO"]
|
||||
|
||||
mock_setup.assert_called_once()
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
@ -162,19 +150,19 @@ async def test_flow_works(hass: HomeAssistant) -> None:
|
|||
],
|
||||
)
|
||||
async def test_flow_minimal_status(
|
||||
hass: HomeAssistant, extra_status: dict[str, str], expected_title: str
|
||||
hass: HomeAssistant,
|
||||
extra_status: dict[str, str],
|
||||
expected_title: str,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test successful creation of config entries via user configuration when minimal status is reported.
|
||||
|
||||
We test different combinations of minimal statuses, where the title of the
|
||||
integration will vary.
|
||||
"""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status,
|
||||
_patch_setup() as mock_setup,
|
||||
):
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status"
|
||||
) as mock_request_status:
|
||||
status = MOCK_MINIMAL_STATUS | extra_status
|
||||
mock_request_status.return_value = status
|
||||
|
||||
|
@ -185,10 +173,12 @@ async def test_flow_minimal_status(
|
|||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == CONF_DATA
|
||||
assert result["title"] == expected_title
|
||||
mock_setup.assert_called_once()
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
|
||||
async def test_reconfigure_flow_works(hass: HomeAssistant) -> None:
|
||||
async def test_reconfigure_flow_works(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test successful reconfiguration of an existing entry."""
|
||||
mock_entry = MockConfigEntry(
|
||||
version=1,
|
||||
|
@ -207,18 +197,15 @@ async def test_reconfigure_flow_works(hass: HomeAssistant) -> None:
|
|||
# New configuration data with different host/port.
|
||||
new_conf_data = {CONF_HOST: "new_host", CONF_PORT: 4321}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
),
|
||||
_patch_setup() as mock_setup,
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=new_conf_data
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
mock_setup.assert_called_once()
|
||||
mock_setup_entry.assert_called_once()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
|
@ -228,7 +215,9 @@ async def test_reconfigure_flow_works(hass: HomeAssistant) -> None:
|
|||
assert mock_entry.data[CONF_PORT] == new_conf_data[CONF_PORT]
|
||||
|
||||
|
||||
async def test_reconfigure_flow_cannot_connect(hass: HomeAssistant) -> None:
|
||||
async def test_reconfigure_flow_cannot_connect(
|
||||
hass: HomeAssistant, mock_setup_entry: AsyncMock
|
||||
) -> None:
|
||||
"""Test reconfiguration with connection error and recovery."""
|
||||
mock_entry = MockConfigEntry(
|
||||
version=1,
|
||||
|
@ -258,12 +247,9 @@ async def test_reconfigure_flow_cannot_connect(hass: HomeAssistant) -> None:
|
|||
assert result["errors"]["base"] == "cannot_connect"
|
||||
|
||||
# Test recovery by fixing the connection issue.
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
),
|
||||
_patch_setup(),
|
||||
with patch(
|
||||
"homeassistant.components.apcupsd.coordinator.aioapcaccess.request_status",
|
||||
return_value=MOCK_STATUS,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input=new_conf_data
|
||||
|
|
Loading…
Reference in New Issue