From 689504af8627da44dd5c20e30a1b9f84f2eb6a5f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 9 Jan 2022 21:20:21 -0800 Subject: [PATCH] Raise if trying to store mocks in storage (#63622) --- tests/common.py | 17 ++++++++++++++++- tests/helpers/test_storage.py | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/common.py b/tests/common.py index b0cc0a3f282..32e91768bf1 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1060,8 +1060,9 @@ def mock_storage(data=None): def mock_write_data(store, path, data_to_write): """Mock version of write data.""" - _LOGGER.info("Writing data to %s: %s", store.key, data_to_write) # To ensure that the data can be serialized + _LOGGER.info("Writing data to %s: %s", store.key, data_to_write) + raise_contains_mocks(data_to_write) data[store.key] = json.loads(json.dumps(data_to_write, cls=store._encoder)) async def mock_remove(store): @@ -1245,3 +1246,17 @@ def assert_lists_same(a, b): assert collections.Counter([hashdict(i) for i in a]) == collections.Counter( [hashdict(i) for i in b] ) + + +def raise_contains_mocks(val): + """Raise for mocks.""" + if isinstance(val, Mock): + raise ValueError + + if isinstance(val, dict): + for dict_value in val.values(): + raise_contains_mocks(dict_value) + + if isinstance(val, list): + for dict_value in val: + raise_contains_mocks(dict_value) diff --git a/tests/helpers/test_storage.py b/tests/helpers/test_storage.py index 0478c17e299..53c1b8a4677 100644 --- a/tests/helpers/test_storage.py +++ b/tests/helpers/test_storage.py @@ -73,7 +73,9 @@ async def test_custom_encoder(hass): return "9" store = storage.Store(hass, MOCK_VERSION, MOCK_KEY, encoder=JSONEncoder) - await store.async_save(Mock()) + with pytest.raises(ValueError): + await store.async_save(Mock()) + await store.async_save(object()) data = await store.async_load() assert data == "9"