Clean up mock_coro (#6037)

pull/6039/head
Paulus Schoutsen 2017-02-15 23:19:34 -08:00 committed by GitHub
parent 9c176ad85a
commit 75e41a21c9
12 changed files with 37 additions and 42 deletions

View File

@ -393,12 +393,7 @@ def mock_coro(return_value=None):
"""Fake coroutine."""
return return_value
return coro
def mock_generator(return_value=None):
"""Helper method to return a coro generator that returns a value."""
return mock_coro(return_value)()
return coro()
def mock_coro_func(return_value=None):

View File

@ -37,7 +37,7 @@ class TestFFmpegNoiseSetup(object):
assert self.hass.data['ffmpeg'].binary == 'ffmpeg'
assert len(self.hass.data['ffmpeg'].entities) == 1
@patch('haffmpeg.SensorNoise.open_sensor', return_value=mock_coro()())
@patch('haffmpeg.SensorNoise.open_sensor', return_value=mock_coro())
def test_setup_component_start(self, mock_start):
"""Setup ffmpeg component."""
with assert_setup_component(1, 'binary_sensor'):
@ -85,7 +85,7 @@ class TestFFmpegMotionSetup(object):
assert self.hass.data['ffmpeg'].binary == 'ffmpeg'
assert len(self.hass.data['ffmpeg'].entities) == 1
@patch('haffmpeg.SensorMotion.open_sensor', return_value=mock_coro()())
@patch('haffmpeg.SensorMotion.open_sensor', return_value=mock_coro())
def test_setup_component_start(self, mock_start):
"""Setup ffmpeg component."""
with assert_setup_component(1, 'binary_sensor'):

View File

@ -20,7 +20,7 @@ def test_validate_config_ok(hass, test_client):
with patch(
'homeassistant.components.config.core.async_check_ha_config_file',
return_value=mock_coro(None)()):
return_value=mock_coro()):
resp = yield from client.post('/api/config/core/check_config')
assert resp.status == 200
@ -30,7 +30,7 @@ def test_validate_config_ok(hass, test_client):
with patch(
'homeassistant.components.config.core.async_check_ha_config_file',
return_value=mock_coro('beer')()):
return_value=mock_coro('beer')):
resp = yield from client.post('/api/config/core/check_config')
assert resp.status == 200

View File

@ -32,7 +32,7 @@ def test_load_on_demand_already_loaded(hass, test_client):
with patch.object(config, 'SECTIONS', []), \
patch.object(config, 'ON_DEMAND', ['zwave']), \
patch('homeassistant.components.config.zwave.async_setup') as stp:
stp.return_value = mock_coro(True)()
stp.return_value = mock_coro(True)
yield from async_setup_component(hass, 'config', {})
@ -51,7 +51,7 @@ def test_load_on_demand_on_load(hass, test_client):
assert 'config.zwave' not in hass.config.components
with patch('homeassistant.components.config.zwave.async_setup') as stp:
stp.return_value = mock_coro(True)()
stp.return_value = mock_coro(True)
hass.bus.async_fire(EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: 'zwave'})
yield from hass.async_block_till_done()

View File

@ -23,7 +23,7 @@ class TestMicrosoftFaceDetectSetup(object):
self.hass.stop()
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_platform(self, store_mock):
"""Setup platform with one entity."""
config = {
@ -49,7 +49,7 @@ class TestMicrosoftFaceDetectSetup(object):
'image_processing.microsoftface_demo_camera')
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_platform_name(self, store_mock):
"""Setup platform with one entity and set name."""
config = {

View File

@ -23,7 +23,7 @@ class TestMicrosoftFaceIdentifySetup(object):
self.hass.stop()
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_platform(self, store_mock):
"""Setup platform with one entity."""
config = {
@ -49,7 +49,7 @@ class TestMicrosoftFaceIdentifySetup(object):
'image_processing.microsoftface_demo_camera')
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_platform_name(self, store_mock):
"""Setup platform with one entity and set name."""
config = {

View File

@ -181,7 +181,7 @@ class TestFFmpegSetup(object):
assert len(manager._cache) == 0
@patch('haffmpeg.Test.run_test',
return_value=mock_coro(return_value=True)())
return_value=mock_coro(True))
def test_setup_component_test_run_test(self, mock_test):
"""Setup ffmpeg component test run_test."""
with assert_setup_component(2):
@ -204,7 +204,7 @@ class TestFFmpegSetup(object):
assert manager._cache['blabalblabla']
@patch('haffmpeg.Test.run_test',
return_value=mock_coro(return_value=False)())
return_value=mock_coro(False))
def test_setup_component_test_run_test_test_fail(self, mock_test):
"""Setup ffmpeg component test run_test."""
with assert_setup_component(2):

View File

@ -153,7 +153,7 @@ class TestComponentsCore(unittest.TestCase):
assert mock_process.called is False
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
return_value=mock_coro())
def test_stop_homeassistant(self, mock_stop):
"""Test stop service."""
comps.stop(self.hass)
@ -161,9 +161,9 @@ class TestComponentsCore(unittest.TestCase):
assert mock_stop.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
return_value=mock_coro())
@patch('homeassistant.config.async_check_ha_config_file',
return_value=mock_coro()())
return_value=mock_coro())
def test_restart_homeassistant(self, mock_check, mock_restart):
"""Test stop service."""
comps.restart(self.hass)
@ -172,7 +172,7 @@ class TestComponentsCore(unittest.TestCase):
assert mock_check.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
return_value=mock_coro())
@patch('homeassistant.config.async_check_ha_config_file',
side_effect=HomeAssistantError("Test error"))
def test_restart_homeassistant_wrong_conf(self, mock_check, mock_restart):
@ -183,9 +183,9 @@ class TestComponentsCore(unittest.TestCase):
assert not mock_restart.called
@patch('homeassistant.core.HomeAssistant.async_stop',
return_value=mock_coro()())
return_value=mock_coro())
@patch('homeassistant.config.async_check_ha_config_file',
return_value=mock_coro()())
return_value=mock_coro())
def test_check_config(self, mock_check, mock_stop):
"""Test stop service."""
comps.check_config(self.hass)

