Renamed test to ha_test because of conflict with built-in python test package

This commit is contained in:
Paulus Schoutsen 2014-11-30 23:14:08 -08:00
parent 5835d502c7
commit ed150b8ea5
19 changed files with 170 additions and 137 deletions

View file

@ -0,0 +1,29 @@
"""
custom_components.light.test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides a mock switch platform.
Call init before using it in your tests to ensure clean test data.
"""
import homeassistant.components as components
from ha_test.helper import MockToggleDevice
DEVICES = []
def init(empty=False):
""" (re-)initalizes the platform with devices. """
global DEVICES
DEVICES = [] if empty else [
MockToggleDevice('Ceiling', components.STATE_ON),
MockToggleDevice('Ceiling', components.STATE_OFF),
MockToggleDevice(None, components.STATE_OFF)
]
def get_lights(hass, config):
""" Returns mock devices. """
return DEVICES

View file

@ -0,0 +1,29 @@
"""
custom_components.switch.test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Provides a mock switch platform.
Call init before using it in your tests to ensure clean test data.
"""
import homeassistant.components as components
from ha_test.helper import MockToggleDevice
DEVICES = []
def init(empty=False):
""" (re-)initalizes the platform with devices. """
global DEVICES
DEVICES = [] if empty else [
MockToggleDevice('AC', components.STATE_ON),
MockToggleDevice('AC', components.STATE_OFF),
MockToggleDevice(None, components.STATE_OFF)
]
def get_switches(hass, config):
""" Returns mock devices. """
return DEVICES

View file

@ -1,16 +1,48 @@
"""
test.mock.switch_platform
~~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.helper
~~~~~~~~~~~~~
Provides a mock switch platform.
Call init before using it in your tests to ensure clean test data.
Helper method for writing tests.
"""
import os
import homeassistant as ha
import homeassistant.components as components
def get_test_home_assistant():
""" Returns a Home Assistant object pointing at test config dir. """
hass = ha.HomeAssistant()
hass.config_dir = os.path.join(os.path.dirname(__file__), "config")
return hass
def mock_service(hass, domain, service):
"""
Sets up a fake service.
Returns a list that logs all calls to fake service.
"""
calls = []
hass.services.register(
domain, service, lambda call: calls.append(call))
return calls
class MockModule(object):
""" Provides a fake module. """
def __init__(self, domain, dependencies=[], setup=None):
self.DOMAIN = domain
self.DEPENDENCIES = dependencies
# Setup a mock setup if none given.
self.setup = lambda hass, config: False if setup is None else setup
class MockToggleDevice(components.ToggleDevice):
""" Fake switch. """
""" Provides a mock toggle device. """
def __init__(self, name, state):
self.name = name
self.state = state
@ -42,23 +74,3 @@ class MockToggleDevice(components.ToggleDevice):
else:
return next(call for call in reversed(self.calls)
if call[0] == method)
DEVICES = []
def init(empty=False):
""" (re-)initalizes the platform with devices. """
global DEVICES
DEVICES = [] if empty else [
MockToggleDevice('AC', components.STATE_ON),
MockToggleDevice('AC', components.STATE_OFF),
MockToggleDevice(None, components.STATE_OFF)
]
def get_switches(hass, config):
""" Returns mock devices. """
return DEVICES
get_lights = get_switches

View file

@ -1,6 +1,6 @@
"""
test.test_component_chromecast
~~~~~~~~~~~
ha_test.test_component_chromecast
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests Chromecast component.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_core
~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_core
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests core compoments.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_demo
~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_demo
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests demo component.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_group
~~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_group
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests the group compoments.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_http
~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_http
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests Home Assistant HTTP component does what it should do.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_switch
~~~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_switch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests switch component.
"""
@ -11,11 +11,11 @@ import os
import homeassistant as ha
import homeassistant.loader as loader
import homeassistant.util as util
import homeassistant.components as components
from homeassistant.components import (
get_component, ATTR_ENTITY_ID, STATE_ON, STATE_OFF,
SERVICE_TURN_ON, SERVICE_TURN_OFF)
import homeassistant.components.light as light
import mock_toggledevice_platform
from helper import mock_service, get_test_home_assistant
@ -25,7 +25,6 @@ class TestLight(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
self.hass = get_test_home_assistant()
loader.prepare(self.hass)
loader.set_component('light.test', mock_toggledevice_platform)
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
@ -39,21 +38,21 @@ class TestLight(unittest.TestCase):
def test_methods(self):
""" Test if methods call the services as expected. """
# Test is_on
self.hass.states.set('light.test', components.STATE_ON)
self.hass.states.set('light.test', STATE_ON)
self.assertTrue(light.is_on(self.hass, 'light.test'))
self.hass.states.set('light.test', components.STATE_OFF)
self.hass.states.set('light.test', STATE_OFF)
self.assertFalse(light.is_on(self.hass, 'light.test'))
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, components.STATE_ON)
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
self.assertTrue(light.is_on(self.hass))
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, components.STATE_OFF)
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_OFF)
self.assertFalse(light.is_on(self.hass))
# Test turn_on
turn_on_calls = mock_service(
self.hass, light.DOMAIN, components.SERVICE_TURN_ON)
self.hass, light.DOMAIN, SERVICE_TURN_ON)
light.turn_on(
self.hass,
@ -70,17 +69,19 @@ class TestLight(unittest.TestCase):
call = turn_on_calls[-1]
self.assertEqual(light.DOMAIN, call.domain)
self.assertEqual(components.SERVICE_TURN_ON, call.service)
self.assertEqual('entity_id_val', call.data[components.ATTR_ENTITY_ID])
self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
self.assertEqual('brightness_val', call.data[light.ATTR_BRIGHTNESS])
self.assertEqual('rgb_color_val', call.data[light.ATTR_RGB_COLOR])
self.assertEqual('xy_color_val', call.data[light.ATTR_XY_COLOR])
self.assertEqual('profile_val', call.data[light.ATTR_PROFILE])
self.assertEqual(SERVICE_TURN_ON, call.service)
self.assertEqual('entity_id_val', call.data.get(ATTR_ENTITY_ID))
self.assertEqual(
'transition_val', call.data.get(light.ATTR_TRANSITION))
self.assertEqual(
'brightness_val', call.data.get(light.ATTR_BRIGHTNESS))
self.assertEqual('rgb_color_val', call.data.get(light.ATTR_RGB_COLOR))
self.assertEqual('xy_color_val', call.data.get(light.ATTR_XY_COLOR))
self.assertEqual('profile_val', call.data.get(light.ATTR_PROFILE))
# Test turn_off
turn_off_calls = mock_service(
self.hass, light.DOMAIN, components.SERVICE_TURN_OFF)
self.hass, light.DOMAIN, SERVICE_TURN_OFF)
light.turn_off(
self.hass, entity_id='entity_id_val', transition='transition_val')
@ -91,17 +92,19 @@ class TestLight(unittest.TestCase):
call = turn_off_calls[-1]
self.assertEqual(light.DOMAIN, call.domain)
self.assertEqual(components.SERVICE_TURN_OFF, call.service)
self.assertEqual('entity_id_val', call.data[components.ATTR_ENTITY_ID])
self.assertEqual(SERVICE_TURN_OFF, call.service)
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
self.assertEqual('transition_val', call.data[light.ATTR_TRANSITION])
def test_services(self):
""" Test the provided services. """
mock_toggledevice_platform.init()
platform = get_component('light.test')
platform.init()
self.assertTrue(
light.setup(self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}}))
dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None)
dev1, dev2, dev3 = platform.get_lights(None, None)
# Test init
self.assertTrue(light.is_on(self.hass, dev1.entity_id))
@ -224,10 +227,10 @@ class TestLight(unittest.TestCase):
))
# Test if light component returns 0 lightes
mock_toggledevice_platform.init(True)
platform = get_component('light.test')
platform.init(True)
self.assertEqual(
[], mock_toggledevice_platform.get_lights(None, None))
self.assertEqual([], platform.get_lights(None, None))
self.assertFalse(light.setup(
self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}}
@ -235,7 +238,8 @@ class TestLight(unittest.TestCase):
def test_light_profiles(self):
""" Test light profiles. """
mock_toggledevice_platform.init()
platform = get_component('light.test')
platform.init()
user_light_file = self.hass.get_config_path(light.LIGHT_PROFILES_FILE)
@ -259,7 +263,7 @@ class TestLight(unittest.TestCase):
self.hass, {light.DOMAIN: {ha.CONF_TYPE: 'test'}}
))
dev1, dev2, dev3 = mock_toggledevice_platform.get_lights(None, None)
dev1, dev2, dev3 = platform.get_lights(None, None)
light.turn_on(self.hass, dev1.entity_id, profile='test')

View file

