Add tests, fix styling

This commit is contained in:
Paulus Schoutsen
2015-01-19 21:39:24 -08:00
parent 980ecdaacb
commit cdbcc844cf
9 changed files with 265 additions and 60 deletions

View File

@@ -24,6 +24,11 @@ def init(empty=False):
]
def get_lights(hass, config):
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Returns mock devices. """
add_devices_callback(DEVICES)
def get_lights():
""" Helper method to get current light objects. """
return DEVICES

View File

@@ -0,0 +1,118 @@
"""
tests.test_component_configurator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests Configurator component.
"""
# pylint: disable=too-many-public-methods,protected-access
import unittest
import time
import homeassistant as ha
import homeassistant.components.configurator as configurator
class TestConfigurator(unittest.TestCase):
""" Test the chromecast module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
self.hass.stop()
def test_request_least_info(self):
""" Test request config with least amount of data. """
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: None)
self.assertEqual(
1, len(self.hass.services.services.get(configurator.DOMAIN, [])),
"No new service registered")
states = self.hass.states.all()
self.assertEqual(1, len(states), "Expected a new state registered")
state = states[0]
self.assertEqual(configurator.STATE_CONFIGURE, state.state)
self.assertEqual(
request_id, state.attributes.get(configurator.ATTR_CONFIGURE_ID))
def test_request_all_info(self):
""" Test request config with all possible info. """
values = [
"config_description", "config image url",
"config submit caption", []]
keys = [
configurator.ATTR_DESCRIPTION, configurator.ATTR_DESCRIPTION_IMAGE,
configurator.ATTR_SUBMIT_CAPTION, configurator.ATTR_FIELDS]
exp_attr = dict(zip(keys, values))
exp_attr[configurator.ATTR_CONFIGURE_ID] = configurator.request_config(
self.hass, "Test Request", lambda _: None,
*values)
states = self.hass.states.all()
self.assertEqual(1, len(states))
state = states[0]
self.assertEqual(configurator.STATE_CONFIGURE, state.state)
self.assertEqual(exp_attr, state.attributes)
def test_callback_called_on_configure(self):
""" Test if our callback gets called when configure service called. """
calls = []
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: calls.append(1))
self.hass.services.call(
configurator.DOMAIN, configurator.SERVICE_CONFIGURE,
{configurator.ATTR_CONFIGURE_ID: request_id})
self.hass.pool.block_till_done()
self.assertEqual(1, len(calls), "Callback not called")
def test_state_change_on_notify_errors(self):
""" Test state change on notify errors. """
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: None)
error = "Oh no bad bad bad"
configurator.notify_errors(request_id, error)
state = self.hass.states.all()[0]
self.assertEqual(error, state.attributes.get(configurator.ATTR_ERRORS))
def test_notify_errors_fail_silently_on_bad_request_id(self):
""" Test if notify errors fails silently with a bad request id. """
configurator.notify_errors(2015, "Try this error")
def test_request_done_works(self):
""" Test if calling request done works. """
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: None)
configurator.request_done(request_id)
self.assertEqual(1, len(self.hass.states.all()))
time.sleep(.02)
self.assertEqual(0, len(self.hass.states.all()))
def test_request_done_fail_silently_on_bad_request_id(self):
""" Test that request_done fails silently with a bad request id. """
configurator.request_done(2016)

View File

@@ -8,7 +8,6 @@ Tests switch component.
import unittest
import os
import homeassistant as ha
import homeassistant.loader as loader
import homeassistant.util as util
from homeassistant.const import (
@@ -104,7 +103,7 @@ class TestLight(unittest.TestCase):
self.assertTrue(
light.setup(self.hass, {light.DOMAIN: {CONF_TYPE: 'test'}}))
dev1, dev2, dev3 = platform.get_lights(None, None)
dev1, dev2, dev3 = platform.get_lights()
# Test init
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
@@ -214,7 +213,7 @@ class TestLight(unittest.TestCase):
light.ATTR_XY_COLOR: [prof_x, prof_y]},
data)
def test_light_profiles(self):
def test_broken_light_profiles(self):
""" Test light profiles. """
platform = loader.get_component('light.test')
platform.init()
@@ -230,8 +229,12 @@ class TestLight(unittest.TestCase):
self.hass, {light.DOMAIN: {CONF_TYPE: 'test'}}
))
# Clean up broken file
os.remove(user_light_file)
def test_light_profiles(self):
""" Test light profiles. """
platform = loader.get_component('light.test')
platform.init()
user_light_file = self.hass.get_config_path(light.LIGHT_PROFILES_FILE)
with open(user_light_file, 'w') as user_file:
user_file.write('id,x,y,brightness\n')
@@ -241,7 +244,7 @@ class TestLight(unittest.TestCase):
self.hass, {light.DOMAIN: {CONF_TYPE: 'test'}}
))
dev1, dev2, dev3 = platform.get_lights(None, None)
dev1, dev2, dev3 = platform.get_lights()
light.turn_on(self.hass, dev1.entity_id, profile='test')