Skip to content

Commit

Permalink
Speed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Sep 1, 2015
1 parent a34b00b commit 58afbec
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 82 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ config/custom_components/*
!config/custom_components/hello_world.py
!config/custom_components/mqtt_example.py

tests/config/home-assistant.log

# Hide sublime text stuff
*.sublime-project
*.sublime-workspace
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from homeassistant import core
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"
DEPENDENCIES = []
Expand Down Expand Up @@ -44,7 +44,7 @@ def process(service):

entity_ids = [
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:
logger.error(
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def ensure_config_exists(config_dir, detect_location=True):
config_path = find_config_file(config_dir)

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_path = create_default_config(config_dir, detect_location)

Expand Down
4 changes: 3 additions & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,10 @@ def start_timer(event):
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. """
if worker_count is None:
worker_count = MIN_WORKER_THREAD

def job_handler(job):
""" Called whenever a job is available to do. """
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import logging
logging.disable(logging.CRITICAL)
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import timedelta
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.dt as dt_util
from homeassistant.helpers.entity import ToggleEntity
Expand Down Expand Up @@ -38,6 +38,9 @@ def get_test_home_assistant(num_threads=None):
hass.config.latitude = 32.87336
hass.config.longitude = -117.22743

# if not loader.PREPARED:
loader. prepare(hass)

return hass


Expand Down
1 change: 0 additions & 1 deletion tests/components/automation/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class TestAutomationTime(unittest.TestCase):

def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
loader.prepare(self.hass)
self.calls = []

def record_call(service):
Expand Down
5 changes: 4 additions & 1 deletion tests/components/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# pylint: disable=protected-access,too-many-public-methods
import unittest
import json
from unittest.mock import patch

import requests

Expand Down Expand Up @@ -35,7 +36,9 @@ def _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. """
global hass

Expand Down
93 changes: 54 additions & 39 deletions tests/components/test_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,106 @@
"""
# pylint: disable=too-many-public-methods,protected-access
import unittest
from unittest.mock import patch

import homeassistant.components as core_components
import homeassistant.components.conversation as conversation
import homeassistant.components.demo as demo
import homeassistant.components.light as light
from homeassistant.components import conversation
from homeassistant.const import ATTR_ENTITY_ID

from common import get_test_home_assistant
from tests.common import get_test_home_assistant


class TestConversation(unittest.TestCase):
""" Test the conversation component. """

def setUp(self): # pylint: disable=invalid-name
""" Start up ha for testing """
self.ent_id = 'light.kitchen_lights'
self.hass = get_test_home_assistant(3)
demo.setup(self.hass, {demo.DOMAIN: {}})
core_components.setup(self.hass, {})
self.hass.states.set(self.ent_id, 'on')
self.assertTrue(core_components.setup(self.hass, {}))
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))

def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()

def test_setup_and_turn_on(self):
def test_turn_on(self):
""" Setup and perform good turn on requests """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
calls = []

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'}
self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))

self.assertTrue(
light.is_on(self.hass, 'light.kitchen_lights'))
call = calls[-1]
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 """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))
calls = []

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'}
self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True)
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))

self.assertFalse(
light.is_on(self.hass, 'light.kitchen_lights'))
call = calls[-1]
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 """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))

event_data = {
conversation.ATTR_TEXT:
'what is the answer to the ultimate question of life, ' +
'the universe and everything'}
self.assertTrue(self.hass.services.call(
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 """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))

event_data = {conversation.ATTR_TEXT: 'turn something off'}
self.assertTrue(self.hass.services.call(
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 """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))

event_data = {conversation.ATTR_TEXT: 'turn kitchen over'}
event_data = {conversation.ATTR_TEXT: 'turn kitchen lights over'}
self.assertTrue(self.hass.services.call(
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 """
self.assertTrue(
conversation.setup(self.hass, {conversation.DOMAIN: {}}))

event_data = {}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
self.assertTrue(mock_logger.called)
self.assertFalse(mock_call.called)
13 changes: 4 additions & 9 deletions tests/components/test_device_sun_light_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@


from tests.common import (
get_test_home_assistant, ensure_sun_risen, ensure_sun_set,
trigger_device_tracker_scan)
get_test_config_dir, get_test_home_assistant, ensure_sun_risen,
ensure_sun_set, trigger_device_tracker_scan)


KNOWN_DEV_PATH = None
Expand All @@ -26,13 +26,8 @@ def setUpModule(): # pylint: disable=invalid-name
""" Initalizes a Home Assistant server. """
global KNOWN_DEV_PATH

hass = get_test_home_assistant()

loader.prepare(hass)
KNOWN_DEV_PATH = hass.config.path(
device_tracker.KNOWN_DEVICES_FILE)

hass.stop()
KNOWN_DEV_PATH = os.path.join(get_test_config_dir(),
device_tracker.KNOWN_DEVICES_FILE)

with open(KNOWN_DEV_PATH, 'w') as fil:
fil.write('device,name,track,picture\n')
Expand Down
7 changes: 0 additions & 7 deletions tests/components/test_device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# pylint: disable=protected-access,too-many-public-methods
import unittest
from datetime import timedelta
import logging
import os

import homeassistant.core as ha
Expand All @@ -21,18 +20,12 @@
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):
""" Tests homeassistant.components.device_tracker module. """

def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = get_test_home_assistant()
loader.prepare(self.hass)

self.known_dev_path = self.hass.config.path(
device_tracker.KNOWN_DEVICES_FILE)
Expand Down
5 changes: 4 additions & 1 deletion tests/components/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# pylint: disable=protected-access,too-many-public-methods
import re
import unittest
from unittest.mock import patch

import requests

Expand Down Expand Up @@ -34,7 +35,9 @@ def _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. """
global hass

Expand Down
1 change: 0 additions & 1 deletion tests/components/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class TestComponentsCore(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = ha.HomeAssistant()
loader.prepare(self.hass)
self.assertTrue(comps.setup(self.hass, {}))

self.hass.states.set('light.Bowl', STATE_ON)
Expand Down
1 change: 0 additions & 1 deletion tests/components/test_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class TestLight(unittest.TestCase):

def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant()
loader.prepare(self.hass)

def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
Expand Down
6 changes: 0 additions & 6 deletions tests/components/test_media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Tests media_player component.
"""
# pylint: disable=too-many-public-methods,protected-access
import logging
import unittest

import homeassistant.core as ha
Expand All @@ -18,11 +17,6 @@
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):
""" Test the media_player module. """

Expand Down
2 changes: 1 addition & 1 deletion tests/components/test_sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_state_change(self):
""" Test if the state changes at next setting/rising. """
self.hass.config.latitude = '32.87336'
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):
test_state = sun.STATE_BELOW_HORIZON
Expand Down
1 change: 0 additions & 1 deletion tests/components/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class TestSwitch(unittest.TestCase):

def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant()
loader.prepare(self.hass)

platform = loader.get_component('switch.test')

Expand Down
1 change: 0 additions & 1 deletion tests/helpers/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class TestComponentsCore(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
""" Init needed objects. """
self.hass = get_test_home_assistant()
loader.prepare(self.hass)

self.hass.states.set('light.Bowl', STATE_ON)
self.hass.states.set('light.Ceiling', STATE_OFF)
Expand Down
Loading

0 comments on commit 58afbec

Please sign in to comment.