Add first set of tests to devolo Home Control integration (#42527)

* Add first two testcases

* Remove repetition

* Add first two testcases

* Remove repetition

* Add connection error test case

* add test_setup_entry_credentials_valid

* First attempt to use fixtures

* Use markers

* Optimize patch

* Optimize marker use

* Always patch mydevolo

* Add first two testcases

* Remove repetition

* Add first two testcases

* Remove repetition

* Add connection error test case

* add test_setup_entry_credentials_valid

* First attempt to use fixtures

* Use markers

* Optimize patch

* Optimize marker use

* Always patch mydevolo

* Add unload entry test case

* Catch up with reality

* Use unittest patch

* Use core interface to start tests

* Use entry state

* Consistently assert entry state

* Patch class instead of init

Co-authored-by: Markus Bong <2Fake1987@gmail.com>
This commit is contained in:
Guido Schmitz 2021-01-28 17:14:33 +01:00 committed by GitHub
parent 0da4034179
commit 8065ece0bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 1 deletions

View File

@ -172,7 +172,6 @@ omit =
homeassistant/components/denonavr/media_player.py
homeassistant/components/denonavr/receiver.py
homeassistant/components/deutsche_bahn/sensor.py
homeassistant/components/devolo_home_control/__init__.py
homeassistant/components/devolo_home_control/binary_sensor.py
homeassistant/components/devolo_home_control/climate.py
homeassistant/components/devolo_home_control/const.py

View File

@ -1 +1,19 @@
"""Tests for the devolo_home_control integration."""
from homeassistant.components.devolo_home_control.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
def configure_integration(hass: HomeAssistant) -> MockConfigEntry:
"""Configure the integration."""
config = {
"username": "test-username",
"password": "test-password",
"mydevolo_url": "https://test_mydevolo_url.test",
}
entry = MockConfigEntry(domain=DOMAIN, data=config, unique_id="123456")
entry.add_to_hass(hass)
return entry

View File

@ -0,0 +1,33 @@
"""Fixtures for tests."""
from unittest.mock import patch
import pytest
def pytest_configure(config):
"""Define custom markers."""
config.addinivalue_line(
"markers",
"credentials_invalid: Treat credentials as invalid.",
)
config.addinivalue_line(
"markers",
"maintenance: Set maintenance mode to on.",
)
@pytest.fixture(autouse=True)
def patch_mydevolo(request):
"""Fixture to patch mydevolo into a desired state."""
with patch(
"homeassistant.components.devolo_home_control.Mydevolo.credentials_valid",
return_value=not bool(request.node.get_closest_marker("credentials_invalid")),
), patch(
"homeassistant.components.devolo_home_control.Mydevolo.maintenance",
return_value=bool(request.node.get_closest_marker("maintenance")),
), patch(
"homeassistant.components.devolo_home_control.Mydevolo.get_gateway_ids",
return_value=["1400000000000001", "1400000000000002"],
):
yield

View File

@ -0,0 +1,71 @@
"""Tests for the devolo Home Control integration."""
from unittest.mock import patch
from devolo_home_control_api.exceptions.gateway import GatewayOfflineError
import pytest
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
ENTRY_STATE_NOT_LOADED,
ENTRY_STATE_SETUP_ERROR,
ENTRY_STATE_SETUP_RETRY,
)
from homeassistant.core import HomeAssistant
from tests.components.devolo_home_control import configure_integration
async def test_setup_entry(hass: HomeAssistant):
"""Test setup entry."""
entry = configure_integration(hass)
with patch("homeassistant.components.devolo_home_control.HomeControl"):
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_LOADED
@pytest.mark.credentials_invalid
async def test_setup_entry_credentials_invalid(hass: HomeAssistant):
"""Test setup entry fails if credentials are invalid."""
entry = configure_integration(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_ERROR
@pytest.mark.maintenance
async def test_setup_entry_maintenance(hass: HomeAssistant):
"""Test setup entry fails if mydevolo is in maintenance mode."""
entry = configure_integration(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_setup_connection_error(hass: HomeAssistant):
"""Test setup entry fails on connection error."""
entry = configure_integration(hass)
with patch(
"homeassistant.components.devolo_home_control.HomeControl",
side_effect=ConnectionError,
):
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_setup_gateway_offline(hass: HomeAssistant):
"""Test setup entry fails on gateway offline."""
entry = configure_integration(hass)
with patch(
"homeassistant.components.devolo_home_control.HomeControl",
side_effect=GatewayOfflineError,
):
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_unload_entry(hass: HomeAssistant):
"""Test unload entry."""
entry = configure_integration(hass)
with patch("homeassistant.components.devolo_home_control.HomeControl"):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
await hass.config_entries.async_unload(entry.entry_id)
assert entry.state == ENTRY_STATE_NOT_LOADED