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

View file

@ -6,13 +6,13 @@ Tests Conversation component.
"""
# 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):
@ -20,77 +20,92 @@ class TestConversation(unittest.TestCase):
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)