WIP: Unit tests, pass 2

This commit is contained in:
puddly
2025-05-12 16:44:41 -04:00
parent 2b35ad535f
commit b993b1ee97
2 changed files with 12 additions and 264 deletions

View File

@@ -234,7 +234,7 @@ async def test_config_flow_zigbee(hass: HomeAssistant) -> None:
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
):
# Pick the menu option: we are now installing the addon
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -301,23 +301,7 @@ async def test_config_flow_zigbee_skip_step_if_installed(hass: HomeAssistant) ->
assert result["type"] is FlowResultType.MENU
assert result["step_id"] == "pick_firmware"
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
flasher_addon_info=AddonInfo(
available=True,
hostname=None,
options={
"device": "",
"baudrate": 115200,
"bootloader_baudrate": 115200,
"flow_control": True,
},
state=AddonState.NOT_RUNNING,
update_available=False,
version="1.2.3",
),
) as mock_otbr_manager:
with mock_addon_info(hass, app_type=ApplicationType.SPINEL):
# Pick the menu option: we skip installation, instead we directly run it
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -328,16 +312,6 @@ async def test_config_flow_zigbee_skip_step_if_installed(hass: HomeAssistant) ->
assert result["step_id"] == "run_zigbee_flasher_addon"
assert result["progress_action"] == "run_zigbee_flasher_addon"
assert result["description_placeholders"]["firmware_type"] == "spinel"
assert mock_flasher_manager.async_set_addon_options.mock_calls == [
call(
{
"device": TEST_DEVICE,
"baudrate": 115200,
"bootloader_baudrate": 115200,
"flow_control": True,
}
)
]
# Uninstall the addon
await hass.async_block_till_done(wait_background_tasks=True)
@@ -528,7 +502,7 @@ async def test_config_flow_zigbee_not_hassio(hass: HomeAssistant) -> None:
hass,
is_hassio=False,
app_type=ApplicationType.EZSP,
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
@@ -676,7 +650,7 @@ async def test_options_flow_thread_to_zigbee(hass: HomeAssistant) -> None:
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
):
# Pick the menu option: we are now installing the addon
result = await hass.config_entries.options.async_configure(
result["flow_id"],
@@ -693,16 +667,6 @@ async def test_options_flow_thread_to_zigbee(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "run_zigbee_flasher_addon"
assert result["progress_action"] == "run_zigbee_flasher_addon"
assert mock_flasher_manager.async_set_addon_options.mock_calls == [
call(
{
"device": TEST_DEVICE,
"baudrate": 115200,
"bootloader_baudrate": 115200,
"flow_control": True,
}
)
]
await hass.async_block_till_done(wait_background_tasks=True)
@@ -714,9 +678,6 @@ async def test_options_flow_thread_to_zigbee(hass: HomeAssistant) -> None:
await hass.async_block_till_done(wait_background_tasks=True)
# We are finally done with the addon
assert mock_flasher_manager.async_uninstall_addon_waiting.mock_calls == [call()]
result = await hass.config_entries.options.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "confirm_zigbee"

View File

@@ -21,7 +21,6 @@ from .test_config_flow import (
TEST_DEVICE,
TEST_DOMAIN,
TEST_HARDWARE_NAME,
delayed_side_effect,
mock_addon_info,
mock_test_firmware_platform, # noqa: F401
)
@@ -54,7 +53,7 @@ async def test_config_flow_cannot_probe_firmware(
with mock_addon_info(
hass,
app_type=None,
) as mock_otbr_manager:
):
# Start the flow
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
@@ -85,7 +84,7 @@ async def test_config_flow_zigbee_not_hassio_wrong_firmware(
hass,
app_type=ApplicationType.SPINEL,
is_hassio=False,
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@@ -98,218 +97,6 @@ async def test_config_flow_zigbee_not_hassio_wrong_firmware(
assert result["reason"] == "not_hassio"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
)
async def test_config_flow_zigbee_flasher_addon_already_running(
hass: HomeAssistant,
) -> None:
"""Test failure case when flasher addon is already running."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
flasher_addon_info=AddonInfo(
available=True,
hostname=None,
options={},
state=AddonState.RUNNING,
update_available=False,
version="1.0.0",
),
) as mock_otbr_manager:
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
# Cannot get addon info
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "addon_already_running"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
)
async def test_config_flow_zigbee_flasher_addon_info_fails(hass: HomeAssistant) -> None:
"""Test failure case when flasher addon cannot be installed."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
flasher_addon_info=AddonInfo(
available=True,
hostname=None,
options={},
state=AddonState.RUNNING,
update_available=False,
version="1.0.0",
),
) as mock_otbr_manager:
mock_flasher_manager.async_get_addon_info.side_effect = AddonError()
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
# Cannot get addon info
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "addon_info_failed"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
)
async def test_config_flow_zigbee_flasher_addon_install_fails(
hass: HomeAssistant,
) -> None:
"""Test failure case when flasher addon cannot be installed."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
mock_flasher_manager.async_install_addon_waiting = AsyncMock(
side_effect=AddonError()
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
# Cannot install addon
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "addon_install_failed"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
)
async def test_config_flow_zigbee_flasher_addon_set_config_fails(
hass: HomeAssistant,
) -> None:
"""Test failure case when flasher addon cannot be configured."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
mock_flasher_manager.async_install_addon_waiting = AsyncMock(
side_effect=delayed_side_effect()
)
mock_flasher_manager.async_set_addon_options = AsyncMock(
side_effect=AddonError()
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done(wait_background_tasks=True)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "addon_set_config_failed"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
)
async def test_config_flow_zigbee_flasher_run_fails(hass: HomeAssistant) -> None:
"""Test failure case when flasher addon fails to run."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
mock_flasher_manager.async_start_addon_waiting = AsyncMock(
side_effect=AddonError()
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done(wait_background_tasks=True)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "addon_start_failed"
async def test_config_flow_zigbee_flasher_uninstall_fails(hass: HomeAssistant) -> None:
"""Test failure case when flasher addon uninstall fails."""
result = await hass.config_entries.flow.async_init(
TEST_DOMAIN, context={"source": "hardware"}
)
with mock_addon_info(
hass,
app_type=ApplicationType.SPINEL,
) as mock_otbr_manager:
mock_flasher_manager.async_uninstall_addon_waiting = AsyncMock(
side_effect=AddonError()
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},
)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done(wait_background_tasks=True)
result = await hass.config_entries.flow.async_configure(result["flow_id"])
await hass.async_block_till_done(wait_background_tasks=True)
# Uninstall failure isn't critical
result = await hass.config_entries.flow.async_configure(result["flow_id"])
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "confirm_zigbee"
@pytest.mark.parametrize(
"ignore_translations_for_mock_domains",
["test_firmware_domain"],
@@ -326,7 +113,7 @@ async def test_config_flow_zigbee_confirmation_fails(hass: HomeAssistant) -> Non
with mock_addon_info(
hass,
app_type=ApplicationType.EZSP,
) as mock_otbr_manager:
):
# Pick the menu option: we are now installing the addon
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -338,7 +125,7 @@ async def test_config_flow_zigbee_confirmation_fails(hass: HomeAssistant) -> Non
with mock_addon_info(
hass,
app_type=None, # Probing fails
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@@ -360,7 +147,7 @@ async def test_config_flow_thread_not_hassio(hass: HomeAssistant) -> None:
hass,
is_hassio=False,
app_type=ApplicationType.EZSP,
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@@ -580,7 +367,7 @@ async def test_config_flow_thread_confirmation_fails(hass: HomeAssistant) -> Non
with mock_addon_info(
hass,
app_type=ApplicationType.EZSP,
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@@ -601,7 +388,7 @@ async def test_config_flow_thread_confirmation_fails(hass: HomeAssistant) -> Non
with mock_addon_info(
hass,
app_type=None, # Probing fails
) as mock_otbr_manager:
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
@@ -694,7 +481,7 @@ async def test_options_flow_thread_to_zigbee_otbr_configured(
update_available=False,
version="1.0.0",
),
) as mock_otbr_manager:
):
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE},