From 75e41a21c9f7af71eae28eb30e136184ad4d9932 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Wed, 15 Feb 2017 23:19:34 -0800 Subject: [PATCH] Clean up mock_coro (#6037) --- tests/common.py | 7 +------ tests/components/binary_sensor/test_ffmpeg.py | 4 ++-- tests/components/config/test_core.py | 4 ++-- tests/components/config/test_init.py | 4 ++-- .../test_microsoft_face_detect.py | 4 ++-- .../test_microsoft_face_identify.py | 4 ++-- tests/components/test_ffmpeg.py | 4 ++-- tests/components/test_init.py | 12 ++++++------ tests/components/test_microsoft_face.py | 16 ++++++++-------- tests/helpers/test_discovery.py | 2 +- tests/helpers/test_entity_component.py | 4 ++-- tests/test_config.py | 14 +++++++------- 12 files changed, 37 insertions(+), 42 deletions(-) diff --git a/tests/common.py b/tests/common.py index ec32d413597..ac6856c4a64 100644 --- a/tests/common.py +++ b/tests/common.py @@ -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): diff --git a/tests/components/binary_sensor/test_ffmpeg.py b/tests/components/binary_sensor/test_ffmpeg.py index ec06d025b8e..d2b999d1255 100644 --- a/tests/components/binary_sensor/test_ffmpeg.py +++ b/tests/components/binary_sensor/test_ffmpeg.py @@ -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'): diff --git a/tests/components/config/test_core.py b/tests/components/config/test_core.py index 1db50873906..fa5629e88c4 100644 --- a/tests/components/config/test_core.py +++ b/tests/components/config/test_core.py @@ -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 diff --git a/tests/components/config/test_init.py b/tests/components/config/test_init.py index dfe3c3fb436..07baec9e3ae 100644 --- a/tests/components/config/test_init.py +++ b/tests/components/config/test_init.py @@ -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() diff --git a/tests/components/image_processing/test_microsoft_face_detect.py b/tests/components/image_processing/test_microsoft_face_detect.py index 82fd54f1633..801db56ed20 100644 --- a/tests/components/image_processing/test_microsoft_face_detect.py +++ b/tests/components/image_processing/test_microsoft_face_detect.py @@ -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 = { diff --git a/tests/components/image_processing/test_microsoft_face_identify.py b/tests/components/image_processing/test_microsoft_face_identify.py index 8812c1c050e..c6490369859 100644 --- a/tests/components/image_processing/test_microsoft_face_identify.py +++ b/tests/components/image_processing/test_microsoft_face_identify.py @@ -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 = { diff --git a/tests/components/test_ffmpeg.py b/tests/components/test_ffmpeg.py index ccce59e754c..abc69a627de 100644 --- a/tests/components/test_ffmpeg.py +++ b/tests/components/test_ffmpeg.py @@ -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): diff --git a/tests/components/test_init.py b/tests/components/test_init.py index 14066d65f2a..0971f0a16bd 100644 --- a/tests/components/test_init.py +++ b/tests/components/test_init.py @@ -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) diff --git a/tests/components/test_microsoft_face.py b/tests/components/test_microsoft_face.py index 6dee9dc9a55..859ac9023af 100644 --- a/tests/components/test_microsoft_face.py +++ b/tests/components/test_microsoft_face.py @@ -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( diff --git a/tests/helpers/test_discovery.py b/tests/helpers/test_discovery.py index fcbec64cc98..628621e3c6e 100644 --- a/tests/helpers/test_discovery.py +++ b/tests/helpers/test_discovery.py @@ -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 = [] diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 59f75bdfb16..d5ae60cc18e 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -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) diff --git a/tests/test_config.py b/tests/test_config.py index fefc39544e3..748c5b5cc2d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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),