View File

@ -27,21 +27,21 @@ class TestMicrosoftFaceSetup(object):
self.hass.stop()
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_component(self, mock_update):
"""Setup component."""
with assert_setup_component(2, mf.DOMAIN):
setup_component(self.hass, mf.DOMAIN, self.config)
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_component_wrong_api_key(self, mock_update):
"""Setup component without api key."""
with assert_setup_component(0, mf.DOMAIN):
setup_component(self.hass, mf.DOMAIN, {mf.DOMAIN: {}})
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_setup_component_test_service(self, mock_update):
"""Setup component."""
with assert_setup_component(2, mf.DOMAIN):
@ -91,7 +91,7 @@ class TestMicrosoftFaceSetup(object):
'2ae4935b-9659-44c3-977f-61fac20d0538'
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_service_groups(self, mock_update, aioclient_mock):
"""Setup component, test groups services."""
aioclient_mock.put(
@ -171,7 +171,7 @@ class TestMicrosoftFaceSetup(object):
assert 'Hans' not in entity_group1.attributes
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_service_train(self, mock_update, aioclient_mock):
"""Setup component, test train groups services."""
with assert_setup_component(2, mf.DOMAIN):
@ -188,7 +188,7 @@ class TestMicrosoftFaceSetup(object):
assert len(aioclient_mock.mock_calls) == 1
@patch('homeassistant.components.camera.async_get_image',
return_value=mock_coro(return_value=b'Test')())
return_value=mock_coro(b'Test'))
def test_service_face(self, camera_mock, aioclient_mock):
"""Setup component, test person face services."""
aioclient_mock.get(
@ -225,7 +225,7 @@ class TestMicrosoftFaceSetup(object):
assert aioclient_mock.mock_calls[3][2] == b'Test'
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_service_status_400(self, mock_update, aioclient_mock):
"""Setup component, test groups services with error."""
aioclient_mock.put(
@ -244,7 +244,7 @@ class TestMicrosoftFaceSetup(object):
assert len(aioclient_mock.mock_calls) == 1
@patch('homeassistant.components.microsoft_face.'
'MicrosoftFace.update_store', return_value=mock_coro()())
'MicrosoftFace.update_store', return_value=mock_coro())
def test_service_status_timeout(self, mock_update, aioclient_mock):
"""Setup component, test groups services with timeout."""
aioclient_mock.put(

View File

@ -62,7 +62,7 @@ class TestHelpersDiscovery:
in calls_multi]
@patch('homeassistant.bootstrap.async_setup_component',
return_value=mock_coro(True)())
return_value=mock_coro(True))
def test_platform(self, mock_setup_component):
"""Test discover platform method."""
calls = []

View File

@ -298,9 +298,9 @@ class TestHelpersEntityComponent(unittest.TestCase):
assert platform2_setup.called
@patch('homeassistant.helpers.entity_component.EntityComponent'
'._async_setup_platform', return_value=mock_coro()())
'._async_setup_platform', return_value=mock_coro())
@patch('homeassistant.bootstrap.async_setup_component',
return_value=mock_coro(True)())
return_value=mock_coro(True))
def test_setup_does_discovery(self, mock_setup_component, mock_setup):
"""Test setup for discovery."""
component = EntityComponent(_LOGGER, DOMAIN, self.hass)

View File

@ -18,7 +18,7 @@ from homeassistant.util.async import run_coroutine_threadsafe
from homeassistant.helpers.entity import Entity
from tests.common import (
get_test_config_dir, get_test_home_assistant, mock_generator)
get_test_config_dir, get_test_home_assistant, mock_coro)
CONFIG_DIR = get_test_config_dir()
YAML_PATH = os.path.join(CONFIG_DIR, config_util.YAML_CONFIG_FILE)
@ -381,10 +381,10 @@ class TestConfig(unittest.TestCase):
"""Check that restart propagates to stop."""
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value': mock_generator(('output', 'error')),
'wait.return_value': mock_generator(0)}
'communicate.return_value': mock_coro(('output', 'error')),
'wait.return_value': mock_coro(0)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
mock_create.return_value = mock_coro(process_mock)
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass), self.hass.loop
@ -396,10 +396,10 @@ class TestConfig(unittest.TestCase):
process_mock = mock.MagicMock()
attrs = {
'communicate.return_value':
mock_generator(('\033[34mhello'.encode('utf-8'), 'error')),
'wait.return_value': mock_generator(1)}
mock_coro(('\033[34mhello'.encode('utf-8'), 'error')),
'wait.return_value': mock_coro(1)}
process_mock.configure_mock(**attrs)
mock_create.return_value = mock_generator(process_mock)
mock_create.return_value = mock_coro(process_mock)
assert run_coroutine_threadsafe(
config_util.async_check_ha_config_file(self.hass),