Refactor bond unit tests to reduce boilerplate (#38177)
* Refactor bond unit tests to reduce boilerplate * Refactor bond unit tests to reduce boilerplate (PR feedback) * Refactor bond unit tests to reduce boilerplate (PR feedback, nullcontext)
This commit is contained in:
1 parent
34ac4e78af
commit
2d6eb5c05d
3 files changed
+122
-80
No files matched your search
@@ -6,17 +6,11 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .common import patch_bond_device_ids, setup_bond_entity
|
||||
from .common import patch_bond_version, patch_setup_entry, setup_bond_entity
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
def patch_setup_entry(domain: str):
|
||||
"""Patch async_setup_entry for specified domain."""
|
||||
return patch(f"homeassistant.components.bond.{domain}.async_setup_entry")
|
||||
|
||||
|
||||
async def test_async_setup_no_domain_config(hass: HomeAssistant):
|
||||
"""Test setup without configuration is noop."""
|
||||
result = await async_setup_component(hass, DOMAIN, {})
|
||||
@@ -27,29 +21,28 @@ async def test_async_setup_no_domain_config(hass: HomeAssistant):
|
||||
async def test_async_setup_entry_sets_up_hub_and_supported_domains(hass: HomeAssistant):
|
||||
"""Test that configuring entry sets up cover domain."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
|
||||
domain=DOMAIN, data={CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
||||
)
|
||||
|
||||
with patch_bond_device_ids(), patch_setup_entry(
|
||||
"cover"
|
||||
) as mock_cover_async_setup_entry, patch_setup_entry(
|
||||
"fan"
|
||||
) as mock_fan_async_setup_entry, patch_setup_entry(
|
||||
"light"
|
||||
) as mock_light_async_setup_entry, patch_setup_entry(
|
||||
"switch"
|
||||
) as mock_switch_async_setup_entry:
|
||||
result = await setup_bond_entity(
|
||||
hass,
|
||||
config_entry,
|
||||
hub_version={
|
||||
"bondid": "test-bond-id",
|
||||
"target": "test-model",
|
||||
"fw_ver": "test-version",
|
||||
},
|
||||
)
|
||||
assert result is True
|
||||
await hass.async_block_till_done()
|
||||
with patch_bond_version(
|
||||
return_value={
|
||||
"bondid": "test-bond-id",
|
||||
"target": "test-model",
|
||||
"fw_ver": "test-version",
|
||||
}
|
||||
):
|
||||
with patch_setup_entry(
|
||||
"cover"
|
||||
) as mock_cover_async_setup_entry, patch_setup_entry(
|
||||
"fan"
|
||||
) as mock_fan_async_setup_entry, patch_setup_entry(
|
||||
"light"
|
||||
) as mock_light_async_setup_entry, patch_setup_entry(
|
||||
"switch"
|
||||
) as mock_switch_async_setup_entry:
|
||||
result = await setup_bond_entity(hass, config_entry, patch_device_ids=True)
|
||||
assert result is True
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.entry_id in hass.data[DOMAIN]
|
||||
assert config_entry.state == ENTRY_STATE_LOADED
|
||||
@@ -74,15 +67,18 @@ async def test_async_setup_entry_sets_up_hub_and_supported_domains(hass: HomeAss
|
||||
async def test_unload_config_entry(hass: HomeAssistant):
|
||||
"""Test that configuration entry supports unloading."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN, data={CONF_HOST: "1.1.1.1", CONF_ACCESS_TOKEN: "test-token"},
|
||||
domain=DOMAIN, data={CONF_HOST: "some host", CONF_ACCESS_TOKEN: "test-token"},
|
||||
)
|
||||
|
||||
with patch_bond_device_ids(), patch_setup_entry("cover"), patch_setup_entry(
|
||||
"fan"
|
||||
), patch_setup_entry("light"), patch_setup_entry("switch"):
|
||||
result = await setup_bond_entity(hass, config_entry)
|
||||
assert result is True
|
||||
await hass.async_block_till_done()
|
||||
result = await setup_bond_entity(
|
||||
hass,
|
||||
config_entry,
|
||||
patch_version=True,
|
||||
patch_device_ids=True,
|
||||
patch_platforms=True,
|
||||
)
|
||||
assert result is True
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await hass.config_entries.async_unload(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
Reference in new issue
Block a user