Improve Withings tests in different time zone (#30326)

* Improve Withings tests in different time zone

* Address code review comment

* Spelling error in code doc
This commit is contained in:
Franck Nijhof 2019-12-31 17:10:39 +01:00 committed by David F. Mulcahey
parent a3061bda60
commit 2620a95944

View File

@ -1,5 +1,6 @@
"""Tests for the Withings component.""" """Tests for the Withings component."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import patch
from asynctest import MagicMock from asynctest import MagicMock
import pytest import pytest
@ -10,18 +11,10 @@ from homeassistant.components.withings.common import (
NotAuthenticatedError, NotAuthenticatedError,
WithingsDataManager, WithingsDataManager,
) )
from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
from homeassistant.util import dt from homeassistant.util import dt
DEFAULT_TIME_ZONE = dt.DEFAULT_TIME_ZONE
def teardown():
"""Ensure the time zone is reverted after tests finish."""
dt.set_default_time_zone(DEFAULT_TIME_ZONE)
@pytest.fixture(name="withings_api") @pytest.fixture(name="withings_api")
def withings_api_fixture() -> WithingsApi: def withings_api_fixture() -> WithingsApi:
@ -34,6 +27,17 @@ def withings_api_fixture() -> WithingsApi:
return withings_api return withings_api
@pytest.fixture
def mock_time_zone():
"""Provide an alternative time zone."""
patch_time_zone = patch(
"homeassistant.util.dt.DEFAULT_TIME_ZONE",
new=dt.get_time_zone("America/Los_Angeles"),
)
with patch_time_zone:
yield
@pytest.fixture(name="data_manager") @pytest.fixture(name="data_manager")
def data_manager_fixture(hass, withings_api: WithingsApi) -> WithingsDataManager: def data_manager_fixture(hass, withings_api: WithingsApi) -> WithingsDataManager:
"""Provide data manager.""" """Provide data manager."""
@ -118,13 +122,9 @@ async def test_data_manager_call_throttle_disabled(
async def test_data_manager_update_sleep_date_range( async def test_data_manager_update_sleep_date_range(
hass: HomeAssistant, data_manager: WithingsDataManager, hass: HomeAssistant, data_manager: WithingsDataManager, mock_time_zone
) -> None: ) -> None:
"""Test method.""" """Test method."""
await async_process_ha_core_config(
hass=hass, config={"time_zone": "America/Los_Angeles"}
)
update_start_time = dt.now() update_start_time = dt.now()
await data_manager.update_sleep() await data_manager.update_sleep()