Test data entry flow form showing suggested values (#141249)

Add test with from showing suggested values to data entry flow tests
This commit is contained in:
Jan Bouwhuis 2025-03-24 10:36:02 +01:00 committed by GitHub
parent b4fd5339c6
commit 0f60fd8c40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -133,6 +133,57 @@ async def test_show_form(manager: MockFlowManager) -> None:
assert form["errors"] == {"username": "Should be unique."}
async def test_form_shows_with_added_suggested_values(manager: MockFlowManager) -> None:
"""Test that we can show a form with suggested values."""
schema = vol.Schema(
{
vol.Required("username"): str,
vol.Required("password"): str,
vol.Required("section_1"): data_entry_flow.section(
vol.Schema(
{
vol.Optional("full_name"): str,
}
),
{"collapsed": False},
),
}
)
@manager.mock_reg_handler("test")
class TestFlow(data_entry_flow.FlowHandler):
async def async_step_init(self, user_input=None):
data_schema = self.add_suggested_values_to_schema(
schema,
{
"username": "doej",
"password": "verySecret1",
"section_1": {"full_name": "John Doe"},
},
)
return self.async_show_form(
step_id="init",
data_schema=data_schema,
)
form = await manager.async_init("test")
assert form["type"] == data_entry_flow.FlowResultType.FORM
assert form["data_schema"].schema == schema.schema
markers = list(form["data_schema"].schema)
assert len(markers) == 3
assert markers[0] == "username"
assert markers[0].description == {"suggested_value": "doej"}
assert markers[1] == "password"
assert markers[1].description == {"suggested_value": "verySecret1"}
assert markers[2] == "section_1"
section_validator = form["data_schema"].schema["section_1"]
assert isinstance(section_validator, data_entry_flow.section)
section_markers = list(section_validator.schema.schema)
assert len(section_markers) == 1
assert section_markers[0] == "full_name"
assert section_markers[0].description == {"suggested_value": "John Doe"}
async def test_abort_removes_instance(manager: MockFlowManager) -> None:
"""Test that abort removes the flow from progress."""