Speed up tests

This commit is contained in:
Paulus Schoutsen 2015-09-01 00:18:26 -07:00
parent a34b00bc9c
commit 58afbecd05
21 changed files with 98 additions and 82 deletions

2
.gitignore vendored
View File

@ -9,6 +9,8 @@ config/custom_components/*
!config/custom_components/hello_world.py !config/custom_components/hello_world.py
!config/custom_components/mqtt_example.py !config/custom_components/mqtt_example.py
tests/config/home-assistant.log
# Hide sublime text stuff # Hide sublime text stuff
*.sublime-project *.sublime-project
*.sublime-workspace *.sublime-workspace

View File

@ -10,7 +10,7 @@ import re
from homeassistant import core from homeassistant import core
from homeassistant.const import ( from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF) ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF)
DOMAIN = "conversation" DOMAIN = "conversation"
DEPENDENCIES = [] DEPENDENCIES = []
@ -44,7 +44,7 @@ def setup(hass, config):
entity_ids = [ entity_ids = [
state.entity_id for state in hass.states.all() state.entity_id for state in hass.states.all()
if state.attributes.get(ATTR_FRIENDLY_NAME, "").lower() == name] if state.name.lower() == name]
if not entity_ids: if not entity_ids:
logger.error( logger.error(

View File

@ -54,7 +54,7 @@ def ensure_config_exists(config_dir, detect_location=True):
config_path = find_config_file(config_dir) config_path = find_config_file(config_dir)
if config_path is None: if config_path is None:
print("Unable to find configuration. Creating default one at", print("Unable to find configuration. Creating default one in",
config_dir) config_dir)
config_path = create_default_config(config_dir, detect_location) config_path = create_default_config(config_dir, detect_location)

View File

@ -761,8 +761,10 @@ def create_timer(hass, interval=TIMER_INTERVAL):
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_timer) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_timer)
def create_worker_pool(worker_count=MIN_WORKER_THREAD): def create_worker_pool(worker_count=None):
""" Creates a worker pool to be used. """ """ Creates a worker pool to be used. """
if worker_count is None:
worker_count = MIN_WORKER_THREAD
def job_handler(job): def job_handler(job):
""" Called whenever a job is available to do. """ """ Called whenever a job is available to do. """

2
tests/__init__.py Normal file
View File

@ -0,0 +1,2 @@
import logging
logging.disable(logging.CRITICAL)

View File

@ -8,7 +8,7 @@ import os
from datetime import timedelta from datetime import timedelta
from unittest import mock from unittest import mock
import homeassistant.core as ha from homeassistant import core as ha, loader
import homeassistant.util.location as location_util import homeassistant.util.location as location_util
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
@ -38,6 +38,9 @@ def get_test_home_assistant(num_threads=None):
hass.config.latitude = 32.87336 hass.config.latitude = 32.87336
hass.config.longitude = -117.22743 hass.config.longitude = -117.22743
# if not loader.PREPARED:
loader. prepare(hass)
return hass return hass

View File

@ -21,7 +21,6 @@ class TestAutomationTime(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant() self.hass = ha.HomeAssistant()
loader.prepare(self.hass)
self.calls = [] self.calls = []
def record_call(service): def record_call(service):

View File

@ -7,6 +7,7 @@ Tests Home Assistant HTTP component does what it should do.
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import unittest import unittest
import json import json
from unittest.mock import patch
import requests import requests
@ -35,7 +36,9 @@ def _url(path=""):
return HTTP_BASE_URL + path return HTTP_BASE_URL + path
def setUpModule(): # pylint: disable=invalid-name @patch('homeassistant.components.http.util.get_local_ip',
return_value='127.0.0.1')
def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """ """ Initalizes a Home Assistant server. """
global hass global hass

View File

@ -6,13 +6,13 @@ Tests Conversation component.
""" """
# pylint: disable=too-many-public-methods,protected-access # pylint: disable=too-many-public-methods,protected-access
import unittest import unittest
from unittest.mock import patch
import homeassistant.components as core_components import homeassistant.components as core_components
import homeassistant.components.conversation as conversation from homeassistant.components import conversation
import homeassistant.components.demo as demo from homeassistant.const import ATTR_ENTITY_ID
import homeassistant.components.light as light
from common import get_test_home_assistant from tests.common import get_test_home_assistant
class TestConversation(unittest.TestCase): class TestConversation(unittest.TestCase):
@ -20,77 +20,92 @@ class TestConversation(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
""" Start up ha for testing """ """ Start up ha for testing """
self.ent_id = 'light.kitchen_lights'
self.hass = get_test_home_assistant(3) self.hass = get_test_home_assistant(3)
demo.setup(self.hass, {demo.DOMAIN: {}}) self.hass.states.set(self.ent_id, 'on')
core_components.setup(self.hass, {}) self.assertTrue(core_components.setup(self.hass, {}))
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """ """ Stop down stuff we started. """
self.hass.stop() self.hass.stop()
def test_setup_and_turn_on(self): def test_turn_on(self):
""" Setup and perform good turn on requests """ """ Setup and perform good turn on requests """
self.assertTrue( calls = []
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
light.turn_off(self.hass, 'light.kitchen_lights') def record_call(service):
calls.append(service)
self.hass.services.register('light', 'turn_on', record_call)
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights on'} event_data = {conversation.ATTR_TEXT: 'turn kitchen lights on'}
self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True) conversation.DOMAIN, 'process', event_data, True))
self.assertTrue( call = calls[-1]
light.is_on(self.hass, 'light.kitchen_lights')) self.assertEqual('light', call.domain)
self.assertEqual('turn_on', call.service)
self.assertEqual([self.ent_id], call.data[ATTR_ENTITY_ID])
def test_setup_and_turn_off(self): def test_turn_off(self):
""" Setup and perform good turn off requests """ """ Setup and perform good turn off requests """
self.assertTrue( calls = []
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
light.turn_on(self.hass, 'light.kitchen_lights') def record_call(service):
calls.append(service)
self.hass.services.register('light', 'turn_off', record_call)
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights off'} event_data = {conversation.ATTR_TEXT: 'turn kitchen lights off'}
self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True) conversation.DOMAIN, 'process', event_data, True))
self.assertFalse( call = calls[-1]
light.is_on(self.hass, 'light.kitchen_lights')) self.assertEqual('light', call.domain)
self.assertEqual('turn_off', call.service)
self.assertEqual([self.ent_id], call.data[ATTR_ENTITY_ID])
def test_setup_and_bad_request_format(self): @patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_format(self, mock_logger, mock_call):
""" Setup and perform a badly formatted request """ """ Setup and perform a badly formatted request """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
event_data = { event_data = {
conversation.ATTR_TEXT: conversation.ATTR_TEXT:
'what is the answer to the ultimate question of life, ' + 'what is the answer to the ultimate question of life, ' +
'the universe and everything'} 'the universe and everything'}
self.assertTrue(self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)) conversation.DOMAIN, 'process', event_data, True))
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
def test_setup_and_bad_request_entity(self): @patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_entity(self, mock_logger, mock_call):
""" Setup and perform requests with bad entity id """ """ Setup and perform requests with bad entity id """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
event_data = {conversation.ATTR_TEXT: 'turn something off'} event_data = {conversation.ATTR_TEXT: 'turn something off'}
self.assertTrue(self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)) conversation.DOMAIN, 'process', event_data, True))
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
def test_setup_and_bad_request_command(self): @patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_command(self, mock_logger, mock_call):
""" Setup and perform requests with bad command """ """ Setup and perform requests with bad command """
self.assertTrue( event_data = {conversation.ATTR_TEXT: 'turn kitchen lights over'}
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
event_data = {conversation.ATTR_TEXT: 'turn kitchen over'}
self.assertTrue(self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)) conversation.DOMAIN, 'process', event_data, True))
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
def test_setup_and_bad_request_notext(self): @patch('homeassistant.components.conversation.logging.Logger.error')
@patch('homeassistant.core.ServiceRegistry.call')
def test_bad_request_notext(self, mock_logger, mock_call):
""" Setup and perform requests with bad command with no text """ """ Setup and perform requests with bad command with no text """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
event_data = {} event_data = {}
self.assertTrue(self.hass.services.call( self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)) conversation.DOMAIN, 'process', event_data, True))
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)

View File

@ -15,8 +15,8 @@ from homeassistant.components import (
from tests.common import ( from tests.common import (
get_test_home_assistant, ensure_sun_risen, ensure_sun_set, get_test_config_dir, get_test_home_assistant, ensure_sun_risen,
trigger_device_tracker_scan) ensure_sun_set, trigger_device_tracker_scan)
KNOWN_DEV_PATH = None KNOWN_DEV_PATH = None
@ -26,14 +26,9 @@ def setUpModule(): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """ """ Initalizes a Home Assistant server. """
global KNOWN_DEV_PATH global KNOWN_DEV_PATH
hass = get_test_home_assistant() KNOWN_DEV_PATH = os.path.join(get_test_config_dir(),
loader.prepare(hass)
KNOWN_DEV_PATH = hass.config.path(
device_tracker.KNOWN_DEVICES_FILE) device_tracker.KNOWN_DEVICES_FILE)
hass.stop()
with open(KNOWN_DEV_PATH, 'w') as fil: with open(KNOWN_DEV_PATH, 'w') as fil:
fil.write('device,name,track,picture\n') fil.write('device,name,track,picture\n')
fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n') fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')

View File

@ -7,7 +7,6 @@ Tests the device tracker compoments.
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import unittest import unittest
from datetime import timedelta from datetime import timedelta
import logging
import os import os
import homeassistant.core as ha import homeassistant.core as ha
@ -21,18 +20,12 @@ import homeassistant.components.device_tracker as device_tracker
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
def setUpModule(): # pylint: disable=invalid-name
""" Setup to ignore group errors. """
logging.disable(logging.CRITICAL)
class TestComponentsDeviceTracker(unittest.TestCase): class TestComponentsDeviceTracker(unittest.TestCase):
""" Tests homeassistant.components.device_tracker module. """ """ Tests homeassistant.components.device_tracker module. """
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """ """ Init needed objects. """
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
loader.prepare(self.hass)
self.known_dev_path = self.hass.config.path( self.known_dev_path = self.hass.config.path(
device_tracker.KNOWN_DEVICES_FILE) device_tracker.KNOWN_DEVICES_FILE)

View File

@ -7,6 +7,7 @@ Tests Home Assistant HTTP component does what it should do.
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import re import re
import unittest import unittest
from unittest.mock import patch
import requests import requests
@ -34,7 +35,9 @@ def _url(path=""):
return HTTP_BASE_URL + path return HTTP_BASE_URL + path
def setUpModule(): # pylint: disable=invalid-name @patch('homeassistant.components.http.util.get_local_ip',
return_value='127.0.0.1')
def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """ """ Initalizes a Home Assistant server. """
global hass global hass

View File

@ -20,7 +20,6 @@ class TestComponentsCore(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """ """ Init needed objects. """
self.hass = ha.HomeAssistant() self.hass = ha.HomeAssistant()
loader.prepare(self.hass)
self.assertTrue(comps.setup(self.hass, {})) self.assertTrue(comps.setup(self.hass, {}))
self.hass.states.set('light.Bowl', STATE_ON) self.hass.states.set('light.Bowl', STATE_ON)

View File

@ -23,7 +23,6 @@ class TestLight(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
loader.prepare(self.hass)
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """ """ Stop down stuff we started. """

View File

@ -5,7 +5,6 @@ tests.test_component_media_player
Tests media_player component. Tests media_player component.
""" """
# pylint: disable=too-many-public-methods,protected-access # pylint: disable=too-many-public-methods,protected-access
import logging
import unittest import unittest
import homeassistant.core as ha import homeassistant.core as ha
@ -18,11 +17,6 @@ import homeassistant.components.media_player as media_player
from tests.common import mock_service from tests.common import mock_service
def setUpModule(): # pylint: disable=invalid-name
""" Setup to ignore media_player errors. """
logging.disable(logging.CRITICAL)
class TestMediaPlayer(unittest.TestCase): class TestMediaPlayer(unittest.TestCase):
""" Test the media_player module. """ """ Test the media_player module. """

View File

@ -77,7 +77,7 @@ class TestSun(unittest.TestCase):
""" Test if the state changes at next setting/rising. """ """ Test if the state changes at next setting/rising. """
self.hass.config.latitude = '32.87336' self.hass.config.latitude = '32.87336'
self.hass.config.longitude = '117.22743' self.hass.config.longitude = '117.22743'
sun.setup(self.hass, {}) sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
if sun.is_on(self.hass): if sun.is_on(self.hass):
test_state = sun.STATE_BELOW_HORIZON test_state = sun.STATE_BELOW_HORIZON

View File

@ -19,7 +19,6 @@ class TestSwitch(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
loader.prepare(self.hass)
platform = loader.get_component('switch.test') platform = loader.get_component('switch.test')

View File

@ -21,7 +21,6 @@ class TestComponentsCore(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """ """ Init needed objects. """
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
loader.prepare(self.hass)
self.hass.states.set('light.Bowl', STATE_ON) self.hass.states.set('light.Bowl', STATE_ON)
self.hass.states.set('light.Ceiling', STATE_OFF) self.hass.states.set('light.Ceiling', STATE_OFF)

View File

@ -49,13 +49,15 @@ class TestConfig(unittest.TestCase):
self.assertEqual(YAML_PATH, config_util.find_config_file(CONFIG_DIR)) self.assertEqual(YAML_PATH, config_util.find_config_file(CONFIG_DIR))
def test_ensure_config_exists_creates_config(self): @mock.patch('builtins.print')
def test_ensure_config_exists_creates_config(self, mock_print):
""" Test that calling ensure_config_exists creates a new config file if """ Test that calling ensure_config_exists creates a new config file if
none exists. """ none exists. """
config_util.ensure_config_exists(CONFIG_DIR, False) config_util.ensure_config_exists(CONFIG_DIR, False)
self.assertTrue(os.path.isfile(YAML_PATH)) self.assertTrue(os.path.isfile(YAML_PATH))
self.assertTrue(mock_print.called)
def test_ensure_config_exists_uses_existing_config(self): def test_ensure_config_exists_uses_existing_config(self):
""" Test that calling ensure_config_exists uses existing config. """ """ Test that calling ensure_config_exists uses existing config. """
@ -100,10 +102,11 @@ class TestConfig(unittest.TestCase):
self.assertEqual({'hello': 'world'}, self.assertEqual({'hello': 'world'},
config_util.load_config_file(YAML_PATH)) config_util.load_config_file(YAML_PATH))
def test_create_default_config_detect_location(self): @mock.patch('homeassistant.util.location.detect_location_info',
mock_detect_location_info)
@mock.patch('builtins.print')
def test_create_default_config_detect_location(self, mock_print):
""" Test that detect location sets the correct config keys. """ """ Test that detect location sets the correct config keys. """
with mock.patch('homeassistant.util.location.detect_location_info',
mock_detect_location_info):
config_util.ensure_config_exists(CONFIG_DIR) config_util.ensure_config_exists(CONFIG_DIR)
config = config_util.load_config_file(YAML_PATH) config = config_util.load_config_file(YAML_PATH)
@ -121,11 +124,15 @@ class TestConfig(unittest.TestCase):
} }
self.assertEqual(expected_values, ha_conf) self.assertEqual(expected_values, ha_conf)
self.assertTrue(mock_print.called)
def test_create_default_config_returns_none_if_write_error(self): @mock.patch('builtins.print')
def test_create_default_config_returns_none_if_write_error(self,
mock_print):
""" """
Test that writing default config to non existing folder returns None. Test that writing default config to non existing folder returns None.
""" """
self.assertIsNone( self.assertIsNone(
config_util.create_default_config( config_util.create_default_config(
os.path.join(CONFIG_DIR, 'non_existing_dir/'), False)) os.path.join(CONFIG_DIR, 'non_existing_dir/'), False))
self.assertTrue(mock_print.called)

View File

@ -17,7 +17,6 @@ class TestLoader(unittest.TestCase):
""" Test the loader module. """ """ Test the loader module. """
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
loader.prepare(self.hass)
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """ """ Stop down stuff we started. """

View File

@ -8,6 +8,7 @@ Uses port 8125 as a port that nothing runs on
""" """
# pylint: disable=protected-access,too-many-public-methods # pylint: disable=protected-access,too-many-public-methods
import unittest import unittest
from unittest.mock import patch
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.bootstrap as bootstrap import homeassistant.bootstrap as bootstrap
@ -29,7 +30,9 @@ def _url(path=""):
return HTTP_BASE_URL + path return HTTP_BASE_URL + path
def setUpModule(): # pylint: disable=invalid-name @patch('homeassistant.components.http.util.get_local_ip',
return_value='127.0.0.1')
def setUpModule(mock_get_local_ip): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server and Slave instance. """ """ Initalizes a Home Assistant server and Slave instance. """
global hass, slave, master_api, broken_api global hass, slave, master_api, broken_api