Refactor tests for Islamic Prayer Times (#103439)

Refactor tests
This commit is contained in:
Rami Mosleh 2023-11-06 05:07:57 +02:00 committed by GitHub
parent ab6b3d5668
commit f6cb7e1bc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 36 deletions

View File

@ -35,13 +35,13 @@ NEW_PRAYER_TIMES = {
} }
NEW_PRAYER_TIMES_TIMESTAMPS = { NEW_PRAYER_TIMES_TIMESTAMPS = {
"Fajr": datetime(2020, 1, 1, 6, 00, 0, tzinfo=dt_util.UTC), "Fajr": datetime(2020, 1, 2, 6, 00, 0, tzinfo=dt_util.UTC),
"Sunrise": datetime(2020, 1, 1, 7, 25, 0, tzinfo=dt_util.UTC), "Sunrise": datetime(2020, 1, 2, 7, 25, 0, tzinfo=dt_util.UTC),
"Dhuhr": datetime(2020, 1, 1, 12, 30, 0, tzinfo=dt_util.UTC), "Dhuhr": datetime(2020, 1, 2, 12, 30, 0, tzinfo=dt_util.UTC),
"Asr": datetime(2020, 1, 1, 15, 32, 0, tzinfo=dt_util.UTC), "Asr": datetime(2020, 1, 2, 15, 32, 0, tzinfo=dt_util.UTC),
"Maghrib": datetime(2020, 1, 1, 17, 45, 0, tzinfo=dt_util.UTC), "Maghrib": datetime(2020, 1, 2, 17, 45, 0, tzinfo=dt_util.UTC),
"Isha": datetime(2020, 1, 1, 18, 53, 0, tzinfo=dt_util.UTC), "Isha": datetime(2020, 1, 2, 18, 53, 0, tzinfo=dt_util.UTC),
"Midnight": datetime(2020, 1, 1, 00, 43, 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) NOW = datetime(2020, 1, 1, 00, 00, 0, tzinfo=dt_util.UTC)

View File

@ -10,16 +10,11 @@ from homeassistant import config_entries
from homeassistant.components import islamic_prayer_times from homeassistant.components import islamic_prayer_times
from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD from homeassistant.components.islamic_prayer_times.const import CONF_CALC_METHOD
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from . import ( from . import NEW_PRAYER_TIMES, NOW, PRAYER_TIMES, PRAYER_TIMES_TIMESTAMPS
NEW_PRAYER_TIMES,
NEW_PRAYER_TIMES_TIMESTAMPS,
NOW,
PRAYER_TIMES,
PRAYER_TIMES_TIMESTAMPS,
)
from tests.common import MockConfigEntry, async_fire_time_changed from tests.common import MockConfigEntry, async_fire_time_changed
@ -85,7 +80,6 @@ async def test_unload_entry(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entry.state is config_entries.ConfigEntryState.NOT_LOADED 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: async def test_options_listener(hass: HomeAssistant) -> None:
@ -108,8 +102,8 @@ async def test_options_listener(hass: HomeAssistant) -> None:
assert mock_fetch_prayer_times.call_count == 2 assert mock_fetch_prayer_times.call_count == 2
async def test_islamic_prayer_times_timestamp_format(hass: HomeAssistant) -> None: async def test_update_failed(hass: HomeAssistant) -> None:
"""Test Islamic prayer times timestamp format.""" """Test integrations tries to update after 1 min if update fails."""
entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={}) entry = MockConfigEntry(domain=islamic_prayer_times.DOMAIN, data={})
entry.add_to_hass(hass) entry.add_to_hass(hass)
@ -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.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.data[islamic_prayer_times.DOMAIN].data == PRAYER_TIMES_TIMESTAMPS assert entry.state is config_entries.ConfigEntryState.LOADED
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)
with patch( with patch(
"prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times" "prayer_times_calculator.PrayerTimesCalculator.fetch_prayer_times"
) as FetchPrayerTimes, freeze_time(NOW): ) as FetchPrayerTimes:
FetchPrayerTimes.side_effect = [ FetchPrayerTimes.side_effect = [
PRAYER_TIMES, InvalidResponseError,
NEW_PRAYER_TIMES, NEW_PRAYER_TIMES,
] ]
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()
await hass.config_entries.async_setup(entry.entry_id) state = hass.states.get("sensor.islamic_prayer_times_fajr_prayer")
await hass.async_block_till_done() assert state.state == STATE_UNAVAILABLE
pt_data = hass.data[islamic_prayer_times.DOMAIN] # coordinator tries to update after 1 minute
assert pt_data.data == PRAYER_TIMES_TIMESTAMPS future = future + timedelta(minutes=1)
with freeze_time(future):
future = pt_data.data["Midnight"] + timedelta(days=1, minutes=1) async_fire_time_changed(hass, future)
await hass.async_block_till_done()
async_fire_time_changed(hass, future) state = hass.states.get("sensor.islamic_prayer_times_fajr_prayer")
await hass.async_block_till_done() assert state.state == "2020-01-02T06:00:00+00:00"
assert pt_data.data == NEW_PRAYER_TIMES_TIMESTAMPS
@pytest.mark.parametrize( @pytest.mark.parametrize(