Skip to content

Commit

Permalink
Mock dwdwfsapi in all tests that use it (#116414)
Browse files Browse the repository at this point in the history
* Mock dwdwfsapi in all tests

* Add mocking for config entries

* Fix assertions in init test
  • Loading branch information
andarotajo committed Apr 30, 2024
1 parent a440783 commit 1e63665
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 107 deletions.
15 changes: 15 additions & 0 deletions tests/components/dwd_weather_warnings/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
"""Tests for Deutscher Wetterdienst (DWD) Weather Warnings."""

from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


async def init_integration(
hass: HomeAssistant, entry: MockConfigEntry
) -> MockConfigEntry:
"""Set up the integration based on the config entry."""
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

return entry
73 changes: 72 additions & 1 deletion tests/components/dwd_weather_warnings/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
"""Configuration for Deutscher Wetterdienst (DWD) Weather Warnings tests."""

from collections.abc import Generator
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import pytest

from homeassistant.components.dwd_weather_warnings.const import (
ADVANCE_WARNING_SENSOR,
CONF_REGION_DEVICE_TRACKER,
CONF_REGION_IDENTIFIER,
CURRENT_WARNING_SENSOR,
DOMAIN,
)
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME

from tests.common import MockConfigEntry

MOCK_NAME = "Unit Test"
MOCK_REGION_IDENTIFIER = "807111000"
MOCK_REGION_DEVICE_TRACKER = "device_tracker.test_gps"
MOCK_CONDITIONS = [CURRENT_WARNING_SENSOR, ADVANCE_WARNING_SENSOR]


@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
Expand All @@ -14,3 +30,58 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry


@pytest.fixture
def mock_identifier_entry() -> MockConfigEntry:
"""Return a mocked config entry with a region identifier."""
return MockConfigEntry(
domain=DOMAIN,
data={
CONF_NAME: MOCK_NAME,
CONF_REGION_IDENTIFIER: MOCK_REGION_IDENTIFIER,
CONF_MONITORED_CONDITIONS: MOCK_CONDITIONS,
},
)


@pytest.fixture
def mock_tracker_entry() -> MockConfigEntry:
"""Return a mocked config entry with a region identifier."""
return MockConfigEntry(
domain=DOMAIN,
data={
CONF_NAME: MOCK_NAME,
CONF_REGION_DEVICE_TRACKER: MOCK_REGION_DEVICE_TRACKER,
CONF_MONITORED_CONDITIONS: MOCK_CONDITIONS,
},
)


@pytest.fixture
def mock_dwdwfsapi() -> Generator[MagicMock, None, None]:
"""Return a mocked dwdwfsapi API client."""
with (
patch(
"homeassistant.components.dwd_weather_warnings.coordinator.DwdWeatherWarningsAPI",
autospec=True,
) as mock_api,
patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
new=mock_api,
),
):
api = mock_api.return_value
api.data_valid = False
api.warncell_id = None
api.warncell_name = None
api.last_update = None
api.current_warning_level = None
api.current_warnings = None
api.expected_warning_level = None
api.expected_warnings = None
api.update = Mock()
api.__bool__ = Mock()
api.__bool__.return_value = True

yield api
84 changes: 36 additions & 48 deletions tests/components/dwd_weather_warnings/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tests for Deutscher Wetterdienst (DWD) Weather Warnings config flow."""

from typing import Final
from unittest.mock import patch
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -29,34 +29,30 @@
pytestmark = pytest.mark.usefixtures("mock_setup_entry")


async def test_create_entry_region(hass: HomeAssistant) -> None:
async def test_create_entry_region(
hass: HomeAssistant, mock_dwdwfsapi: MagicMock
) -> None:
"""Test that the full config flow works for a region identifier."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM

with patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
return_value=False,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)
mock_dwdwfsapi.__bool__.return_value = False
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)

# Test for invalid region identifier.
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_identifier"}

with patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
return_value=True,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)
mock_dwdwfsapi.__bool__.return_value = True
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)

# Test for successfully created entry.
await hass.async_block_till_done()
Expand All @@ -68,22 +64,22 @@ async def test_create_entry_region(hass: HomeAssistant) -> None:


async def test_create_entry_gps(
hass: HomeAssistant, entity_registry: er.EntityRegistry
hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_dwdwfsapi: MagicMock
) -> None:
"""Test that the full config flow works for a device tracker."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM

# Test for missing registry entry error.
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_GPS
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "entity_not_found"}

# Test for missing device tracker error.
Expand All @@ -96,7 +92,7 @@ async def test_create_entry_gps(
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "entity_not_found"}

# Test for missing attribute error.
Expand All @@ -111,7 +107,7 @@ async def test_create_entry_gps(
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "attribute_not_found"}

# Test for invalid provided identifier.
Expand All @@ -121,36 +117,32 @@ async def test_create_entry_gps(
{ATTR_LATITUDE: "50.180454", ATTR_LONGITUDE: "7.610263"},
)

with patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
return_value=False,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_GPS
)
mock_dwdwfsapi.__bool__.return_value = False
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_GPS
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "invalid_identifier"}

# Test for successfully created entry.
with patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
return_value=True,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_GPS
)
mock_dwdwfsapi.__bool__.return_value = True
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_GPS
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "test_gps"
assert result["data"] == {
CONF_REGION_DEVICE_TRACKER: registry_entry.id,
}


async def test_config_flow_already_configured(hass: HomeAssistant) -> None:
async def test_config_flow_already_configured(
hass: HomeAssistant, mock_dwdwfsapi: MagicMock
) -> None:
"""Test aborting, if the warncell ID / name is already configured during the config."""
entry = MockConfigEntry(
domain=DOMAIN,
Expand All @@ -167,13 +159,9 @@ async def test_config_flow_already_configured(hass: HomeAssistant) -> None:
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM

with patch(
"homeassistant.components.dwd_weather_warnings.config_flow.DwdWeatherWarningsAPI",
return_value=True,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input=DEMO_CONFIG_ENTRY_REGION
)

await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
Expand All @@ -187,15 +175,15 @@ async def test_config_flow_with_errors(hass: HomeAssistant) -> None:
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM

# Test error for empty input data.
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "no_identifier"}

# Test error for setting both options during configuration.
Expand All @@ -207,5 +195,5 @@ async def test_config_flow_with_errors(hass: HomeAssistant) -> None:
)

await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "ambiguous_identifier"}

0 comments on commit 1e63665

Please sign in to comment.