Skip to content

Commit

Permalink
Refactor tests for Islamic Prayer Times (#103439)
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
engrbm87 committed Nov 6, 2023
1 parent ab6b3d5 commit f6cb7e1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
14 changes: 7 additions & 7 deletions tests/components/islamic_prayer_times/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
}

NEW_PRAYER_TIMES_TIMESTAMPS = {
"Fajr": datetime(2020, 1, 1, 6, 00, 0, tzinfo=dt_util.UTC),
"Sunrise": datetime(2020, 1, 1, 7, 25, 0, tzinfo=dt_util.UTC),
"Dhuhr": datetime(2020, 1, 1, 12, 30, 0, tzinfo=dt_util.UTC),
"Asr": datetime(2020, 1, 1, 15, 32, 0, tzinfo=dt_util.UTC),
"Maghrib": datetime(2020, 1, 1, 17, 45, 0, tzinfo=dt_util.UTC),
"Isha": datetime(2020, 1, 1, 18, 53, 0, tzinfo=dt_util.UTC),
"Midnight": datetime(2020, 1, 1, 00, 43, 0, tzinfo=dt_util.UTC),
"Fajr": datetime(2020, 1, 2, 6, 00, 0, tzinfo=dt_util.UTC),
"Sunrise": datetime(2020, 1, 2, 7, 25, 0, tzinfo=dt_util.UTC),
"Dhuhr": datetime(2020, 1, 2, 12, 30, 0, tzinfo=dt_util.UTC),
"Asr": datetime(2020, 1, 2, 15, 32, 0, tzinfo=dt_util.UTC),
"Maghrib": datetime(2020, 1, 2, 17, 45, 0, tzinfo=dt_util.UTC),
"Isha": datetime(2020, 1, 2, 18, 53, 0, tzinfo=dt_util.UTC),
"Midnight": datetime(2020, 1, 2, 00, 43, 0, tzinfo=dt_util.UTC),
}

NOW = datetime(2020, 1, 1, 00, 00, 0, tzinfo=dt_util.UTC)
53 changes: 22 additions & 31 deletions tests/components/islamic_prayer_times/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
from homeassistant.components import islamic_prayer_times
from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from . import (
NEW_PRAYER_TIMES,
NEW_PRAYER_TIMES_TIMESTAMPS,
NOW,
PRAYER_TIMES,
PRAYER_TIMES_TIMESTAMPS,
)
from . import NEW_PRAYER_TIMES, NOW, PRAYER_TIMES, PRAYER_TIMES_TIMESTAMPS

from tests.common import MockConfigEntry, async_fire_time_changed

Expand Down Expand Up @@ -85,7 +80,6 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is config_entries.ConfigEntryState.NOT_LOADED
assert islamic_prayer_times.DOMAIN not in hass.data


async def test_options_listener(hass: HomeAssistant) -> None:
Expand All @@ -108,8 +102,8 @@ async def test_options_listener(hass: HomeAssistant) -> None:
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."""
async def test_update_failed(hass: HomeAssistant) -> None:
"""Test integrations tries to update after 1 min if update fails."""
entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={})
entry.add_to_hass(hass)

Expand All @@ -120,33 +114,30 @@ async def test_islamic_prayer_times_timestamp_format(hass: HomeAssistant) -> Non
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert hass.data[islamic_prayer_times.DOMAIN].data == PRAYER_TIMES_TIMESTAMPS


async def test_update(hass: HomeAssistant) -> None:
"""Test sensors are updated with new prayer times."""
entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={})
entry.add_to_hass(hass)
assert entry.state is config_entries.ConfigEntryState.LOADED

with patch(
"prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times"
) as FetchPrayerTimes, freeze_time(NOW):
) as FetchPrayerTimes:
FetchPrayerTimes.side_effect = [
PRAYER_TIMES,
InvalidResponseError,
NEW_PRAYER_TIMES,
]

await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

pt_data = hass.data[islamic_prayer_times.DOMAIN]
assert pt_data.data == PRAYER_TIMES_TIMESTAMPS

future = pt_data.data["Midnight"] + timedelta(days=1, minutes=1)

async_fire_time_changed(hass, future)
await hass.async_block_till_done()
assert pt_data.data == NEW_PRAYER_TIMES_TIMESTAMPS
future = PRAYER_TIMES_TIMESTAMPS["Midnight"] + timedelta(days=1, minutes=1)
with freeze_time(future):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()

state = hass.states.get("sensor.islamic_prayer_times_fajr_prayer")
assert state.state == STATE_UNAVAILABLE

# coordinator tries to update after 1 minute
future = future + timedelta(minutes=1)
with freeze_time(future):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
state = hass.states.get("sensor.islamic_prayer_times_fajr_prayer")
assert state.state == "2020-01-02T06:00:00+00:00"


@pytest.mark.parametrize(
Expand Down

0 comments on commit f6cb7e1

Please sign in to comment.