-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy pathtest_init.py
More file actions
98 lines (74 loc) · 3.09 KB
/
test_init.py
File metadata and controls
98 lines (74 loc) · 3.09 KB
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
"""Tests for Glances integration."""
from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
from glances_api.exceptions import (
GlancesApiAuthorizationError,
GlancesApiConnectionError,
GlancesApiNoDataAvailable,
)
import pytest
from homeassistant.components.glances.const import DEFAULT_SCAN_INTERVAL, DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import UpdateFailed
from . import MOCK_USER_INPUT
from tests.common import MockConfigEntry, async_fire_time_changed
async def test_successful_config_entry(hass: HomeAssistant) -> None:
"""Test that Glances is configured successfully."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.LOADED
@pytest.mark.parametrize(
("error", "entry_state"),
[
(GlancesApiAuthorizationError, ConfigEntryState.SETUP_ERROR),
(GlancesApiConnectionError, ConfigEntryState.SETUP_RETRY),
(GlancesApiNoDataAvailable, ConfigEntryState.SETUP_ERROR),
],
)
async def test_setup_error(
hass: HomeAssistant,
error: Exception,
entry_state: ConfigEntryState,
mock_api: MagicMock,
) -> None:
"""Test Glances failed due to api error."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
mock_api.return_value.get_ha_sensor_data.side_effect = error
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is entry_state
async def test_update_error_includes_message(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
mock_api: MagicMock,
) -> None:
"""Test that the underlying API error message is propagated to UpdateFailed."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.LOADED
mock_api.return_value.get_ha_sensor_data.side_effect = GlancesApiConnectionError(
"Connection to http://localhost:61209/api/4/all failed"
)
freezer.tick(DEFAULT_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
coordinator = entry.runtime_data
assert coordinator.last_update_success is False
assert isinstance(coordinator.last_exception, UpdateFailed)
assert "Connection to http://localhost:61209/api/4/all failed" in str(
coordinator.last_exception
)
async def test_unload_entry(hass: HomeAssistant) -> None:
"""Test removing Glances."""
entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_INPUT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state is ConfigEntryState.NOT_LOADED
assert DOMAIN not in hass.data