From 38a4f08e157c5992510b8f972ac7ee2fdfc9ddde Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 23 Mar 2023 20:10:51 +0100 Subject: [PATCH] Fix missing mock in islamic_prayer_times (#90178) * Fix missing mock in islamic_prayer_times * Restore 100% coverage * Update test_config_flow.py --- .../islamic_prayer_times/conftest.py | 15 +++++++++++ .../islamic_prayer_times/test_config_flow.py | 25 ++++++------------- .../islamic_prayer_times/test_init.py | 21 ++++++++++++++++ 3 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 tests/components/islamic_prayer_times/conftest.py diff --git a/tests/components/islamic_prayer_times/conftest.py b/tests/components/islamic_prayer_times/conftest.py new file mode 100644 index 00000000000..63c6ad8414b --- /dev/null +++ b/tests/components/islamic_prayer_times/conftest.py @@ -0,0 +1,15 @@ +"""Common fixtures for the islamic_prayer_times tests.""" +from collections.abc import Generator +from unittest.mock import AsyncMock, patch + +import pytest + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock, None, None]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.islamic_prayer_times.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + yield mock_setup_entry diff --git a/tests/components/islamic_prayer_times/test_config_flow.py b/tests/components/islamic_prayer_times/test_config_flow.py index 66430938734..a25b8ba0f0b 100644 --- a/tests/components/islamic_prayer_times/test_config_flow.py +++ b/tests/components/islamic_prayer_times/test_config_flow.py @@ -1,15 +1,15 @@ """Tests for Islamic Prayer Times config flow.""" -from unittest.mock import patch +import pytest from homeassistant import config_entries, data_entry_flow from homeassistant.components import islamic_prayer_times from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD, DOMAIN from homeassistant.core import HomeAssistant -from . import PRAYER_TIMES - from tests.common import MockConfigEntry +pytestmark = pytest.mark.usefixtures("mock_setup_entry") + async def test_flow_works(hass: HomeAssistant) -> None: """Test user config.""" @@ -19,13 +19,11 @@ async def test_flow_works(hass: HomeAssistant) -> None: assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["step_id"] == "user" - with patch( - "homeassistant.components.islamic_prayer_times.async_setup_entry", - return_value=True, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={} - ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={} + ) + await hass.async_block_till_done() + assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["title"] == "Islamic Prayer Times" @@ -40,13 +38,6 @@ async def test_options(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - with patch( - "prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times", - return_value=PRAYER_TIMES, - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - result = await hass.config_entries.options.async_init(entry.entry_id) assert result["type"] == data_entry_flow.FlowResultType.FORM diff --git a/tests/components/islamic_prayer_times/test_init.py b/tests/components/islamic_prayer_times/test_init.py index d641a22590d..b1cf8f2c9a5 100644 --- a/tests/components/islamic_prayer_times/test_init.py +++ b/tests/components/islamic_prayer_times/test_init.py @@ -8,6 +8,7 @@ import pytest from homeassistant import config_entries from homeassistant.components import islamic_prayer_times +from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD from homeassistant.core import HomeAssistant from . import ( @@ -85,6 +86,26 @@ async def test_unload_entry(hass: HomeAssistant) -> None: assert islamic_prayer_times.DOMAIN not in hass.data +async def test_options_listener(hass: HomeAssistant) -> None: + """Ensure updating options triggers a coordinator refresh.""" + entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={}) + entry.add_to_hass(hass) + + with patch( + "prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times", + return_value=PRAYER_TIMES, + ) as mock_fetch_prayer_times: + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + assert mock_fetch_prayer_times.call_count == 1 + + hass.config_entries.async_update_entry( + entry, options={CONF_CALC_METHOD: "makkah"} + ) + await hass.async_block_till_done() + assert mock_fetch_prayer_times.call_count == 2 + + async def test_islamic_prayer_times_timestamp_format(hass: HomeAssistant) -> None: """Test Islamic prayer times timestamp format.""" entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={})