mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Merge similar tests to parameterized tests for enphase_envoy (#133740)
This commit is contained in:
parent
0c24afec6c
commit
cd6da9d9e8
@ -11,8 +11,6 @@ rules:
|
|||||||
config-flow-test-coverage:
|
config-flow-test-coverage:
|
||||||
status: todo
|
status: todo
|
||||||
comment: |
|
comment: |
|
||||||
- Let's have every test result in either CREATE_ENTRY or ABORT (like test_form_invalid_auth or test_form_cannot_connect, they can be parametrized)
|
|
||||||
- test_zeroconf_token_firmware and test_zeroconf_pre_token_firmware can also be parametrized I think
|
|
||||||
- test_zero_conf_malformed_serial_property - with pytest.raises(KeyError) as ex::
|
- test_zero_conf_malformed_serial_property - with pytest.raises(KeyError) as ex::
|
||||||
I don't believe this should be able to raise a KeyError Shouldn't we abort the flow?
|
I don't believe this should be able to raise a KeyError Shouldn't we abort the flow?
|
||||||
config-flow:
|
config-flow:
|
||||||
|
@ -90,47 +90,23 @@ async def test_user_no_serial_number(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_form_invalid_auth(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
mock_setup_entry: AsyncMock,
|
|
||||||
mock_envoy: AsyncMock,
|
|
||||||
) -> None:
|
|
||||||
"""Test we handle invalid auth."""
|
|
||||||
mock_envoy.authenticate.side_effect = EnvoyAuthenticationError(
|
|
||||||
"fail authentication"
|
|
||||||
)
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
|
||||||
)
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result["flow_id"],
|
|
||||||
{
|
|
||||||
CONF_HOST: "1.1.1.1",
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
assert result2["type"] is FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "invalid_auth"}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("exception", "error"),
|
("exception", "error"),
|
||||||
[
|
[
|
||||||
|
(EnvoyAuthenticationError("fail authentication"), "invalid_auth"),
|
||||||
(EnvoyError, "cannot_connect"),
|
(EnvoyError, "cannot_connect"),
|
||||||
|
(Exception, "unknown"),
|
||||||
(ValueError, "unknown"),
|
(ValueError, "unknown"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_form_cannot_connect(
|
async def test_form_errors(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_setup_entry: AsyncMock,
|
mock_setup_entry: AsyncMock,
|
||||||
mock_envoy: AsyncMock,
|
mock_envoy: AsyncMock,
|
||||||
exception: Exception,
|
exception: Exception,
|
||||||
error: str,
|
error: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we handle cannot connect error."""
|
"""Test we handle form errors."""
|
||||||
mock_envoy.setup.side_effect = exception
|
mock_envoy.setup.side_effect = exception
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": SOURCE_USER}
|
DOMAIN, context={"source": SOURCE_USER}
|
||||||
@ -148,41 +124,8 @@ async def test_form_cannot_connect(
|
|||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["errors"] == {"base": error}
|
assert result["errors"] == {"base": error}
|
||||||
|
|
||||||
|
mock_envoy.setup.side_effect = None
|
||||||
def _get_schema_default(schema, key_name):
|
# mock successful authentication and update of credentials
|
||||||
"""Iterate schema to find a key."""
|
|
||||||
for schema_key in schema:
|
|
||||||
if schema_key == key_name:
|
|
||||||
return schema_key.default()
|
|
||||||
raise KeyError(f"{key_name} not found in schema")
|
|
||||||
|
|
||||||
|
|
||||||
async def test_zeroconf_pre_token_firmware(
|
|
||||||
hass: HomeAssistant,
|
|
||||||
mock_setup_entry: AsyncMock,
|
|
||||||
mock_envoy: AsyncMock,
|
|
||||||
) -> None:
|
|
||||||
"""Test we can setup from zeroconf."""
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN,
|
|
||||||
context={"source": SOURCE_ZEROCONF},
|
|
||||||
data=zeroconf.ZeroconfServiceInfo(
|
|
||||||
ip_address=ip_address("1.1.1.1"),
|
|
||||||
ip_addresses=[ip_address("1.1.1.1")],
|
|
||||||
hostname="mock_hostname",
|
|
||||||
name="mock_name",
|
|
||||||
port=None,
|
|
||||||
properties={"serialnum": "1234", "protovers": "3.0.0"},
|
|
||||||
type="mock_type",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
assert result["type"] is FlowResultType.FORM
|
|
||||||
assert result["step_id"] == "user"
|
|
||||||
|
|
||||||
assert (
|
|
||||||
_get_schema_default(result["data_schema"].schema, CONF_USERNAME) == "installer"
|
|
||||||
)
|
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
@ -192,20 +135,29 @@ async def test_zeroconf_pre_token_firmware(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "Envoy 1234"
|
|
||||||
assert result["result"].unique_id == "1234"
|
|
||||||
assert result["data"] == {
|
|
||||||
CONF_HOST: "1.1.1.1",
|
|
||||||
CONF_NAME: "Envoy 1234",
|
|
||||||
CONF_USERNAME: "test-username",
|
|
||||||
CONF_PASSWORD: "test-password",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async def test_zeroconf_token_firmware(
|
def _get_schema_default(schema, key_name):
|
||||||
|
"""Iterate schema to find a key."""
|
||||||
|
for schema_key in schema:
|
||||||
|
if schema_key == key_name:
|
||||||
|
return schema_key.default()
|
||||||
|
raise KeyError(f"{key_name} not found in schema")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("version", "schema_username"),
|
||||||
|
[
|
||||||
|
("7.0.0", ""),
|
||||||
|
("3.0.0", "installer"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_zeroconf(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_setup_entry: AsyncMock,
|
mock_setup_entry: AsyncMock,
|
||||||
mock_envoy: AsyncMock,
|
mock_envoy: AsyncMock,
|
||||||
|
version: str,
|
||||||
|
schema_username: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test we can setup from zeroconf."""
|
"""Test we can setup from zeroconf."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -217,13 +169,16 @@ async def test_zeroconf_token_firmware(
|
|||||||
hostname="mock_hostname",
|
hostname="mock_hostname",
|
||||||
name="mock_name",
|
name="mock_name",
|
||||||
port=None,
|
port=None,
|
||||||
properties={"serialnum": "1234", "protovers": "7.0.0"},
|
properties={"serialnum": "1234", "protovers": version},
|
||||||
type="mock_type",
|
type="mock_type",
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert _get_schema_default(result["data_schema"].schema, CONF_USERNAME) == ""
|
assert (
|
||||||
|
_get_schema_default(result["data_schema"].schema, CONF_USERNAME)
|
||||||
|
== schema_username
|
||||||
|
)
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user