2019-08-31 12:30:59 +00:00
|
|
|
"""Tests for the Withings component."""
|
|
|
|
from asynctest import MagicMock
|
2019-10-24 16:41:04 +00:00
|
|
|
|
2019-08-31 12:30:59 +00:00
|
|
|
import pytest
|
2019-10-24 16:41:04 +00:00
|
|
|
from withings_api import WithingsApi
|
|
|
|
from withings_api.common import UnauthorizedException, TimeoutException
|
2019-08-31 12:30:59 +00:00
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-08-31 12:30:59 +00:00
|
|
|
from homeassistant.components.withings.common import (
|
|
|
|
NotAuthenticatedError,
|
|
|
|
WithingsDataManager,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-10 18:22:36 +00:00
|
|
|
@pytest.fixture(name="withings_api")
|
2019-10-24 16:41:04 +00:00
|
|
|
def withings_api_fixture() -> WithingsApi:
|
2019-10-10 18:22:36 +00:00
|
|
|
"""Provide withings api."""
|
2019-10-24 16:41:04 +00:00
|
|
|
withings_api = WithingsApi.__new__(WithingsApi)
|
2019-10-10 18:22:36 +00:00
|
|
|
withings_api.get_measures = MagicMock()
|
|
|
|
withings_api.get_sleep = MagicMock()
|
|
|
|
return withings_api
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="data_manager")
|
2019-10-24 16:41:04 +00:00
|
|
|
def data_manager_fixture(hass, withings_api: WithingsApi) -> WithingsDataManager:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Provide data manager."""
|
2019-10-10 18:22:36 +00:00
|
|
|
return WithingsDataManager(hass, "My Profile", withings_api)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
def test_print_service() -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
# Go from None to True
|
|
|
|
WithingsDataManager.service_available = None
|
|
|
|
assert WithingsDataManager.print_service_available()
|
|
|
|
assert WithingsDataManager.service_available is True
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
|
|
|
|
# Go from True to False
|
|
|
|
assert WithingsDataManager.print_service_unavailable()
|
|
|
|
assert WithingsDataManager.service_available is False
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
|
|
|
|
# Go from False to True
|
|
|
|
assert WithingsDataManager.print_service_available()
|
|
|
|
assert WithingsDataManager.service_available is True
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
assert not WithingsDataManager.print_service_available()
|
|
|
|
|
|
|
|
# Go from Non to False
|
|
|
|
WithingsDataManager.service_available = None
|
|
|
|
assert WithingsDataManager.print_service_unavailable()
|
|
|
|
assert WithingsDataManager.service_available is False
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
assert not WithingsDataManager.print_service_unavailable()
|
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call(data_manager: WithingsDataManager) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
# Not authenticated 1.
|
2019-10-24 16:41:04 +00:00
|
|
|
test_function = MagicMock(side_effect=UnauthorizedException(401))
|
|
|
|
with pytest.raises(NotAuthenticatedError):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
# Not authenticated 2.
|
2019-10-24 16:41:04 +00:00
|
|
|
test_function = MagicMock(side_effect=TimeoutException(522))
|
|
|
|
with pytest.raises(PlatformNotReady):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
# Service error.
|
|
|
|
test_function = MagicMock(side_effect=PlatformNotReady())
|
2019-10-24 16:41:04 +00:00
|
|
|
with pytest.raises(PlatformNotReady):
|
|
|
|
await data_manager.call(test_function)
|
2019-08-31 12:30:59 +00:00
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call_throttle_enabled(
|
|
|
|
data_manager: WithingsDataManager
|
|
|
|
) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
hello_func = MagicMock(return_value="HELLO2")
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func, throttle_domain="test")
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func, throttle_domain="test")
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
assert hello_func.call_count == 1
|
|
|
|
|
|
|
|
|
2019-10-24 16:41:04 +00:00
|
|
|
async def test_data_manager_call_throttle_disabled(
|
|
|
|
data_manager: WithingsDataManager
|
|
|
|
) -> None:
|
2019-08-31 12:30:59 +00:00
|
|
|
"""Test method."""
|
|
|
|
hello_func = MagicMock(return_value="HELLO2")
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func)
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
result = await data_manager.call(hello_func)
|
|
|
|
assert result == "HELLO2"
|
|
|
|
|
|
|
|
assert hello_func.call_count == 2
|