-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
/
test_migration.py
111 lines (97 loc) · 3.38 KB
/
test_migration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Test the swiss_public_transport config flow."""
from unittest.mock import patch
from homeassistant.components.swiss_public_transport import (
async_migrate_entry,
async_setup_entry,
)
from homeassistant.components.swiss_public_transport.const import (
CONF_DESTINATION,
CONF_START,
DOMAIN,
)
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry
MOCK_DATA_STEP = {
CONF_START: "test_start",
CONF_DESTINATION: "test_destination",
}
FAULTY_ENTRY_ID = "FAULTY"
def side_effect_async_get_data(x):
"""Side effect on calling async get data which fills the connections attribute."""
x.connections = [
{
"departure": "DEPARTURE_0",
"number": 0,
"platform": 0,
"transfers": 0,
"duration": "10",
"delay": 0,
},
{
"departure": "DEPARTURE_1",
"number": 1,
"platform": 1,
"transfers": 0,
"duration": "10",
"delay": 0,
},
{
"departure": "DEPARTURE_2",
"number": 2,
"platform": 2,
"transfers": 0,
"duration": "10",
"delay": 0,
},
]
async def test_migration_1_2(hass: HomeAssistant) -> None:
"""Test successful setup."""
with patch(
"homeassistant.components.swiss_public_transport.OpendataTransport.async_get_data",
autospec=True,
return_value=True,
side_effect=side_effect_async_get_data,
):
# Set up mock objects
entity_registry = er.async_get(hass)
config_entry_faulty = MockConfigEntry(
domain=DOMAIN,
entry_id=FAULTY_ENTRY_ID,
data=MOCK_DATA_STEP,
title="MIGRATION_TEST",
unique_id=None,
)
config_entry_faulty.add_to_hass(hass)
# Setup the config entry
await async_setup_entry(hass, config_entry_faulty)
await hass.async_block_till_done()
# print(entity_registry.entities.values())
# print(entity_registry.entities._index)
assert entity_registry.async_is_registered(
entity_registry.entities.get_entity_id(
(Platform.SENSOR, DOMAIN, "None_departure")
)
)
# Migration: add unique_id and save new config entry, remove faulty entity from registry
assert await async_migrate_entry(hass, config_entry_faulty)
await hass.async_block_till_done()
# Check change in config entry
assert config_entry_faulty.version == 2
assert config_entry_faulty.unique_id == "test_start test_destination"
# Check faulty entity has been removed
assert not entity_registry.async_is_registered(
entity_registry.entities.get_entity_id(
(Platform.SENSOR, DOMAIN, "None_departure")
)
)
# Check migrated entity has been added
# print(config_entry_faulty.entry_id)
# print(entity_registry.entities.values())
# print(entity_registry._entities_data.keys())
assert entity_registry.async_is_registered(
entity_registry.entities.get_entity_id(
(Platform.SENSOR, DOMAIN, "test_start test_destination_departure")
)
)