Use pytest.mark.parametrize in airthings_ble tests (#119461)

This commit is contained in:
epenet 2024-06-12 12:23:24 +02:00 committed by GitHub
parent 7d631c28a6
commit 8323266960
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ from unittest.mock import patch
from airthings_ble import AirthingsDevice, AirthingsDeviceType from airthings_ble import AirthingsDevice, AirthingsDeviceType
from bleak import BleakError from bleak import BleakError
import pytest
from homeassistant.components.airthings_ble.const import DOMAIN from homeassistant.components.airthings_ble.const import DOMAIN
from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER
@ -71,24 +72,25 @@ async def test_bluetooth_discovery_no_BLEDevice(hass: HomeAssistant) -> None:
assert result["reason"] == "cannot_connect" assert result["reason"] == "cannot_connect"
@pytest.mark.parametrize(
("exc", "reason"), [(Exception(), "unknown"), (BleakError(), "cannot_connect")]
)
async def test_bluetooth_discovery_airthings_ble_update_failed( async def test_bluetooth_discovery_airthings_ble_update_failed(
hass: HomeAssistant, hass: HomeAssistant, exc: Exception, reason: str
) -> None: ) -> None:
"""Test discovery via bluetooth but there's an exception from airthings-ble.""" """Test discovery via bluetooth but there's an exception from airthings-ble."""
for loop in [(Exception(), "unknown"), (BleakError(), "cannot_connect")]: with (
exc, reason = loop patch_async_ble_device_from_address(WAVE_SERVICE_INFO),
with ( patch_airthings_ble(side_effect=exc),
patch_async_ble_device_from_address(WAVE_SERVICE_INFO), ):
patch_airthings_ble(side_effect=exc), result = await hass.config_entries.flow.async_init(
): DOMAIN,
result = await hass.config_entries.flow.async_init( context={"source": SOURCE_BLUETOOTH},
DOMAIN, data=WAVE_SERVICE_INFO,
context={"source": SOURCE_BLUETOOTH}, )
data=WAVE_SERVICE_INFO,
)
assert result["type"] is FlowResultType.ABORT assert result["type"] is FlowResultType.ABORT
assert result["reason"] == reason assert result["reason"] == reason
async def test_bluetooth_discovery_already_setup(hass: HomeAssistant) -> None: async def test_bluetooth_discovery_already_setup(hass: HomeAssistant) -> None: