Improve SFR Box test coverage (#85054)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
epenet 2023-01-03 17:56:30 +01:00 committed by GitHub
parent 3737170d37
commit 34798189ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 3 deletions

View File

@ -1117,8 +1117,6 @@ omit =
homeassistant/components/sesame/lock.py
homeassistant/components/seven_segments/image_processing.py
homeassistant/components/seventeentrack/sensor.py
homeassistant/components/sfr_box/__init__.py
homeassistant/components/sfr_box/coordinator.py
homeassistant/components/sfr_box/sensor.py
homeassistant/components/shiftr/*
homeassistant/components/shodan/sensor.py

View File

@ -1,12 +1,17 @@
"""Provide common SFR Box fixtures."""
from collections.abc import Generator
import json
from unittest.mock import patch
import pytest
from sfrbox_api.models import DslInfo, SystemInfo
from homeassistant.components.sfr_box.const import DOMAIN
from homeassistant.config_entries import SOURCE_USER, ConfigEntry
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, load_fixture
@pytest.fixture(name="config_entry")
@ -22,3 +27,25 @@ def get_config_entry(hass: HomeAssistant) -> ConfigEntry:
)
config_entry.add_to_hass(hass)
return config_entry
@pytest.fixture()
def system_get_info() -> Generator[SystemInfo, None, None]:
"""Fixture for SFRBox.system_get_info."""
system_info = SystemInfo(**json.loads(load_fixture("system_getInfo.json", DOMAIN)))
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.system_get_info",
return_value=system_info,
):
yield system_info
@pytest.fixture()
def dsl_get_info() -> Generator[DslInfo, None, None]:
"""Fixture for SFRBox.dsl_get_info."""
dsl_info = DslInfo(**json.loads(load_fixture("dsl_getInfo.json", DOMAIN)))
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.dsl_get_info",
return_value=dsl_info,
):
yield dsl_info

View File

@ -0,0 +1,15 @@
{
"linemode": "ADSL2+",
"uptime": 450796,
"counter": 16,
"crc": 0,
"status": "up",
"noise_down": 5.8,
"noise_up": 6.0,
"attenuation_down": 28.5,
"attenuation_up": 20.8,
"rate_down": 5549,
"rate_up": 187,
"line_status": "No Defect",
"training": "Showtime"
}

View File

@ -0,0 +1,50 @@
"""Test the SFR Box setup process."""
from collections.abc import Generator
from unittest.mock import patch
import pytest
from sfrbox_api.exceptions import SFRBoxError
from homeassistant.components.sfr_box.const import DOMAIN
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.core import HomeAssistant
@pytest.fixture(autouse=True)
def override_platforms() -> Generator[None, None, None]:
"""Override PLATFORMS."""
with patch("homeassistant.components.sfr_box.PLATFORMS", []):
yield
@pytest.mark.usefixtures("system_get_info", "dsl_get_info")
async def test_setup_unload_entry(
hass: HomeAssistant, config_entry: ConfigEntry
) -> None:
"""Test entry setup and unload."""
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.LOADED
# Unload the entry and verify that the data has been removed
await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.NOT_LOADED
async def test_setup_entry_exception(
hass: HomeAssistant, config_entry: ConfigEntry
) -> None:
"""Test ConfigEntryNotReady when API raises an exception during entry setup."""
with patch(
"homeassistant.components.sfr_box.coordinator.SFRBox.system_get_info",
side_effect=SFRBoxError,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert config_entry.state is ConfigEntryState.SETUP_RETRY
assert not hass.data.get(DOMAIN)