De-duplicate test helper function (#143437)

* De-duplicate test helper function

* One more
This commit is contained in:
epenet 2025-04-22 12:04:12 +02:00 committed by GitHub
parent e9269a1d33
commit a3605921c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 57 additions and 153 deletions

View File

@ -1911,3 +1911,16 @@ def get_quality_scale(integration: str) -> dict[str, QualityScaleStatus]:
)
for rule, details in raw["rules"].items()
}
def get_schema_suggested_value(schema: vol.Schema, key: str) -> Any | None:
"""Get suggested value for key in voluptuous schema."""
for schema_key in schema:
if schema_key == key:
if (
schema_key.description is None
or "suggested_value" not in schema_key.description
):
return None
return schema_key.description["suggested_value"]
return None

View File

@ -10,7 +10,7 @@ from homeassistant.components.cast.home_assistant_cast import CAST_USER_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
async def test_creating_entry_sets_up_media_player(hass: HomeAssistant) -> None:
@ -141,16 +141,6 @@ async def test_zeroconf_setup_onboarding(hass: HomeAssistant) -> None:
}
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
return None
@pytest.mark.parametrize(
("parameter", "initial", "suggested", "user_input", "updated"),
[
@ -219,9 +209,9 @@ async def test_option_flow(
for other_param in basic_parameters:
if other_param == parameter:
continue
assert get_suggested(data_schema, other_param) == []
assert get_schema_suggested_value(data_schema, other_param) == []
if parameter in basic_parameters:
assert get_suggested(data_schema, parameter) == suggested
assert get_schema_suggested_value(data_schema, parameter) == suggested
user_input_dict = {}
if parameter in basic_parameters:
@ -244,9 +234,9 @@ async def test_option_flow(
for other_param in advanced_parameters:
if other_param == parameter:
continue
assert get_suggested(data_schema, other_param) == ""
assert get_schema_suggested_value(data_schema, other_param) == ""
if parameter in advanced_parameters:
assert get_suggested(data_schema, parameter) == suggested
assert get_schema_suggested_value(data_schema, parameter) == suggested
user_input_dict = {}
if parameter in advanced_parameters:

View File

@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import selector
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("platform", ["sensor"])
@ -64,17 +64,6 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None:
assert config_entry.title == "My derivative"
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
@pytest.mark.parametrize("platform", ["sensor"])
async def test_options(hass: HomeAssistant, platform) -> None:
"""Test reconfiguring."""
@ -104,10 +93,10 @@ async def test_options(hass: HomeAssistant, platform) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "round") == 1.0
assert get_suggested(schema, "time_window") == {"seconds": 0.0}
assert get_suggested(schema, "unit_prefix") == "k"
assert get_suggested(schema, "unit_time") == "min"
assert get_schema_suggested_value(schema, "round") == 1.0
assert get_schema_suggested_value(schema, "time_window") == {"seconds": 0.0}
assert get_schema_suggested_value(schema, "unit_prefix") == "k"
assert get_schema_suggested_value(schema, "unit_time") == "min"
source = schema["source"]
assert isinstance(source, selector.EntitySelector)

View File

@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
from tests.typing import WebSocketGenerator
@ -201,17 +201,6 @@ async def test_config_flow_hides_members(
assert entity_registry.async_get(f"{group_type}.three").hidden_by == hidden_by
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
@pytest.mark.parametrize(
("group_type", "member_state", "extra_options", "options_options"),
[
@ -269,7 +258,9 @@ async def test_options(
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == group_type
assert get_suggested(result["data_schema"].schema, "entities") == members1
assert (
get_schema_suggested_value(result["data_schema"].schema, "entities") == members1
)
assert "name" not in result["data_schema"].schema
assert result["data_schema"].schema["entities"].config["exclude_entities"] == [
f"{group_type}.bed_room"
@ -316,8 +307,8 @@ async def test_options(
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == group_type
assert get_suggested(result["data_schema"].schema, "entities") is None
assert get_suggested(result["data_schema"].schema, "name") is None
assert get_schema_suggested_value(result["data_schema"].schema, "entities") is None
assert get_schema_suggested_value(result["data_schema"].schema, "name") is None
@pytest.mark.parametrize(

View File

@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import selector
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("platform", ["sensor"])
@ -67,17 +67,6 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None:
assert config_entry.title == "My integration"
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
@pytest.mark.parametrize("platform", ["sensor"])
async def test_options(hass: HomeAssistant, platform) -> None:
"""Test reconfiguring."""
@ -108,7 +97,7 @@ async def test_options(hass: HomeAssistant, platform) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "round") == 1.0
assert get_schema_suggested_value(schema, "round") == 1.0
source = schema["source"]
assert isinstance(source, selector.EntitySelector)

View File

@ -9,7 +9,7 @@ from homeassistant.components.min_max.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("platform", ["sensor"])
@ -55,17 +55,6 @@ async def test_config_flow(hass: HomeAssistant, platform: str) -> None:
assert config_entry.title == "My min_max"
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
@pytest.mark.parametrize("platform", ["sensor"])
async def test_options(hass: HomeAssistant, platform: str) -> None:
"""Test reconfiguring."""
@ -96,9 +85,9 @@ async def test_options(hass: HomeAssistant, platform: str) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "entity_ids") == input_sensors1
assert get_suggested(schema, "round_digits") == 0
assert get_suggested(schema, "type") == "min"
assert get_schema_suggested_value(schema, "entity_ids") == input_sensors1
assert get_schema_suggested_value(schema, "round_digits") == 0
assert get_schema_suggested_value(schema, "type") == "min"
result = await hass.config_entries.options.async_configure(
result["flow_id"],

View File

@ -42,7 +42,7 @@ from .common import (
MOCK_SWITCH_SUBENTRY_DATA_SINGLE_STATE_CLASS,
)
from tests.common import MockConfigEntry, MockMqttReasonCode
from tests.common import MockConfigEntry, MockMqttReasonCode, get_schema_suggested_value
from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient
ADD_ON_DISCOVERY_INFO = {
@ -1453,19 +1453,6 @@ def get_default(schema: vol.Schema, key: str) -> Any | None:
return None
def get_suggested(schema: vol.Schema, key: str) -> Any | None:
"""Get suggested value for key in voluptuous schema."""
for schema_key in schema: # type:ignore[attr-defined]
if schema_key == key:
if (
schema_key.description is None
or "suggested_value" not in schema_key.description
):
return None
return schema_key.description["suggested_value"]
return None
@pytest.mark.usefixtures("mock_reload_after_entry_update")
async def test_option_flow_default_suggested_values(
hass: HomeAssistant,
@ -1520,7 +1507,7 @@ async def test_option_flow_default_suggested_values(
for key, value in defaults.items():
assert get_default(result["data_schema"].schema, key) == value
for key, value in suggested.items():
assert get_suggested(result["data_schema"].schema, key) == value
assert get_schema_suggested_value(result["data_schema"].schema, key) == value
result = await hass.config_entries.options.async_configure(
result["flow_id"],
@ -1556,7 +1543,7 @@ async def test_option_flow_default_suggested_values(
for key, value in defaults.items():
assert get_default(result["data_schema"].schema, key) == value
for key, value in suggested.items():
assert get_suggested(result["data_schema"].schema, key) == value
assert get_schema_suggested_value(result["data_schema"].schema, key) == value
result = await hass.config_entries.options.async_configure(
result["flow_id"],
@ -2038,7 +2025,7 @@ async def test_try_connection_with_advanced_parameters(
for k, v in defaults.items():
assert get_default(result["data_schema"].schema, k) == v
for k, v in suggested.items():
assert get_suggested(result["data_schema"].schema, k) == v
assert get_schema_suggested_value(result["data_schema"].schema, k) == v
# test we can change username and password
mock_try_connection_success.reset_mock()

View File

@ -20,7 +20,7 @@ from homeassistant.helpers import entity_registry as er
from . import PLATFORMS_TO_TEST, STATE_MAP
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("target_domain", PLATFORMS_TO_TEST)
@ -160,9 +160,7 @@ async def test_options(
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
schema_key = next(k for k in schema if k == CONF_INVERT)
assert schema_key.description["suggested_value"] is True
assert get_schema_suggested_value(result["data_schema"].schema, CONF_INVERT) is True
result = await hass.config_entries.options.async_configure(
result["flow_id"],

View File

@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
from tests.typing import WebSocketGenerator
SWITCH_BEFORE_OPTIONS = {
@ -407,17 +407,6 @@ async def test_config_flow_device(
}
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# If the desired key is missing from the schema, return None
return None
@pytest.mark.parametrize(
(
"template_type",
@ -608,7 +597,7 @@ async def test_options(
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == template_type
assert get_suggested(
assert get_schema_suggested_value(
result["data_schema"].schema, key_template
) == old_state_template.get(key_template)
assert "name" not in result["data_schema"].schema
@ -655,8 +644,10 @@ async def test_options(
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == template_type
assert get_suggested(result["data_schema"].schema, "name") is None
assert get_suggested(result["data_schema"].schema, key_template) is None
assert get_schema_suggested_value(result["data_schema"].schema, "name") is None
assert (
get_schema_suggested_value(result["data_schema"].schema, key_template) is None
)
@pytest.mark.parametrize(

View File

@ -11,7 +11,7 @@ from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
from tests.typing import WebSocketGenerator
@ -88,17 +88,6 @@ async def test_fail(hass: HomeAssistant, extra_input_data, error) -> None:
assert result["errors"] == {"base": error}
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
async def test_options(hass: HomeAssistant) -> None:
"""Test reconfiguring."""
input_sensor = "sensor.input"
@ -125,9 +114,9 @@ async def test_options(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "hysteresis") == 0.0
assert get_suggested(schema, "lower") == -2.0
assert get_suggested(schema, "upper") is None
assert get_schema_suggested_value(schema, "hysteresis") == 0.0
assert get_schema_suggested_value(schema, "lower") == -2.0
assert get_schema_suggested_value(schema, "upper") is None
result = await hass.config_entries.options.async_configure(
result["flow_id"],

View File

@ -9,7 +9,7 @@ from homeassistant.components.tod.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("platform", ["sensor"])
@ -55,17 +55,6 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None:
assert config_entry.title == "My tod"
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
@pytest.mark.freeze_time("2022-03-16 17:37:00", tz_offset=-7)
async def test_options(hass: HomeAssistant) -> None:
"""Test reconfiguring."""
@ -88,8 +77,8 @@ async def test_options(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "after_time") == "10:00"
assert get_suggested(schema, "before_time") == "18:05"
assert get_schema_suggested_value(schema, "after_time") == "10:00"
assert get_schema_suggested_value(schema, "before_time") == "18:05"
result = await hass.config_entries.options.async_configure(
result["flow_id"],

View File

@ -10,7 +10,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.mark.parametrize("platform", ["sensor"])
@ -253,17 +253,6 @@ async def test_always_available(hass: HomeAssistant) -> None:
}
def get_suggested(schema, key):
"""Get suggested value for key in voluptuous schema."""
for k in schema:
if k == key:
if k.description is None or "suggested_value" not in k.description:
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise KeyError("Wanted key absent from schema")
async def test_options(hass: HomeAssistant) -> None:
"""Test reconfiguring."""
input_sensor1_entity_id = "sensor.input1"
@ -293,8 +282,8 @@ async def test_options(hass: HomeAssistant) -> None:
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
schema = result["data_schema"].schema
assert get_suggested(schema, "source") == input_sensor1_entity_id
assert get_suggested(schema, "periodically_resetting") is True
assert get_schema_suggested_value(schema, "source") == input_sensor1_entity_id
assert get_schema_suggested_value(schema, "periodically_resetting") is True
result = await hass.config_entries.options.async_configure(
result["flow_id"],