Skip to content

Commit

Permalink
Simplify time zone setting in tests (home-assistant#68330)
Browse files Browse the repository at this point in the history
* Simplify timezone setting in tests

* Fix typo

* Adjust caldav tests

* Adjust input_datetime tests

* Adjust time_date tests

* Adjust tod tests

* Adjust helper tests

* Adjust recorder tests

* Adjust risco tests

* Adjust aemet tests

* Adjust flux tests

* Adjust forecast_solar tests

* Revert unnecessary change in forecast_solar test

* Adjust climacell tests

* Adjust google tests

* Adjust sensor tests

* Adjust sonarr tests

* Adjust template tests

* Adjust zodiac tests

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
  • Loading branch information
emontnemery and MartinHjelmare authored Mar 20, 2022
1 parent dbeec1f commit cf4033b
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 320 deletions.
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ async def _await_count_and_log_pending(
hass.config.latitude = 32.87336
hass.config.longitude = -117.22743
hass.config.elevation = 0
hass.config.time_zone = "US/Pacific"
hass.config.set_time_zone("US/Pacific")
hass.config.units = METRIC_SYSTEM
hass.config.media_dirs = {"local": get_test_config_dir("media")}
hass.config.skip_pip = True
Expand Down
1 change: 1 addition & 0 deletions tests/components/aemet/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
async def test_aemet_forecast_create_sensors(hass):
"""Test creation of forecast sensors."""

hass.config.set_time_zone("UTC")
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
with patch("homeassistant.util.dt.now", return_value=now), patch(
"homeassistant.util.dt.utcnow", return_value=now
Expand Down
1 change: 1 addition & 0 deletions tests/components/aemet/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
async def test_aemet_weather(hass):
"""Test states of the weather."""

hass.config.set_time_zone("UTC")
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
with patch("homeassistant.util.dt.now", return_value=now), patch(
"homeassistant.util.dt.utcnow", return_value=now
Expand Down
63 changes: 34 additions & 29 deletions tests/components/caldav/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,6 @@
"custom_calendars": [],
}

ORIG_TZ = dt.DEFAULT_TIME_ZONE


@pytest.fixture(autouse=True)
def reset_tz():
"""Restore the default TZ after test runs."""
yield
dt.DEFAULT_TIME_ZONE = ORIG_TZ


@pytest.fixture
def set_tz(request):
Expand All @@ -239,21 +230,21 @@ def set_tz(request):


@pytest.fixture
def utc():
def utc(hass):
"""Set the default TZ to UTC."""
dt.set_default_time_zone(dt.get_time_zone("UTC"))
hass.config.set_time_zone("UTC")


@pytest.fixture
def new_york():
def new_york(hass):
"""Set the default TZ to America/New_York."""
dt.set_default_time_zone(dt.get_time_zone("America/New_York"))
hass.config.set_time_zone("America/New_York")


@pytest.fixture
def baghdad():
def baghdad(hass):
"""Set the default TZ to Asia/Baghdad."""
dt.set_default_time_zone(dt.get_time_zone("Asia/Baghdad"))
hass.config.set_time_zone("Asia/Baghdad")


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -364,8 +355,9 @@ async def test_setup_component_with_one_custom_calendar(hass, mock_dav_client):
assert state.name == "HomeOffice"


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(17, 45))
async def test_ongoing_event(mock_now, hass, calendar):
async def test_ongoing_event(mock_now, hass, calendar, set_tz):
"""Test that the ongoing event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -385,8 +377,9 @@ async def test_ongoing_event(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(17, 30))
async def test_just_ended_event(mock_now, hass, calendar):
async def test_just_ended_event(mock_now, hass, calendar, set_tz):
"""Test that the next ongoing event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -406,8 +399,9 @@ async def test_just_ended_event(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(17, 00))
async def test_ongoing_event_different_tz(mock_now, hass, calendar):
async def test_ongoing_event_different_tz(mock_now, hass, calendar, set_tz):
"""Test that the ongoing event with another timezone is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -427,8 +421,9 @@ async def test_ongoing_event_different_tz(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(19, 10))
async def test_ongoing_floating_event_returned(mock_now, hass, calendar):
async def test_ongoing_floating_event_returned(mock_now, hass, calendar, set_tz):
"""Test that floating events without timezones work."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -448,8 +443,9 @@ async def test_ongoing_floating_event_returned(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(8, 30))
async def test_ongoing_event_with_offset(mock_now, hass, calendar):
async def test_ongoing_event_with_offset(mock_now, hass, calendar, set_tz):
"""Test that the offset is taken into account."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -469,8 +465,9 @@ async def test_ongoing_event_with_offset(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(12, 00))
async def test_matching_filter(mock_now, hass, calendar):
async def test_matching_filter(mock_now, hass, calendar, set_tz):
"""Test that the matching event is returned."""
config = dict(CALDAV_CONFIG)
config["custom_calendars"] = [
Expand All @@ -495,8 +492,9 @@ async def test_matching_filter(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(12, 00))
async def test_matching_filter_real_regexp(mock_now, hass, calendar):
async def test_matching_filter_real_regexp(mock_now, hass, calendar, set_tz):
"""Test that the event matching the regexp is returned."""
config = dict(CALDAV_CONFIG)
config["custom_calendars"] = [
Expand Down Expand Up @@ -625,8 +623,9 @@ async def test_all_day_event_returned_late(hass, calendar, set_tz):
)


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(21, 45))
async def test_event_rrule(mock_now, hass, calendar):
async def test_event_rrule(mock_now, hass, calendar, set_tz):
"""Test that the future recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -646,8 +645,9 @@ async def test_event_rrule(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(22, 15))
async def test_event_rrule_ongoing(mock_now, hass, calendar):
async def test_event_rrule_ongoing(mock_now, hass, calendar, set_tz):
"""Test that the current recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -667,8 +667,9 @@ async def test_event_rrule_ongoing(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(22, 45))
async def test_event_rrule_duration(mock_now, hass, calendar):
async def test_event_rrule_duration(mock_now, hass, calendar, set_tz):
"""Test that the future recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -688,8 +689,9 @@ async def test_event_rrule_duration(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(23, 15))
async def test_event_rrule_duration_ongoing(mock_now, hass, calendar):
async def test_event_rrule_duration_ongoing(mock_now, hass, calendar, set_tz):
"""Test that the ongoing recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -709,8 +711,9 @@ async def test_event_rrule_duration_ongoing(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch("homeassistant.util.dt.now", return_value=_local_datetime(23, 37))
async def test_event_rrule_endless(mock_now, hass, calendar):
async def test_event_rrule_endless(mock_now, hass, calendar, set_tz):
"""Test that the endless recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand Down Expand Up @@ -798,11 +801,12 @@ async def test_event_rrule_all_day_late(hass, calendar, set_tz):
)


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch(
"homeassistant.util.dt.now",
return_value=dt.as_local(datetime.datetime(2015, 11, 27, 0, 15)),
)
async def test_event_rrule_hourly_on_first(mock_now, hass, calendar):
async def test_event_rrule_hourly_on_first(mock_now, hass, calendar, set_tz):
"""Test that the endless recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand All @@ -822,11 +826,12 @@ async def test_event_rrule_hourly_on_first(mock_now, hass, calendar):
}


@pytest.mark.parametrize("set_tz", ["utc"], indirect=True)
@patch(
"homeassistant.util.dt.now",
return_value=dt.as_local(datetime.datetime(2015, 11, 27, 11, 15)),
)
async def test_event_rrule_hourly_on_last(mock_now, hass, calendar):
async def test_event_rrule_hourly_on_last(mock_now, hass, calendar, set_tz):
"""Test that the endless recurring event is returned."""
assert await async_setup_component(hass, "calendar", {"calendar": CALDAV_CONFIG})
await hass.async_block_till_done()
Expand Down
30 changes: 15 additions & 15 deletions tests/components/climacell/test_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,119 +91,119 @@ async def test_v3_weather(
assert weather_state.attributes[ATTR_FORECAST] == [
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_SUNNY,
ATTR_FORECAST_TIME: "2021-03-07T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-07T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 0,
ATTR_FORECAST_TEMP: 7,
ATTR_FORECAST_TEMP_LOW: -5,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-08T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-08T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 0,
ATTR_FORECAST_TEMP: 10,
ATTR_FORECAST_TEMP_LOW: -4,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-09T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-09T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 0,
ATTR_FORECAST_TEMP: 19,
ATTR_FORECAST_TEMP_LOW: 0,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-10T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-10T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 0,
ATTR_FORECAST_TEMP: 18,
ATTR_FORECAST_TEMP_LOW: 3,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-11T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-11T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 5,
ATTR_FORECAST_TEMP: 20,
ATTR_FORECAST_TEMP_LOW: 9,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-12T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-12T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0.0457,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 25,
ATTR_FORECAST_TEMP: 20,
ATTR_FORECAST_TEMP_LOW: 12,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-13T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-13T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 25,
ATTR_FORECAST_TEMP: 16,
ATTR_FORECAST_TEMP_LOW: 7,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_RAINY,
ATTR_FORECAST_TIME: "2021-03-14T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-14T00:00:00-08:00",
ATTR_FORECAST_PRECIPITATION: 1.0744,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 75,
ATTR_FORECAST_TEMP: 6,
ATTR_FORECAST_TEMP_LOW: 3,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_SNOWY,
ATTR_FORECAST_TIME: "2021-03-15T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-15T00:00:00-07:00", # DST starts
ATTR_FORECAST_PRECIPITATION: 7.3050,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 95,
ATTR_FORECAST_TEMP: 1,
ATTR_FORECAST_TEMP_LOW: 0,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-16T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-16T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 0.0051,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 5,
ATTR_FORECAST_TEMP: 6,
ATTR_FORECAST_TEMP_LOW: -2,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-17T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-17T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 0,
ATTR_FORECAST_TEMP: 11,
ATTR_FORECAST_TEMP_LOW: 1,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-18T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-18T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 0,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 5,
ATTR_FORECAST_TEMP: 12,
ATTR_FORECAST_TEMP_LOW: 6,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-19T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-19T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 0.1778,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 45,
ATTR_FORECAST_TEMP: 9,
ATTR_FORECAST_TEMP_LOW: 5,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_RAINY,
ATTR_FORECAST_TIME: "2021-03-20T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-20T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 1.2319,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 55,
ATTR_FORECAST_TEMP: 5,
ATTR_FORECAST_TEMP_LOW: 3,
},
{
ATTR_FORECAST_CONDITION: ATTR_CONDITION_CLOUDY,
ATTR_FORECAST_TIME: "2021-03-21T00:00:00+00:00",
ATTR_FORECAST_TIME: "2021-03-21T00:00:00-07:00",
ATTR_FORECAST_PRECIPITATION: 0.0432,
ATTR_FORECAST_PRECIPITATION_PROBABILITY: 20,
ATTR_FORECAST_TEMP: 7,
Expand Down
2 changes: 0 additions & 2 deletions tests/components/config/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL
from homeassistant.util import dt as dt_util, location

ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE


@pytest.fixture
async def client(hass, hass_ws_client):
Expand Down
6 changes: 6 additions & 0 deletions tests/components/flux/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
)


@pytest.fixture(autouse=True)
def set_utc(hass):
"""Set timezone to UTC."""
hass.config.set_time_zone("UTC")


async def test_valid_config(hass):
"""Test configuration."""
assert await async_setup_component(
Expand Down
7 changes: 5 additions & 2 deletions tests/components/forecast_solar/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ def mock_config_entry() -> MockConfigEntry:


@pytest.fixture
def mock_forecast_solar() -> Generator[None, MagicMock, None]:
"""Return a mocked Forecast.Solar client."""
def mock_forecast_solar(hass) -> Generator[None, MagicMock, None]:
"""Return a mocked Forecast.Solar client.
hass fixture included because it sets the time zone.
"""
with patch(
"homeassistant.components.forecast_solar.ForecastSolar", autospec=True
) as forecast_solar_mock:
Expand Down
Loading

0 comments on commit cf4033b

Please sign in to comment.