Raise if trying to store mocks in storage (#63622)

pull/63774/head
Paulus Schoutsen 2022-01-09 21:20:21 -08:00 committed by GitHub
parent d8ba90fb8a
commit 689504af86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -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)

View File

@ -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"