Further integration load cleanups (#23104)

* Further integration load cleanups

* Fix tests

* Unflake MQTT vacuum command test
This commit is contained in:
Paulus Schoutsen 2019-04-14 19:07:05 -07:00 committed by GitHub
parent 930f75220c
commit d722f4d64a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 160 additions and 237 deletions

View file

@ -5,7 +5,7 @@ from unittest.mock import MagicMock, patch
import pytest
from homeassistant import config_entries, loader, data_entry_flow
from homeassistant import config_entries, data_entry_flow
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.setup import async_setup_component
@ -49,8 +49,8 @@ async def test_call_setup_entry(hass):
mock_setup_entry = MagicMock(return_value=mock_coro(True))
mock_migrate_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
@ -70,8 +70,8 @@ async def test_call_async_migrate_entry(hass):
mock_migrate_entry = MagicMock(return_value=mock_coro(True))
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
@ -91,8 +91,8 @@ async def test_call_async_migrate_entry_failure_false(hass):
mock_migrate_entry = MagicMock(return_value=mock_coro(False))
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
@ -113,8 +113,8 @@ async def test_call_async_migrate_entry_failure_exception(hass):
return_value=mock_coro(exception=Exception))
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
@ -135,8 +135,8 @@ async def test_call_async_migrate_entry_failure_not_bool(hass):
return_value=mock_coro())
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry,
async_migrate_entry=mock_migrate_entry))
@ -155,8 +155,8 @@ async def test_call_async_migrate_entry_failure_not_supported(hass):
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry))
result = await async_setup_component(hass, 'comp', {})
@ -265,7 +265,7 @@ async def test_remove_entry_handles_callback_error(hass, manager):
mock_unload_entry = MagicMock(return_value=mock_coro(True))
mock_remove_entry = MagicMock(
side_effect=lambda *args, **kwargs: mock_coro())
loader.set_component(hass, 'test', MockModule(
mock_integration(hass, MockModule(
'test',
async_setup_entry=mock_setup_entry,
async_unload_entry=mock_unload_entry,
@ -304,13 +304,12 @@ def test_remove_entry_raises(hass, manager):
"""Mock unload entry function."""
raise Exception("BROKEN")
loader.set_component(
hass, 'test',
MockModule('comp', async_unload_entry=mock_unload_entry))
mock_integration(hass, MockModule(
'comp', async_unload_entry=mock_unload_entry))
MockConfigEntry(domain='test', entry_id='test1').add_to_manager(manager)
MockConfigEntry(
domain='test',
domain='comp',
entry_id='test2',
state=config_entries.ENTRY_STATE_LOADED
).add_to_manager(manager)
@ -330,15 +329,14 @@ def test_remove_entry_raises(hass, manager):
@asyncio.coroutine
def test_remove_entry_if_not_loaded(hass, manager):
"""Test that we can remove an entry."""
"""Test that we can remove an entry that is not loaded."""
mock_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'test',
MockModule('comp', async_unload_entry=mock_unload_entry))
mock_integration(hass, MockModule(
'comp', async_unload_entry=mock_unload_entry))
MockConfigEntry(domain='test', entry_id='test1').add_to_manager(manager)
MockConfigEntry(domain='test', entry_id='test2').add_to_manager(manager)
MockConfigEntry(domain='comp', entry_id='test2').add_to_manager(manager)
MockConfigEntry(domain='test', entry_id='test3').add_to_manager(manager)
assert [item.entry_id for item in manager.async_entries()] == \
@ -352,7 +350,7 @@ def test_remove_entry_if_not_loaded(hass, manager):
assert [item.entry_id for item in manager.async_entries()] == \
['test1', 'test3']
assert len(mock_unload_entry.mock_calls) == 1
assert len(mock_unload_entry.mock_calls) == 0
@asyncio.coroutine
@ -360,8 +358,8 @@ def test_add_entry_calls_setup_entry(hass, manager):
"""Test we call setup_config_entry."""
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'comp',
mock_integration(
hass,
MockModule('comp', async_setup_entry=mock_setup_entry))
class TestFlow(config_entries.ConfigFlow):
@ -416,9 +414,8 @@ def test_domains_gets_uniques(manager):
async def test_saving_and_loading(hass):
"""Test that we're saving and loading correctly."""
loader.set_component(
hass, 'test',
MockModule('test', async_setup_entry=lambda *args: mock_coro(True)))
mock_integration(hass, MockModule(
'test', async_setup_entry=lambda *args: mock_coro(True)))
class TestFlow(config_entries.ConfigFlow):
VERSION = 5
@ -480,13 +477,13 @@ async def test_forward_entry_sets_up_component(hass):
entry = MockConfigEntry(domain='original')
mock_original_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'original',
mock_integration(
hass,
MockModule('original', async_setup_entry=mock_original_setup_entry))
mock_forwarded_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(
hass, 'forwarded',
mock_integration(
hass,
MockModule('forwarded', async_setup_entry=mock_forwarded_setup_entry))
await hass.config_entries.async_forward_entry_setup(entry, 'forwarded')
@ -500,7 +497,7 @@ async def test_forward_entry_does_not_setup_entry_if_setup_fails(hass):
mock_setup = MagicMock(return_value=mock_coro(False))
mock_setup_entry = MagicMock()
hass, loader.set_component(hass, 'forwarded', MockModule(
mock_integration(hass, MockModule(
'forwarded',
async_setup=mock_setup,
async_setup_entry=mock_setup_entry,
@ -513,7 +510,7 @@ async def test_forward_entry_does_not_setup_entry_if_setup_fails(hass):
async def test_discovery_notification(hass):
"""Test that we create/dismiss a notification when source is discovery."""
loader.set_component(hass, 'test', MockModule('test'))
mock_integration(hass, MockModule('test'))
await async_setup_component(hass, 'persistent_notification', {})
class TestFlow(config_entries.ConfigFlow):
@ -550,7 +547,7 @@ async def test_discovery_notification(hass):
async def test_discovery_notification_not_created(hass):
"""Test that we not create a notification when discovery is aborted."""
loader.set_component(hass, 'test', MockModule('test'))
mock_integration(hass, MockModule('test'))
await async_setup_component(hass, 'persistent_notification', {})
class TestFlow(config_entries.ConfigFlow):
@ -630,8 +627,8 @@ async def test_setup_raise_not_ready(hass, caplog):
entry = MockConfigEntry(domain='test')
mock_setup_entry = MagicMock(side_effect=ConfigEntryNotReady)
loader.set_component(
hass, 'test', MockModule('test', async_setup_entry=mock_setup_entry))
mock_integration(
hass, MockModule('test', async_setup_entry=mock_setup_entry))
with patch('homeassistant.helpers.event.async_call_later') as mock_call:
await entry.async_setup(hass)
@ -656,8 +653,8 @@ async def test_setup_retrying_during_unload(hass):
entry = MockConfigEntry(domain='test')
mock_setup_entry = MagicMock(side_effect=ConfigEntryNotReady)
loader.set_component(
hass, 'test', MockModule('test', async_setup_entry=mock_setup_entry))
mock_integration(
hass, MockModule('test', async_setup_entry=mock_setup_entry))
with patch('homeassistant.helpers.event.async_call_later') as mock_call:
await entry.async_setup(hass)
@ -718,7 +715,7 @@ async def test_entry_setup_succeed(hass, manager):
mock_setup = MagicMock(return_value=mock_coro(True))
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_setup=mock_setup,
async_setup_entry=mock_setup_entry
@ -748,7 +745,7 @@ async def test_entry_setup_invalid_state(hass, manager, state):
mock_setup = MagicMock(return_value=mock_coro(True))
mock_setup_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_setup=mock_setup,
async_setup_entry=mock_setup_entry
@ -772,7 +769,7 @@ async def test_entry_unload_succeed(hass, manager):
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_unload_entry=async_unload_entry
))
@ -797,7 +794,7 @@ async def test_entry_unload_failed_to_load(hass, manager, state):
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_unload_entry=async_unload_entry
))
@ -821,7 +818,7 @@ async def test_entry_unload_invalid_state(hass, manager, state):
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_unload_entry=async_unload_entry
))
@ -845,7 +842,7 @@ async def test_entry_reload_succeed(hass, manager):
async_setup_entry = MagicMock(return_value=mock_coro(True))
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_setup=async_setup,
async_setup_entry=async_setup_entry,
@ -876,7 +873,7 @@ async def test_entry_reload_not_loaded(hass, manager, state):
async_setup_entry = MagicMock(return_value=mock_coro(True))
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_setup=async_setup,
async_setup_entry=async_setup_entry,
@ -906,7 +903,7 @@ async def test_entry_reload_error(hass, manager, state):
async_setup_entry = MagicMock(return_value=mock_coro(True))
async_unload_entry = MagicMock(return_value=mock_coro(True))
loader.set_component(hass, 'comp', MockModule(
mock_integration(hass, MockModule(
'comp',
async_setup=async_setup,
async_setup_entry=async_setup_entry,