Use snapshot assertion for KNX diagnostics (#98724)

Co-authored-by: farmio <farmio@alphart.net>
This commit is contained in:
Joost Lekkerkerker 2023-08-21 11:48:55 +02:00 committed by GitHub
parent 1fca665b77
commit 9c54c4abf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 70 deletions

View File

@ -0,0 +1,95 @@
# serializer version: 1
# name: test_diagnostic_config_error[hass_config0]
dict({
'config_entry_data': dict({
'connection_type': 'automatic',
'individual_address': '0.0.240',
'multicast_group': '224.0.23.12',
'multicast_port': 3671,
'rate_limit': 0,
'state_updater': True,
}),
'configuration_error': "extra keys not allowed @ data['knx']['wrong_key']",
'configuration_yaml': dict({
'wrong_key': dict({
}),
}),
'project_info': None,
'xknx': dict({
'current_address': '0.0.0',
'version': '0.0.0',
}),
})
# ---
# name: test_diagnostic_redact[hass_config0]
dict({
'config_entry_data': dict({
'backbone_key': '**REDACTED**',
'connection_type': 'automatic',
'device_authentication': '**REDACTED**',
'individual_address': '0.0.240',
'knxkeys_password': '**REDACTED**',
'multicast_group': '224.0.23.12',
'multicast_port': 3671,
'rate_limit': 0,
'state_updater': True,
'user_password': '**REDACTED**',
}),
'configuration_error': None,
'configuration_yaml': None,
'project_info': None,
'xknx': dict({
'current_address': '0.0.0',
'version': '0.0.0',
}),
})
# ---
# name: test_diagnostics[hass_config0]
dict({
'config_entry_data': dict({
'connection_type': 'automatic',
'individual_address': '0.0.240',
'multicast_group': '224.0.23.12',
'multicast_port': 3671,
'rate_limit': 0,
'state_updater': True,
}),
'configuration_error': None,
'configuration_yaml': None,
'project_info': None,
'xknx': dict({
'current_address': '0.0.0',
'version': '0.0.0',
}),
})
# ---
# name: test_diagnostics_project[hass_config0]
dict({
'config_entry_data': dict({
'connection_type': 'automatic',
'individual_address': '0.0.240',
'multicast_group': '224.0.23.12',
'multicast_port': 3671,
'rate_limit': 0,
'state_updater': True,
}),
'configuration_error': None,
'configuration_yaml': None,
'project_info': dict({
'created_by': 'ETS5',
'group_address_style': 'ThreeLevel',
'guid': '6a019e80-5945-489e-95a3-378735c642d1',
'language_code': 'de-DE',
'last_modified': '2023-04-30T09:04:04.4043671Z',
'name': '**REDACTED**',
'project_id': 'P-04FF',
'schema_version': '20',
'tool_version': '5.7.1428.39779',
'xknxproject_version': '3.1.0',
}),
'xknx': dict({
'current_address': '0.0.0',
'version': '0.0.0',
}),
})
# ---

View File

@ -1,6 +1,7 @@
"""Tests for the diagnostics data provided by the KNX integration.""" """Tests for the diagnostics data provided by the KNX integration."""
import pytest import pytest
from syrupy import SnapshotAssertion
from xknx.io import DEFAULT_MCAST_GRP, DEFAULT_MCAST_PORT from xknx.io import DEFAULT_MCAST_GRP, DEFAULT_MCAST_PORT
from homeassistant.components.knx.const import ( from homeassistant.components.knx.const import (
@ -36,28 +37,17 @@ async def test_diagnostics(
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
knx: KNXTestKit, knx: KNXTestKit,
mock_hass_config: None, mock_hass_config: None,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics.""" """Test diagnostics."""
await knx.setup_integration({}) await knx.setup_integration({})
# Overwrite the version for this test since we don't want to change this with every library bump # Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0" knx.xknx.version = "0.0.0"
assert await get_diagnostics_for_config_entry( assert (
hass, hass_client, mock_config_entry await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
) == { == snapshot
"config_entry_data": { )
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": None,
"configuration_yaml": None,
"project_info": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
@pytest.mark.parametrize("hass_config", [{"knx": {"wrong_key": {}}}]) @pytest.mark.parametrize("hass_config", [{"knx": {"wrong_key": {}}}])
@ -67,28 +57,18 @@ async def test_diagnostic_config_error(
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
knx: KNXTestKit, knx: KNXTestKit,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics.""" """Test diagnostics."""
await knx.setup_integration({}) await knx.setup_integration({})
# Overwrite the version for this test since we don't want to change this with every library bump # Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0" knx.xknx.version = "0.0.0"
assert await get_diagnostics_for_config_entry( # the snapshot will contain 'configuration_error' key with the voluptuous error message
hass, hass_client, mock_config_entry assert (
) == { await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
"config_entry_data": { == snapshot
"connection_type": "automatic", )
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
},
"configuration_error": "extra keys not allowed @ data['knx']['wrong_key']",
"configuration_yaml": {"wrong_key": {}},
"project_info": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
@pytest.mark.parametrize("hass_config", [{}]) @pytest.mark.parametrize("hass_config", [{}])
@ -96,6 +76,7 @@ async def test_diagnostic_redact(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
mock_hass_config: None, mock_hass_config: None,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics redacting data.""" """Test diagnostics redacting data."""
mock_config_entry: MockConfigEntry = MockConfigEntry( mock_config_entry: MockConfigEntry = MockConfigEntry(
@ -118,27 +99,11 @@ async def test_diagnostic_redact(
await knx.setup_integration({}) await knx.setup_integration({})
# Overwrite the version for this test since we don't want to change this with every library bump # Overwrite the version for this test since we don't want to change this with every library bump
knx.xknx.version = "1.0.0" knx.xknx.version = "0.0.0"
assert await get_diagnostics_for_config_entry( assert (
hass, hass_client, mock_config_entry await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
) == { == snapshot
"config_entry_data": { )
"connection_type": "automatic",
"individual_address": "0.0.240",
"multicast_group": "224.0.23.12",
"multicast_port": 3671,
"rate_limit": 0,
"state_updater": True,
"knxkeys_password": "**REDACTED**",
"user_password": "**REDACTED**",
"device_authentication": "**REDACTED**",
"backbone_key": "**REDACTED**",
},
"configuration_error": None,
"configuration_yaml": None,
"project_info": None,
"xknx": {"current_address": "0.0.0", "version": "1.0.0"},
}
@pytest.mark.parametrize("hass_config", [{}]) @pytest.mark.parametrize("hass_config", [{}])
@ -149,21 +114,13 @@ async def test_diagnostics_project(
knx: KNXTestKit, knx: KNXTestKit,
mock_hass_config: None, mock_hass_config: None,
load_knxproj: None, load_knxproj: None,
snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics.""" """Test diagnostics."""
await knx.setup_integration({}) await knx.setup_integration({})
diag = await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry) knx.xknx.version = "0.0.0"
# snapshot will contain project specific fields in `project_info`
assert "config_entry_data" in diag assert (
assert "configuration_error" in diag await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
assert "configuration_yaml" in diag == snapshot
assert "project_info" in diag )
assert "xknx" in diag
# project specific fields
assert "created_by" in diag["project_info"]
assert "group_address_style" in diag["project_info"]
assert "last_modified" in diag["project_info"]
assert "schema_version" in diag["project_info"]
assert "tool_version" in diag["project_info"]
assert "language_code" in diag["project_info"]
assert diag["project_info"]["name"] == "**REDACTED**"