Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Nov 29, 2022
1 parent f869ce9 commit 34fb77b
Showing 1 changed file with 73 additions and 2 deletions.
75 changes: 73 additions & 2 deletions tests/helpers/test_schema_config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
TEST_DOMAIN = "test"


@pytest.fixture
def manager():
@pytest.fixture(name="manager")
def manager_fixture():
"""Return a flow manager."""
handlers = Registry()
entries = []
Expand Down Expand Up @@ -443,3 +443,74 @@ class TestConfigFlow(SchemaConfigFlowHandler, domain=TEST_DOMAIN):

result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == FlowResultType.CREATE_ENTRY


async def test_suggested_values(
hass: HomeAssistant, manager: data_entry_flow.FlowManager
) -> None:
"""Test suggested_values handling in SchemaFlowFormStep."""
manager.hass = hass

OPTIONS_SCHEMA = vol.Schema(
{vol.Optional("option1", default="a very reasonable default"): str}
)

OPTIONS_FLOW: dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
"init": SchemaFlowFormStep(OPTIONS_SCHEMA, next_step="step_1"),
"step_1": SchemaFlowFormStep(OPTIONS_SCHEMA, next_step="step_2"),
"step_2": SchemaFlowFormStep(
OPTIONS_SCHEMA,
suggested_values=lambda _: {"option1": "a random override"},
next_step="step_3",
),
"step_3": SchemaFlowFormStep(OPTIONS_SCHEMA, suggested_values=None),
}

class TestFlow(SchemaConfigFlowHandler, domain="test"):
config_flow = {}
options_flow = OPTIONS_FLOW

config_entry = MockConfigEntry(
data={},
domain="test",
options={"option1": "initial value"},
)
config_entry.add_to_hass(hass)

# Start flow in basic mode, suggested values should be the existing options
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
schema_keys: list[vol.Optional] = list(result["data_schema"].schema.keys())
assert schema_keys == ["option1"]
assert schema_keys[0].description == {"suggested_value": "initial value"}

# Go to step 1, suggested values should be the input from init
result = await hass.config_entries.options.async_configure(
result["flow_id"], {"option1": "blublu"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "step_1"
schema_keys: list[vol.Optional] = list(result["data_schema"].schema.keys())
assert schema_keys == ["option1"]
assert schema_keys[0].description == {"suggested_value": "blublu"}

# Go to step 2, suggested values should come from the callback function
result = await hass.config_entries.options.async_configure(
result["flow_id"], {"option1": "blabla"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "step_2"
schema_keys: list[vol.Optional] = list(result["data_schema"].schema.keys())
assert schema_keys == ["option1"]
assert schema_keys[0].description == {"suggested_value": "a random override"}

# Go to step 3, suggested values should be empty
result = await hass.config_entries.options.async_configure(
result["flow_id"], {"option1": "blabla"}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "step_3"
schema_keys: list[vol.Optional] = list(result["data_schema"].schema.keys())
assert schema_keys == ["option1"]
assert schema_keys[0].description is None

0 comments on commit 34fb77b

Please sign in to comment.