From 22fa33ce7a98cd0c0a54fce3c678251b88e8a2c4 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 8 Nov 2023 01:11:07 +0100 Subject: [PATCH] Small cleanup in conftest mocks of Sensors.Community (#103630) --- tests/components/luftdaten/conftest.py | 16 ++++------------ tests/components/luftdaten/test_config_flow.py | 18 ++++++++---------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/tests/components/luftdaten/conftest.py b/tests/components/luftdaten/conftest.py index 248e1344f1b..08cbe7a2c3c 100644 --- a/tests/components/luftdaten/conftest.py +++ b/tests/components/luftdaten/conftest.py @@ -33,24 +33,16 @@ def mock_setup_entry() -> Generator[None, None, None]: yield -@pytest.fixture -def mock_luftdaten_config_flow() -> Generator[None, MagicMock, None]: - """Return a mocked Luftdaten client.""" - with patch( - "homeassistant.components.luftdaten.config_flow.Luftdaten", autospec=True - ) as luftdaten_mock: - luftdaten = luftdaten_mock.return_value - luftdaten.validate_sensor.return_value = True - yield luftdaten - - @pytest.fixture def mock_luftdaten() -> Generator[None, MagicMock, None]: """Return a mocked Luftdaten client.""" with patch( "homeassistant.components.luftdaten.Luftdaten", autospec=True - ) as luftdaten_mock: + ) as luftdaten_mock, patch( + "homeassistant.components.luftdaten.config_flow.Luftdaten", new=luftdaten_mock + ): luftdaten = luftdaten_mock.return_value + luftdaten.validate_sensor.return_value = True luftdaten.sensor_id = 12345 luftdaten.meta = { "altitude": 123.456, diff --git a/tests/components/luftdaten/test_config_flow.py b/tests/components/luftdaten/test_config_flow.py index 5197a101bfd..a0b741f7d2a 100644 --- a/tests/components/luftdaten/test_config_flow.py +++ b/tests/components/luftdaten/test_config_flow.py @@ -2,6 +2,7 @@ from unittest.mock import MagicMock from luftdaten.exceptions import LuftdatenConnectionError +import pytest from homeassistant.components.luftdaten import DOMAIN from homeassistant.components.luftdaten.const import CONF_SENSOR_ID @@ -36,7 +37,7 @@ async def test_duplicate_error( async def test_communication_error( - hass: HomeAssistant, mock_luftdaten_config_flow: MagicMock + hass: HomeAssistant, mock_luftdaten: MagicMock ) -> None: """Test that no sensor is added while unable to communicate with API.""" result = await hass.config_entries.flow.async_init( @@ -46,7 +47,7 @@ async def test_communication_error( assert result.get("type") == FlowResultType.FORM assert result.get("step_id") == "user" - mock_luftdaten_config_flow.get_data.side_effect = LuftdatenConnectionError + mock_luftdaten.get_data.side_effect = LuftdatenConnectionError result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_SENSOR_ID: 12345}, @@ -56,7 +57,7 @@ async def test_communication_error( assert result2.get("step_id") == "user" assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"} - mock_luftdaten_config_flow.get_data.side_effect = None + mock_luftdaten.get_data.side_effect = None result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input={CONF_SENSOR_ID: 12345}, @@ -70,9 +71,7 @@ async def test_communication_error( } -async def test_invalid_sensor( - hass: HomeAssistant, mock_luftdaten_config_flow: MagicMock -) -> None: +async def test_invalid_sensor(hass: HomeAssistant, mock_luftdaten: MagicMock) -> None: """Test that an invalid sensor throws an error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} @@ -81,7 +80,7 @@ async def test_invalid_sensor( assert result.get("type") == FlowResultType.FORM assert result.get("step_id") == "user" - mock_luftdaten_config_flow.validate_sensor.return_value = False + mock_luftdaten.validate_sensor.return_value = False result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_SENSOR_ID: 11111}, @@ -91,7 +90,7 @@ async def test_invalid_sensor( assert result2.get("step_id") == "user" assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"} - mock_luftdaten_config_flow.validate_sensor.return_value = True + mock_luftdaten.validate_sensor.return_value = True result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input={CONF_SENSOR_ID: 12345}, @@ -105,10 +104,9 @@ async def test_invalid_sensor( } +@pytest.mark.usefixtures("mock_setup_entry", "mock_luftdaten") async def test_step_user( hass: HomeAssistant, - mock_setup_entry: MagicMock, - mock_luftdaten_config_flow: MagicMock, ) -> None: """Test that the user step works.""" result = await hass.config_entries.flow.async_init(