Refactor tests for modem_callerid (#59691)
* Refactor tests for modem_callerid * uno mas * uno mas * uno mas
This commit is contained in:
parent
f2f2a08966
commit
7947866962
3 changed files with 55 additions and 67 deletions
|
@ -3,24 +3,29 @@
|
|||
from unittest.mock import patch
|
||||
|
||||
from phone_modem import DEFAULT_PORT
|
||||
|
||||
from homeassistant.const import CONF_DEVICE
|
||||
|
||||
CONF_DATA = {CONF_DEVICE: DEFAULT_PORT}
|
||||
|
||||
IMPORT_DATA = {"sensor": {"platform": "modem_callerid"}}
|
||||
from serial.tools.list_ports_common import ListPortInfo
|
||||
|
||||
|
||||
def _patch_init_modem():
|
||||
def patch_init_modem():
|
||||
"""Mock modem."""
|
||||
return patch(
|
||||
"homeassistant.components.modem_callerid.PhoneModem",
|
||||
autospec=True,
|
||||
"homeassistant.components.modem_callerid.PhoneModem.initialize",
|
||||
)
|
||||
|
||||
|
||||
def _patch_config_flow_modem(mocked_modem):
|
||||
def patch_config_flow_modem():
|
||||
"""Mock modem config flow."""
|
||||
return patch(
|
||||
"homeassistant.components.modem_callerid.config_flow.PhoneModem",
|
||||
autospec=True,
|
||||
return_value=mocked_modem,
|
||||
"homeassistant.components.modem_callerid.config_flow.PhoneModem.test",
|
||||
)
|
||||
|
||||
|
||||
def com_port():
|
||||
"""Mock of a serial port."""
|
||||
port = ListPortInfo(DEFAULT_PORT)
|
||||
port.serial_number = "1234"
|
||||
port.manufacturer = "Virtual serial port"
|
||||
port.device = DEFAULT_PORT
|
||||
port.description = "Some serial port"
|
||||
|
||||
return port
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
"""Test Modem Caller ID config flow."""
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import phone_modem
|
||||
import serial.tools.list_ports
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components import usb
|
||||
from homeassistant.components.modem_callerid.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USB, SOURCE_USER
|
||||
from homeassistant.const import CONF_DEVICE, CONF_SOURCE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
|
||||
from . import _patch_config_flow_modem
|
||||
from . import com_port, patch_config_flow_modem
|
||||
|
||||
DISCOVERY_INFO = usb.UsbServiceInfo(
|
||||
device=phone_modem.DEFAULT_PORT,
|
||||
|
@ -30,51 +25,38 @@ DISCOVERY_INFO = usb.UsbServiceInfo(
|
|||
def _patch_setup():
|
||||
return patch(
|
||||
"homeassistant.components.modem_callerid.async_setup_entry",
|
||||
return_value=True,
|
||||
)
|
||||
|
||||
|
||||
def com_port():
|
||||
"""Mock of a serial port."""
|
||||
port = serial.tools.list_ports_common.ListPortInfo(phone_modem.DEFAULT_PORT)
|
||||
port.serial_number = "1234"
|
||||
port.manufacturer = "Virtual serial port"
|
||||
port.device = phone_modem.DEFAULT_PORT
|
||||
port.description = "Some serial port"
|
||||
|
||||
return port
|
||||
|
||||
|
||||
@patch("serial.tools.list_ports.comports", MagicMock(return_value=[com_port()]))
|
||||
async def test_flow_usb(hass: HomeAssistant):
|
||||
"""Test usb discovery flow."""
|
||||
port = com_port()
|
||||
with _patch_config_flow_modem(AsyncMock()), _patch_setup():
|
||||
with patch_config_flow_modem(), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USB},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "usb_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_DEVICE: phone_modem.DEFAULT_PORT},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_DEVICE: port.device}
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_DEVICE: com_port().device}
|
||||
|
||||
|
||||
@patch("serial.tools.list_ports.comports", MagicMock(return_value=[com_port()]))
|
||||
async def test_flow_usb_cannot_connect(hass: HomeAssistant):
|
||||
"""Test usb flow connection error."""
|
||||
with _patch_config_flow_modem(AsyncMock()) as modemmock:
|
||||
with patch_config_flow_modem() as modemmock:
|
||||
modemmock.side_effect = phone_modem.exceptions.SerialError
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USB}, data=DISCOVERY_INFO
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "cannot_connect"
|
||||
|
||||
|
||||
|
@ -90,14 +72,13 @@ async def test_flow_user(hass: HomeAssistant):
|
|||
port.vid,
|
||||
port.pid,
|
||||
)
|
||||
mocked_modem = AsyncMock()
|
||||
with _patch_config_flow_modem(mocked_modem), _patch_setup():
|
||||
with patch_config_flow_modem(), _patch_setup():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_DEVICE: port_select},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_DEVICE: port.device}
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
|
@ -105,7 +86,7 @@ async def test_flow_user(hass: HomeAssistant):
|
|||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_DEVICE: port_select},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "no_devices_found"
|
||||
|
||||
|
||||
|
@ -121,12 +102,12 @@ async def test_flow_user_error(hass: HomeAssistant):
|
|||
port.vid,
|
||||
port.pid,
|
||||
)
|
||||
with _patch_config_flow_modem(AsyncMock()) as modemmock:
|
||||
with patch_config_flow_modem() as modemmock:
|
||||
modemmock.side_effect = phone_modem.exceptions.SerialError
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={CONF_SOURCE: SOURCE_USER}, data={CONF_DEVICE: port_select}
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
@ -135,32 +116,32 @@ async def test_flow_user_error(hass: HomeAssistant):
|
|||
result["flow_id"],
|
||||
user_input={CONF_DEVICE: port_select},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["data"] == {CONF_DEVICE: port.device}
|
||||
|
||||
|
||||
@patch("serial.tools.list_ports.comports", MagicMock())
|
||||
async def test_flow_user_no_port_list(hass: HomeAssistant):
|
||||
"""Test user with no list of ports."""
|
||||
with _patch_config_flow_modem(AsyncMock()):
|
||||
with patch_config_flow_modem():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USER},
|
||||
data={CONF_DEVICE: phone_modem.DEFAULT_PORT},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_ABORT
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "no_devices_found"
|
||||
|
||||
|
||||
async def test_abort_user_with_existing_flow(hass: HomeAssistant):
|
||||
"""Test user flow is aborted when another discovery has happened."""
|
||||
with _patch_config_flow_modem(AsyncMock()):
|
||||
with patch_config_flow_modem():
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={CONF_SOURCE: SOURCE_USB},
|
||||
data=DISCOVERY_INFO,
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "usb_confirm"
|
||||
|
||||
result2 = await hass.config_entries.flow.async_init(
|
||||
|
@ -169,5 +150,5 @@ async def test_abort_user_with_existing_flow(hass: HomeAssistant):
|
|||
data={},
|
||||
)
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_ABORT
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result2["reason"] == "already_in_progress"
|
||||
|
|
|
@ -1,25 +1,29 @@
|
|||
"""Test Modem Caller ID integration."""
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from phone_modem import exceptions
|
||||
|
||||
from homeassistant.components.modem_callerid.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_DEVICE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import CONF_DATA, _patch_init_modem
|
||||
from . import com_port, patch_init_modem
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup_config(hass: HomeAssistant):
|
||||
"""Test Modem Caller ID setup."""
|
||||
async def test_setup_entry(hass: HomeAssistant):
|
||||
"""Test Modem Caller ID entry setup."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONF_DATA,
|
||||
data={CONF_DEVICE: com_port().device},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with _patch_init_modem():
|
||||
with patch("aioserial.AioSerial", return_value=AsyncMock()), patch(
|
||||
"homeassistant.components.modem_callerid.PhoneModem._get_response",
|
||||
return_value="OK",
|
||||
), patch("phone_modem.PhoneModem._modem_sm"):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert entry.state == ConfigEntryState.LOADED
|
||||
|
||||
|
@ -28,28 +32,26 @@ async def test_async_setup_entry_not_ready(hass: HomeAssistant):
|
|||
"""Test that it throws ConfigEntryNotReady when exception occurs during setup."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONF_DATA,
|
||||
data={CONF_DEVICE: com_port().device},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.modem_callerid.PhoneModem",
|
||||
side_effect=exceptions.SerialError(),
|
||||
):
|
||||
with patch_init_modem() as modemmock:
|
||||
modemmock.side_effect = exceptions.SerialError
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ConfigEntryState.SETUP_ERROR
|
||||
assert entry.state == ConfigEntryState.SETUP_RETRY
|
||||
assert not hass.data.get(DOMAIN)
|
||||
|
||||
|
||||
async def test_unload_config_entry(hass: HomeAssistant):
|
||||
async def test_unload_entry(hass: HomeAssistant):
|
||||
"""Test unload."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONF_DATA,
|
||||
data={CONF_DEVICE: com_port().device},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with _patch_init_modem():
|
||||
with patch_init_modem():
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
|
Loading…
Add table
Reference in a new issue