Skip to content

Commit

Permalink
fix: add tests for config flow
Browse files Browse the repository at this point in the history
muhlba91 committed Dec 15, 2023
1 parent 2d1aaaf commit c43a8e6
Showing 2 changed files with 96 additions and 5 deletions.
6 changes: 3 additions & 3 deletions custom_components/hella_onyx/config_flow.py
Original file line number Diff line number Diff line change
@@ -99,9 +99,9 @@ async def async_step_init(self, user_input, is_import=False):
@callback
def async_get_options_flow(
config_entry: ConfigEntry,
) -> "OptionsFlowHandler":
) -> "OnyxOptionsFlowHandler":
"""Get the options flow for this handler."""
return OptionsFlowHandler(config_entry)
return OnyxOptionsFlowHandler(config_entry)

async def _async_exists(self, fingerprint):
"""Check if the endpoint exists already."""
@@ -120,7 +120,7 @@ async def _async_verify_conn(self, fingerprint, token):
).verify()


class OptionsFlowHandler(OptionsFlow):
class OnyxOptionsFlowHandler(OptionsFlow):
"""Handle a option flow for ONYX."""

def __init__(self, config_entry: ConfigEntry) -> None:
95 changes: 93 additions & 2 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test for the ONYX Config Flow."""
import pytest

from unittest.mock import patch
from unittest.mock import patch, MagicMock

from homeassistant.const import (
CONF_ACCESS_TOKEN,
@@ -14,7 +14,10 @@
CONF_MIN_DIM_DURATION,
CONF_MAX_DIM_DURATION,
)
from custom_components.hella_onyx.config_flow import OnyxFlowHandler
from custom_components.hella_onyx.config_flow import (
OnyxFlowHandler,
OnyxOptionsFlowHandler,
)


class TestOnyxFlowHandler:
@@ -118,3 +121,91 @@ async def test_async_step_init_with_data(
assert mock_async_verify_conn.called
assert not mock_async_abort.called
assert mock_async_create_entry.called

@patch(
"custom_components.hella_onyx.config_flow.OnyxFlowHandler._async_current_entries"
)
@pytest.mark.asyncio
async def test_async_exists(
self,
mock_async_current_entries,
):
entry = MagicMock()
entry.data = {"fingerprint": "finger"}
mock_async_current_entries.return_value = [entry]

config_flow = OnyxFlowHandler()
exists = await config_flow._async_exists("finger")
assert exists
assert mock_async_current_entries.called

@patch(
"custom_components.hella_onyx.config_flow.OnyxFlowHandler._async_current_entries"
)
@pytest.mark.asyncio
async def test_async_exists_none(
self,
mock_async_current_entries,
):
mock_async_current_entries.return_value = []

config_flow = OnyxFlowHandler()
exists = await config_flow._async_exists("finger")
assert not exists
assert mock_async_current_entries.called

@patch("onyx_client.client.OnyxClient.verify")
@pytest.mark.asyncio
async def test_async_verify_conn(
self,
mock_verify,
):
config_flow = OnyxFlowHandler()
config_flow.hass = MagicMock()
await config_flow._async_verify_conn("finger", "token")
assert mock_verify.called


class TestOnyxOptionsFlowHandler:
@pytest.mark.asyncio
async def test_async_step_init_without_data(
self,
):
entry = MagicMock()
value = {
CONF_SCAN_INTERVAL: 10,
CONF_MIN_DIM_DURATION: 0,
CONF_MAX_DIM_DURATION: 100,
CONF_FORCE_UPDATE: False,
}
entry.options.return_value = value
options_flow = OnyxOptionsFlowHandler(entry)
form = await options_flow.async_step_init()
assert form is not None
assert "title" not in form
assert "min_dim_duration" in form["data_schema"].schema
assert "scan_interval" in form["data_schema"].schema
assert "force_update" in form["data_schema"].schema

@pytest.mark.asyncio
async def test_async_step_init_with_data(
self,
):
entry = MagicMock()
entry.options.return_value = {
CONF_SCAN_INTERVAL: 10,
CONF_MIN_DIM_DURATION: 0,
CONF_MAX_DIM_DURATION: 100,
CONF_FORCE_UPDATE: False,
}
options_flow = OnyxOptionsFlowHandler(entry)
user_input = {
CONF_SCAN_INTERVAL: 100,
CONF_MIN_DIM_DURATION: 10,
CONF_MAX_DIM_DURATION: 10,
CONF_FORCE_UPDATE: False,
}
form = await options_flow.async_step_init(user_input)
assert form is not None
assert form["title"] == ""
assert form["data"] == user_input

0 comments on commit c43a8e6

Please sign in to comment.