mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Improve SFR Box test coverage (#85054)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
3737170d37
commit
34798189ca
@ -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
|
||||
|
@ -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
|
||||
|
15
tests/components/sfr_box/fixtures/dsl_getInfo.json
Normal file
15
tests/components/sfr_box/fixtures/dsl_getInfo.json
Normal 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"
|
||||
}
|
50
tests/components/sfr_box/test_init.py
Normal file
50
tests/components/sfr_box/test_init.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user