From f4254208997901eeb554e3c1a3de0357dd63ab7a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:10:13 +0200 Subject: [PATCH] Improve type hints in rainforest_raven tests (#118950) --- .../rainforest_raven/test_config_flow.py | 59 +++++++++---------- .../rainforest_raven/test_coordinator.py | 24 ++++++-- .../components/rainforest_raven/test_init.py | 6 +- .../rainforest_raven/test_sensor.py | 5 +- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/tests/components/rainforest_raven/test_config_flow.py b/tests/components/rainforest_raven/test_config_flow.py index d86dee6e0f6..36e03254dc5 100644 --- a/tests/components/rainforest_raven/test_config_flow.py +++ b/tests/components/rainforest_raven/test_config_flow.py @@ -1,10 +1,11 @@ """Test Rainforest RAVEn config flow.""" -from unittest.mock import patch +from collections.abc import Generator +from unittest.mock import AsyncMock, patch from aioraven.device import RAVEnConnectionError import pytest -import serial.tools.list_ports +from serial.tools.list_ports_common import ListPortInfo from homeassistant.components.rainforest_raven.const import DOMAIN from homeassistant.config_entries import SOURCE_USB, SOURCE_USER @@ -19,7 +20,7 @@ from tests.common import MockConfigEntry @pytest.fixture -def mock_device(): +def mock_device() -> Generator[AsyncMock, None, None]: """Mock a functioning RAVEn device.""" device = create_mock_device() with patch( @@ -30,7 +31,7 @@ def mock_device(): @pytest.fixture -def mock_device_no_open(mock_device): +def mock_device_no_open(mock_device: AsyncMock) -> AsyncMock: """Mock a device which fails to open.""" mock_device.__aenter__.side_effect = RAVEnConnectionError mock_device.open.side_effect = RAVEnConnectionError @@ -38,7 +39,7 @@ def mock_device_no_open(mock_device): @pytest.fixture -def mock_device_comm_error(mock_device): +def mock_device_comm_error(mock_device: AsyncMock) -> AsyncMock: """Mock a device which fails to read or parse raw data.""" mock_device.get_meter_list.side_effect = RAVEnConnectionError mock_device.get_meter_info.side_effect = RAVEnConnectionError @@ -46,7 +47,7 @@ def mock_device_comm_error(mock_device): @pytest.fixture -def mock_device_timeout(mock_device): +def mock_device_timeout(mock_device: AsyncMock) -> AsyncMock: """Mock a device which times out when queried.""" mock_device.get_meter_list.side_effect = TimeoutError mock_device.get_meter_info.side_effect = TimeoutError @@ -54,9 +55,9 @@ def mock_device_timeout(mock_device): @pytest.fixture -def mock_comports(): +def mock_comports() -> Generator[list[ListPortInfo], None, None]: """Mock serial port list.""" - port = serial.tools.list_ports_common.ListPortInfo(DISCOVERY_INFO.device) + port = ListPortInfo(DISCOVERY_INFO.device) port.serial_number = DISCOVERY_INFO.serial_number port.manufacturer = DISCOVERY_INFO.manufacturer port.device = DISCOVERY_INFO.device @@ -68,7 +69,8 @@ def mock_comports(): yield comports -async def test_flow_usb(hass: HomeAssistant, mock_comports, mock_device): +@pytest.mark.usefixtures("mock_comports", "mock_device") +async def test_flow_usb(hass: HomeAssistant) -> None: """Test usb flow connection.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USB}, data=DISCOVERY_INFO @@ -86,9 +88,8 @@ async def test_flow_usb(hass: HomeAssistant, mock_comports, mock_device): assert result.get("type") is FlowResultType.CREATE_ENTRY -async def test_flow_usb_cannot_connect( - hass: HomeAssistant, mock_comports, mock_device_no_open -): +@pytest.mark.usefixtures("mock_comports", "mock_device_no_open") +async def test_flow_usb_cannot_connect(hass: HomeAssistant) -> None: """Test usb flow connection error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USB}, data=DISCOVERY_INFO @@ -98,9 +99,8 @@ async def test_flow_usb_cannot_connect( assert result.get("reason") == "cannot_connect" -async def test_flow_usb_timeout_connect( - hass: HomeAssistant, mock_comports, mock_device_timeout -): +@pytest.mark.usefixtures("mock_comports", "mock_device_timeout") +async def test_flow_usb_timeout_connect(hass: HomeAssistant) -> None: """Test usb flow connection timeout.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USB}, data=DISCOVERY_INFO @@ -110,9 +110,8 @@ async def test_flow_usb_timeout_connect( assert result.get("reason") == "timeout_connect" -async def test_flow_usb_comm_error( - hass: HomeAssistant, mock_comports, mock_device_comm_error -): +@pytest.mark.usefixtures("mock_comports", "mock_device_comm_error") +async def test_flow_usb_comm_error(hass: HomeAssistant) -> None: """Test usb flow connection failure to communicate.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_USB}, data=DISCOVERY_INFO @@ -122,7 +121,8 @@ async def test_flow_usb_comm_error( assert result.get("reason") == "cannot_connect" -async def test_flow_user(hass: HomeAssistant, mock_comports, mock_device): +@pytest.mark.usefixtures("mock_comports", "mock_device") +async def test_flow_user(hass: HomeAssistant) -> None: """Test user flow connection.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -153,7 +153,8 @@ async def test_flow_user(hass: HomeAssistant, mock_comports, mock_device): assert result.get("type") is FlowResultType.CREATE_ENTRY -async def test_flow_user_no_available_devices(hass: HomeAssistant, mock_comports): +@pytest.mark.usefixtures("mock_comports") +async def test_flow_user_no_available_devices(hass: HomeAssistant) -> None: """Test user flow with no available devices.""" entry = MockConfigEntry( domain=DOMAIN, @@ -169,7 +170,8 @@ async def test_flow_user_no_available_devices(hass: HomeAssistant, mock_comports assert result.get("reason") == "no_devices_found" -async def test_flow_user_in_progress(hass: HomeAssistant, mock_comports): +@pytest.mark.usefixtures("mock_comports") +async def test_flow_user_in_progress(hass: HomeAssistant) -> None: """Test user flow with no available devices.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -190,9 +192,8 @@ async def test_flow_user_in_progress(hass: HomeAssistant, mock_comports): assert result.get("reason") == "already_in_progress" -async def test_flow_user_cannot_connect( - hass: HomeAssistant, mock_comports, mock_device_no_open -): +@pytest.mark.usefixtures("mock_comports", "mock_device_no_open") +async def test_flow_user_cannot_connect(hass: HomeAssistant) -> None: """Test user flow connection failure to communicate.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -206,9 +207,8 @@ async def test_flow_user_cannot_connect( assert result.get("errors") == {CONF_DEVICE: "cannot_connect"} -async def test_flow_user_timeout_connect( - hass: HomeAssistant, mock_comports, mock_device_timeout -): +@pytest.mark.usefixtures("mock_comports", "mock_device_timeout") +async def test_flow_user_timeout_connect(hass: HomeAssistant) -> None: """Test user flow connection failure to communicate.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -222,9 +222,8 @@ async def test_flow_user_timeout_connect( assert result.get("errors") == {CONF_DEVICE: "timeout_connect"} -async def test_flow_user_comm_error( - hass: HomeAssistant, mock_comports, mock_device_comm_error -): +@pytest.mark.usefixtures("mock_comports", "mock_device_comm_error") +async def test_flow_user_comm_error(hass: HomeAssistant) -> None: """Test user flow connection failure to communicate.""" result = await hass.config_entries.flow.async_init( DOMAIN, diff --git a/tests/components/rainforest_raven/test_coordinator.py b/tests/components/rainforest_raven/test_coordinator.py index cc0bcac3978..db70118f7b9 100644 --- a/tests/components/rainforest_raven/test_coordinator.py +++ b/tests/components/rainforest_raven/test_coordinator.py @@ -2,6 +2,7 @@ import asyncio import functools +from unittest.mock import AsyncMock from aioraven.device import RAVEnConnectionError import pytest @@ -13,7 +14,8 @@ from homeassistant.exceptions import ConfigEntryNotReady from . import create_mock_entry -async def test_coordinator_device_info(hass: HomeAssistant, mock_device): +@pytest.mark.usefixtures("mock_device") +async def test_coordinator_device_info(hass: HomeAssistant) -> None: """Test reporting device information from the coordinator.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) @@ -37,7 +39,9 @@ async def test_coordinator_device_info(hass: HomeAssistant, mock_device): assert coordinator.device_name == "RAVEn Device" -async def test_coordinator_cache_device(hass: HomeAssistant, mock_device): +async def test_coordinator_cache_device( + hass: HomeAssistant, mock_device: AsyncMock +) -> None: """Test that the device isn't re-opened for subsequent refreshes.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) @@ -51,7 +55,9 @@ async def test_coordinator_cache_device(hass: HomeAssistant, mock_device): assert mock_device.open.call_count == 1 -async def test_coordinator_device_error_setup(hass: HomeAssistant, mock_device): +async def test_coordinator_device_error_setup( + hass: HomeAssistant, mock_device: AsyncMock +) -> None: """Test handling of a device error during initialization.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) @@ -61,7 +67,9 @@ async def test_coordinator_device_error_setup(hass: HomeAssistant, mock_device): await coordinator.async_config_entry_first_refresh() -async def test_coordinator_device_error_update(hass: HomeAssistant, mock_device): +async def test_coordinator_device_error_update( + hass: HomeAssistant, mock_device: AsyncMock +) -> None: """Test handling of a device error during an update.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) @@ -74,7 +82,9 @@ async def test_coordinator_device_error_update(hass: HomeAssistant, mock_device) assert coordinator.last_update_success is False -async def test_coordinator_device_timeout_update(hass: HomeAssistant, mock_device): +async def test_coordinator_device_timeout_update( + hass: HomeAssistant, mock_device: AsyncMock +) -> None: """Test handling of a device timeout during an update.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) @@ -87,7 +97,9 @@ async def test_coordinator_device_timeout_update(hass: HomeAssistant, mock_devic assert coordinator.last_update_success is False -async def test_coordinator_comm_error(hass: HomeAssistant, mock_device): +async def test_coordinator_comm_error( + hass: HomeAssistant, mock_device: AsyncMock +) -> None: """Test handling of an error parsing or reading raw device data.""" entry = create_mock_entry() coordinator = RAVEnDataCoordinator(hass, entry) diff --git a/tests/components/rainforest_raven/test_init.py b/tests/components/rainforest_raven/test_init.py index 5214e1ca563..974c45150a6 100644 --- a/tests/components/rainforest_raven/test_init.py +++ b/tests/components/rainforest_raven/test_init.py @@ -4,8 +4,12 @@ from homeassistant.components.rainforest_raven.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from tests.common import MockConfigEntry -async def test_load_unload_entry(hass: HomeAssistant, mock_entry): + +async def test_load_unload_entry( + hass: HomeAssistant, mock_entry: MockConfigEntry +) -> None: """Test load and unload.""" assert len(hass.config_entries.async_entries(DOMAIN)) == 1 assert mock_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/rainforest_raven/test_sensor.py b/tests/components/rainforest_raven/test_sensor.py index 3259d8d7f2f..3b859621cb4 100644 --- a/tests/components/rainforest_raven/test_sensor.py +++ b/tests/components/rainforest_raven/test_sensor.py @@ -1,9 +1,12 @@ """Tests for the Rainforest RAVEn sensors.""" +import pytest + from homeassistant.core import HomeAssistant -async def test_sensors(hass: HomeAssistant, mock_device, mock_entry): +@pytest.mark.usefixtures("mock_entry") +async def test_sensors(hass: HomeAssistant) -> None: """Test the sensors.""" assert len(hass.states.async_all()) == 5