From 94fd7d0353a8ddca0809d631c394af42fd6d7887 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 11 Dec 2023 16:48:12 +0100 Subject: [PATCH] Improve test of config entry store (#105487) * Improve test of config entry store * Tweak test --- tests/snapshots/test_config_entries.ambr | 18 ++++++++++ tests/test_config_entries.py | 46 +++++++++++++++++++----- 2 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 tests/snapshots/test_config_entries.ambr diff --git a/tests/snapshots/test_config_entries.ambr b/tests/snapshots/test_config_entries.ambr new file mode 100644 index 00000000000..beaa60cf762 --- /dev/null +++ b/tests/snapshots/test_config_entries.ambr @@ -0,0 +1,18 @@ +# serializer version: 1 +# name: test_as_dict + dict({ + 'data': dict({ + }), + 'disabled_by': None, + 'domain': 'test', + 'entry_id': 'mock-entry', + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'Mock Title', + 'unique_id': None, + 'version': 1, + }) +# --- diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index f63972c79e8..40e3b3b4c3c 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -9,6 +9,7 @@ from typing import Any from unittest.mock import AsyncMock, Mock, patch import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant import config_entries, data_entry_flow, loader from homeassistant.components import dhcp @@ -667,14 +668,43 @@ async def test_saving_and_loading(hass: HomeAssistant) -> None: for orig, loaded in zip( hass.config_entries.async_entries(), manager.async_entries() ): - assert orig.version == loaded.version - assert orig.domain == loaded.domain - assert orig.title == loaded.title - assert orig.data == loaded.data - assert orig.source == loaded.source - assert orig.unique_id == loaded.unique_id - assert orig.pref_disable_new_entities == loaded.pref_disable_new_entities - assert orig.pref_disable_polling == loaded.pref_disable_polling + assert orig.as_dict() == loaded.as_dict() + + +async def test_as_dict(snapshot: SnapshotAssertion) -> None: + """Test ConfigEntry.as_dict.""" + + # Ensure as_dict is not overridden + assert MockConfigEntry.as_dict is config_entries.ConfigEntry.as_dict + + excluded_from_dict = { + "supports_unload", + "supports_remove_device", + "state", + "_setup_lock", + "update_listeners", + "reason", + "_async_cancel_retry_setup", + "_on_unload", + "reload_lock", + "_reauth_lock", + "_tasks", + "_background_tasks", + "_integration_for_domain", + "_tries", + "_setup_again_job", + } + + entry = MockConfigEntry(entry_id="mock-entry") + + # Make sure the expected keys are present + dict_repr = entry.as_dict() + for key in config_entries.ConfigEntry.__slots__: + assert key in dict_repr or key in excluded_from_dict + assert not (key in dict_repr and key in excluded_from_dict) + + # Make sure the dict representation is as expected + assert dict_repr == snapshot async def test_forward_entry_sets_up_component(hass: HomeAssistant) -> None: