Merge similar tests to parameterized tests for enphase_envoy (#133740)

This commit is contained in:
Arie Catsman 2024-12-22 10:07:35 +01:00 committed by GitHub
parent 0c24afec6c
commit cd6da9d9e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 76 deletions

View File

@ -11,8 +11,6 @@ rules:
config-flow-test-coverage:
status: todo
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::
I don't believe this should be able to raise a KeyError Shouldn't we abort the flow?
config-flow:

View File

@ -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(
("exception", "error"),
[
(EnvoyAuthenticationError("fail authentication"), "invalid_auth"),
(EnvoyError, "cannot_connect"),
(Exception, "unknown"),
(ValueError, "unknown"),
],
)
async def test_form_cannot_connect(
async def test_form_errors(
hass: HomeAssistant,
mock_setup_entry: AsyncMock,
mock_envoy: AsyncMock,
exception: Exception,
error: str,
) -> None:
"""Test we handle cannot connect error."""
"""Test we handle form errors."""
mock_envoy.setup.side_effect = exception
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
@ -148,41 +124,8 @@ async def test_form_cannot_connect(
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": error}
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")
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"
)
mock_envoy.setup.side_effect = None
# mock successful authentication and update of credentials
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
@ -192,20 +135,29 @@ async def test_zeroconf_pre_token_firmware(
},
)
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,
mock_setup_entry: AsyncMock,
mock_envoy: AsyncMock,
version: str,
schema_username: str,
) -> None:
"""Test we can setup from zeroconf."""
result = await hass.config_entries.flow.async_init(
@ -217,13 +169,16 @@ async def test_zeroconf_token_firmware(
hostname="mock_hostname",
name="mock_name",
port=None,
properties={"serialnum": "1234", "protovers": "7.0.0"},
properties={"serialnum": "1234", "protovers": version},
type="mock_type",
),
)
assert result["type"] is FlowResultType.FORM
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(
result["flow_id"],