home-assistant-core/tests/helpers/test_instance_id.py
Franck Nijhof bfb47eb212
Final clean up of helpers accessed via hass (#72032)
* Final clean up of helpers accessed via hass

* Fix circular dep

* Fix import
2022-05-17 23:42:37 +03:00

29 lines
846 B
Python

"""Tests for instance ID helper."""
from unittest.mock import patch
from homeassistant.helpers import instance_id
async def test_get_id_empty(hass, hass_storage):
"""Get unique ID."""
uuid = await instance_id.async_get(hass)
assert uuid is not None
# Assert it's stored
assert hass_storage["core.uuid"]["data"]["uuid"] == uuid
async def test_get_id_migrate(hass, hass_storage):
"""Migrate existing file."""
with patch(
"homeassistant.util.json.load_json", return_value={"uuid": "1234"}
), patch("os.path.isfile", return_value=True), patch("os.remove") as mock_remove:
uuid = await instance_id.async_get(hass)
assert uuid == "1234"
# Assert it's stored
assert hass_storage["core.uuid"]["data"]["uuid"] == uuid
# assert old deleted
assert len(mock_remove.mock_calls) == 1