@ -1,6 +1,6 @@
"""
test.test_component_sun
~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_sun
~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests Sun component.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_component_switch
~~~~~~~~~~~~~~~~~~~~~~~~~~
ha_test.test_component_switch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tests switch component.
"""
@ -9,28 +9,29 @@ import unittest
import homeassistant as ha
import homeassistant.loader as loader
import homeassistant.components as components
from homeassistant.components import get_component, STATE_ON, STATE_OFF
import homeassistant.components.switch as switch
import mock_toggledevice_platform
from helper import get_test_home_assistant
class TestSwitch(unittest.TestCase):
""" Test the switch module. """
def setUp(self): # pylint: disable=invalid-name
self.hass = ha.HomeAssistant()
self.hass = get_test_home_assistant()
loader.prepare(self.hass)
loader.set_component('switch.test', mock_toggledevice_platform)
mock_toggledevice_platform.init()
platform = get_component('switch.test')
platform.init()
self.assertTrue(switch.setup(
self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}}
))
# Switch 1 is ON, switch 2 is OFF
self.switch_1, self.switch_2, self.switch_3 = \
mock_toggledevice_platform.get_switches(None, None)
platform.get_switches(None, None)
def tearDown(self): # pylint: disable=invalid-name
""" Stop down stuff we started. """
@ -40,7 +41,7 @@ class TestSwitch(unittest.TestCase):
""" Test is_on, turn_on, turn_off methods. """
self.assertTrue(switch.is_on(self.hass))
self.assertEqual(
components.STATE_ON,
STATE_ON,
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
@ -62,7 +63,7 @@ class TestSwitch(unittest.TestCase):
self.assertFalse(switch.is_on(self.hass))
self.assertEqual(
components.STATE_OFF,
STATE_OFF,
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
self.assertFalse(switch.is_on(self.hass, self.switch_1.entity_id))
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
@ -75,7 +76,7 @@ class TestSwitch(unittest.TestCase):
self.assertTrue(switch.is_on(self.hass))
self.assertEqual(
components.STATE_ON,
STATE_ON,
self.hass.states.get(switch.ENTITY_ID_ALL_SWITCHES).state)
self.assertTrue(switch.is_on(self.hass, self.switch_1.entity_id))
self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
@ -93,10 +94,10 @@ class TestSwitch(unittest.TestCase):
))
# Test if switch component returns 0 switches
mock_toggledevice_platform.init(True)
get_component('switch.test').init(True)
self.assertEqual(
[], mock_toggledevice_platform.get_switches(None, None))
[], get_component('switch.test').get_switches(None, None))
self.assertFalse(switch.setup(
self.hass, {switch.DOMAIN: {ha.CONF_TYPE: 'test'}}

View file

@ -1,6 +1,6 @@
"""
test.test_core
~~~~~~~~~~~~~~
ha_test.test_core
~~~~~~~~~~~~~~~~~
Provides tests to verify that Home Assistant core works.
"""

View file

@ -1,6 +1,6 @@
"""
test.test_loader
~~~~~~~~~~~~~~~~~~
ha_ha_test.test_loader
~~~~~~~~~~~~~~~~~~~~~~
Provides tests to verify that we can load components.
"""
@ -10,7 +10,6 @@ import unittest
import homeassistant.loader as loader
import homeassistant.components.http as http
import mock_toggledevice_platform
from helper import get_test_home_assistant, MockModule
@ -26,16 +25,15 @@ class TestLoader(unittest.TestCase):
def test_set_component(self):
""" Test if set_component works. """
loader.set_component('switch.test', mock_toggledevice_platform)
loader.set_component('switch.test', http)
self.assertEqual(
mock_toggledevice_platform, loader.get_component('switch.test'))
self.assertEqual(http, loader.get_component('switch.test'))
def test_get_component(self):
""" Test if get_component works. """
self.assertEqual(http, loader.get_component('http'))
self.assertIsNotNone(loader.get_component('custom_one'))
self.assertIsNotNone(loader.get_component('switch.test'))
def test_load_order_component(self):
""" Test if we can get the proper load order of components. """

View file

@ -1,6 +1,6 @@
"""
test.remote
~~~~~~~~~~~
ha_test.remote
~~~~~~~~~~~~~~
Tests Home Assistant remote methods and classes.
Uses port 8122 for master, 8123 for slave

View file

@ -1,6 +1,6 @@
"""
test.test_util
~~~~~~~~~~~~~~
ha_test.test_util
~~~~~~~~~~~~~~~~~
Tests Home Assistant util methods.
"""

View file

@ -62,9 +62,12 @@ def prepare(hass):
# just might output more errors.
for fil in os.listdir(custom_path):
if os.path.isdir(os.path.join(custom_path, fil)):
AVAILABLE_COMPONENTS.append('custom_components.{}'.format(fil))
if fil != '__pycache__':
AVAILABLE_COMPONENTS.append(
'custom_components.{}'.format(fil))
else:
# For files we will strip out .py extension
AVAILABLE_COMPONENTS.append(
'custom_components.{}'.format(fil[0:-3]))

View file

@ -2,4 +2,4 @@
pylint homeassistant
flake8 homeassistant --exclude bower_components,external
python3 -m unittest discover test
python3 -m unittest discover ha_test

View file

@ -1,3 +0,0 @@
"""
Module to be loaded by the Loader test.
"""

View file

@ -1,40 +0,0 @@
"""
test.helper
~~~~~~~~~~~
Helper method for writing tests.
"""
import os
import homeassistant as ha
def get_test_home_assistant():
""" Returns a Home Assistant object pointing at test config dir. """
hass = ha.HomeAssistant()
hass.config_dir = os.path.join(os.path.dirname(__file__), "config")
return hass
def mock_service(hass, domain, service):
"""
Sets up a fake service.
Returns a list that logs all calls to fake service.
"""
calls = []
hass.services.register(
domain, service, lambda call: calls.append(call))
return calls
class MockModule(object):
""" Provides a fake module. """
def __init__(self, domain, dependencies=[], setup=None):
self.DOMAIN = domain
self.DEPENDENCIES = dependencies
# Setup a mock setup if none given.
self.setup = lambda hass, config: False if setup is None else setup