Small cleanup in conftest mocks of Sensors.Community (#103630)

This commit is contained in:
Franck Nijhof 2023-11-08 01:11:07 +01:00 committed by GitHub
parent 2b12a95607
commit 22fa33ce7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 22 deletions

View File

@ -33,24 +33,16 @@ def mock_setup_entry() -> Generator[None, None, None]:
yield 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 @pytest.fixture
def mock_luftdaten() -> Generator[None, MagicMock, None]: def mock_luftdaten() -> Generator[None, MagicMock, None]:
"""Return a mocked Luftdaten client.""" """Return a mocked Luftdaten client."""
with patch( with patch(
"homeassistant.components.luftdaten.Luftdaten", autospec=True "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 = luftdaten_mock.return_value
luftdaten.validate_sensor.return_value = True
luftdaten.sensor_id = 12345 luftdaten.sensor_id = 12345
luftdaten.meta = { luftdaten.meta = {
"altitude": 123.456, "altitude": 123.456,

View File

@ -2,6 +2,7 @@
from unittest.mock import MagicMock from unittest.mock import MagicMock
from luftdaten.exceptions import LuftdatenConnectionError from luftdaten.exceptions import LuftdatenConnectionError
import pytest
from homeassistant.components.luftdaten import DOMAIN from homeassistant.components.luftdaten import DOMAIN
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
@ -36,7 +37,7 @@ async def test_duplicate_error(
async def test_communication_error( async def test_communication_error(
hass: HomeAssistant, mock_luftdaten_config_flow: MagicMock hass: HomeAssistant, mock_luftdaten: MagicMock
) -> None: ) -> None:
"""Test that no sensor is added while unable to communicate with API.""" """Test that no sensor is added while unable to communicate with API."""
result = await hass.config_entries.flow.async_init( 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("type") == FlowResultType.FORM
assert result.get("step_id") == "user" 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_SENSOR_ID: 12345}, user_input={CONF_SENSOR_ID: 12345},
@ -56,7 +57,7 @@ async def test_communication_error(
assert result2.get("step_id") == "user" assert result2.get("step_id") == "user"
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"} 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( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
user_input={CONF_SENSOR_ID: 12345}, user_input={CONF_SENSOR_ID: 12345},
@ -70,9 +71,7 @@ async def test_communication_error(
} }
async def test_invalid_sensor( async def test_invalid_sensor(hass: HomeAssistant, mock_luftdaten: MagicMock) -> None:
hass: HomeAssistant, mock_luftdaten_config_flow: MagicMock
) -> None:
"""Test that an invalid sensor throws an error.""" """Test that an invalid sensor throws an error."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER} DOMAIN, context={"source": SOURCE_USER}
@ -81,7 +80,7 @@ async def test_invalid_sensor(
assert result.get("type") == FlowResultType.FORM assert result.get("type") == FlowResultType.FORM
assert result.get("step_id") == "user" 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( result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], result["flow_id"],
user_input={CONF_SENSOR_ID: 11111}, user_input={CONF_SENSOR_ID: 11111},
@ -91,7 +90,7 @@ async def test_invalid_sensor(
assert result2.get("step_id") == "user" assert result2.get("step_id") == "user"
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"} 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( result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], result2["flow_id"],
user_input={CONF_SENSOR_ID: 12345}, 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( async def test_step_user(
hass: HomeAssistant, hass: HomeAssistant,
mock_setup_entry: MagicMock,
mock_luftdaten_config_flow: MagicMock,
) -> None: ) -> None:
"""Test that the user step works.""" """Test that the user step works."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(