Skip to content

Commit

Permalink
chore (tests): Update tests for new config_flow
Browse files Browse the repository at this point in the history
  • Loading branch information
agittins committed May 18, 2024
1 parent 7908bb2 commit 6d500c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
18 changes: 9 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import pytest

from .const import SERVICE_INFOS

# from custom_components.bermuda import BermudaDataUpdateCoordinator


Expand Down Expand Up @@ -65,10 +63,12 @@ def error_get_data_fixture():
yield


# This fixture ensures that the config flow gets service info for the anticipated address
# to go into configured_devices
@pytest.fixture(autouse=True)
def mock_service_info():
"""Simulate a discovered advertisement for config_flow"""
with patch("custom_components.bermuda.bluetooth.async_discovered_service_info"):
return SERVICE_INFOS
# 2024-05-18: No longer required as config_flow no longer accesses the bluetooth platform,
# instead pulling data from the dataupdatecoordinator.
# # This fixture ensures that the config flow gets service info for the anticipated address
# # to go into configured_devices
# @pytest.fixture(autouse=True)
# def mock_service_info():
# """Simulate a discovered advertisement for config_flow"""
# with patch("custom_components.bermuda.bluetooth.async_discovered_service_info"):
# return SERVICE_INFOS
14 changes: 14 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
custom_components.bermuda.const.CONF_DEVICES: [], # ["EE:E8:37:9F:6B:54"],
}

MOCK_OPTIONS_GLOBALS = {
custom_components.bermuda.const.CONF_MAX_RADIUS: 20.0,
custom_components.bermuda.const.CONF_MAX_VELOCITY: 3.0,
custom_components.bermuda.const.CONF_DEVTRACK_TIMEOUT: 30,
custom_components.bermuda.const.CONF_UPDATE_INTERVAL: 10.0,
custom_components.bermuda.const.CONF_SMOOTHING_SAMPLES: 20,
custom_components.bermuda.const.CONF_ATTENUATION: 3.0,
custom_components.bermuda.const.CONF_REF_POWER: -55.0,
}

MOCK_OPTIONS_DEVICES = {
custom_components.bermuda.const.CONF_DEVICES: [], # ["EE:E8:37:9F:6B:54"],
}

MOCK_CONFIG = {"source": "user"}


Expand Down
29 changes: 20 additions & 9 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import pytest
from homeassistant import config_entries
from homeassistant import data_entry_flow
from homeassistant.core import HomeAssistant # noqa: F401

# from homeassistant.core import HomeAssistant # noqa: F401
from homeassistant.data_entry_flow import FlowResultType
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.bermuda.const import DOMAIN
from custom_components.bermuda.const import NAME

# from .const import MOCK_OPTIONS
from .const import MOCK_CONFIG
from .const import MOCK_OPTIONS
from .const import MOCK_OPTIONS_GLOBALS


# This fixture bypasses the actual setup of the integration
Expand Down Expand Up @@ -44,7 +47,7 @@ async def test_successful_config_flow(hass, bypass_get_data):
)

# Check that the config flow shows the user form as the first step
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"

# If a user were to enter `test_username` for username and `test_password`
Expand All @@ -55,7 +58,7 @@ async def test_successful_config_flow(hass, bypass_get_data):

# Check that the config flow is complete and a new entry is created with
# the input data
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == NAME
assert result["data"] == {"source": "user"}
assert result["options"] == {}
Expand All @@ -73,7 +76,7 @@ async def test_failed_config_flow(hass, error_on_get_data):
DOMAIN, context={"source": config_entries.SOURCE_USER}
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"

result = await hass.config_entries.flow.async_configure(
Expand All @@ -97,18 +100,26 @@ async def test_options_flow(hass):
result = await hass.config_entries.options.async_init(entry.entry_id)

# Verify that the first options step is a user form
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["type"] == FlowResultType.MENU
assert result["step_id"] == "init"

# select the globalopts menu option
result = await hass.config_entries.options.async_configure(
result["flow_id"], user_input={"next_step_id": "globalopts"}
)

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "globalopts"

# Enter some fake data into the form
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=MOCK_OPTIONS,
user_input=MOCK_OPTIONS_GLOBALS,
)

# Verify that the flow finishes
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == NAME

# Verify that the options were updated
assert entry.options == MOCK_OPTIONS
assert entry.options == MOCK_OPTIONS_GLOBALS

0 comments on commit 6d500c4

Please sign in to comment.