Test remote methods for errors

This commit is contained in:
Paulus Schoutsen 2014-11-28 21:01:44 -08:00
parent 63f8f2ee7f
commit cdccdb432a
2 changed files with 29 additions and 9 deletions

View File

@ -340,7 +340,7 @@ def fire_event(api, event_type, data=None):
req.status_code, req.text) req.status_code, req.text)
except ha.HomeAssistantError: except ha.HomeAssistantError:
pass _LOGGER.exception("Error firing event")
def get_state(api, entity_id): def get_state(api, entity_id):
@ -376,7 +376,7 @@ def get_states(api):
# ValueError if req.json() can't parse the json # ValueError if req.json() can't parse the json
_LOGGER.exception("Error fetching states") _LOGGER.exception("Error fetching states")
return {} return []
def set_state(api, entity_id, new_state, attributes=None): def set_state(api, entity_id, new_state, attributes=None):

View File

@ -4,10 +4,10 @@ test.remote
Tests Home Assistant remote methods and classes. Tests Home Assistant remote methods and classes.
Uses port 8122 for master, 8123 for slave Uses port 8122 for master, 8123 for slave
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
import json
import homeassistant as ha import homeassistant as ha
import homeassistant.remote as remote import homeassistant.remote as remote
@ -19,7 +19,7 @@ HTTP_BASE_URL = "http://127.0.0.1:{}".format(remote.SERVER_PORT)
HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD} HA_HEADERS = {remote.AUTH_HEADER: API_PASSWORD}
hass, slave, master_api = None, None, None hass, slave, master_api, broken_api = None, None, None, None
def _url(path=""): def _url(path=""):
@ -29,7 +29,7 @@ def _url(path=""):
def setUpModule(): # pylint: disable=invalid-name def setUpModule(): # 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 global hass, slave, master_api, broken_api
hass = ha.HomeAssistant() hass = ha.HomeAssistant()
@ -49,6 +49,9 @@ def setUpModule(): # pylint: disable=invalid-name
slave.start() slave.start()
# Setup API pointing at nothing
broken_api = remote.API("127.0.0.1", "", 8125)
def tearDownModule(): # pylint: disable=invalid-name def tearDownModule(): # pylint: disable=invalid-name
""" Stops the Home Assistant server and slave. """ """ Stops the Home Assistant server and slave. """
@ -70,8 +73,7 @@ class TestRemoteMethods(unittest.TestCase):
remote.API("127.0.0.1", API_PASSWORD + "A"))) remote.API("127.0.0.1", API_PASSWORD + "A")))
self.assertEqual(remote.APIStatus.CANNOT_CONNECT, self.assertEqual(remote.APIStatus.CANNOT_CONNECT,
remote.validate_api( remote.validate_api(broken_api))
remote.API("127.0.0.1", API_PASSWORD, 8125)))
def test_get_event_listeners(self): def test_get_event_listeners(self):
""" Test Python API get_event_listeners. """ """ Test Python API get_event_listeners. """
@ -84,6 +86,8 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual(len(local_data), 0) self.assertEqual(len(local_data), 0)
self.assertEqual({}, remote.get_event_listeners(broken_api))
def test_fire_event(self): def test_fire_event(self):
""" Test Python API fire_event. """ """ Test Python API fire_event. """
test_value = [] test_value = []
@ -100,6 +104,9 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual(1, len(test_value)) self.assertEqual(1, len(test_value))
# Should not trigger any exception
remote.fire_event(broken_api, "test.event_no_data")
def test_get_state(self): def test_get_state(self):
""" Test Python API get_state. """ """ Test Python API get_state. """
@ -107,11 +114,13 @@ class TestRemoteMethods(unittest.TestCase):
hass.states.get('test.test'), hass.states.get('test.test'),
remote.get_state(master_api, 'test.test')) remote.get_state(master_api, 'test.test'))
self.assertEqual(None, remote.get_state(broken_api, 'test.test'))
def test_get_states(self): def test_get_states(self):
""" Test Python API get_state_entity_ids. """ """ Test Python API get_state_entity_ids. """
self.assertEqual( self.assertEqual(hass.states.all(), remote.get_states(master_api))
remote.get_states(master_api), hass.states.all()) self.assertEqual([], remote.get_states(broken_api))
def test_set_state(self): def test_set_state(self):
""" Test Python API set_state. """ """ Test Python API set_state. """
@ -119,6 +128,8 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual('set_test', hass.states.get('test.test').state) self.assertEqual('set_test', hass.states.get('test.test').state)
self.assertFalse(remote.set_state(broken_api, 'test.test', 'set_test'))
def test_is_state(self): def test_is_state(self):
""" Test Python API is_state. """ """ Test Python API is_state. """
@ -126,6 +137,10 @@ class TestRemoteMethods(unittest.TestCase):
remote.is_state(master_api, 'test.test', remote.is_state(master_api, 'test.test',
hass.states.get('test.test').state)) hass.states.get('test.test').state))
self.assertFalse(
remote.is_state(broken_api, 'test.test',
hass.states.get('test.test').state))
def test_get_services(self): def test_get_services(self):
""" Test Python API get_services. """ """ Test Python API get_services. """
@ -136,6 +151,8 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual(local, serv_domain["services"]) self.assertEqual(local, serv_domain["services"])
self.assertEqual({}, remote.get_services(broken_api))
def test_call_service(self): def test_call_service(self):
""" Test Python API call_service. """ """ Test Python API call_service. """
test_value = [] test_value = []
@ -152,6 +169,9 @@ class TestRemoteMethods(unittest.TestCase):
self.assertEqual(1, len(test_value)) self.assertEqual(1, len(test_value))
# Should not raise an exception
remote.call_service(broken_api, "test_domain", "test_service")
class TestRemoteClasses(unittest.TestCase): class TestRemoteClasses(unittest.TestCase):
""" Test the homeassistant.remote module. """ """ Test the homeassistant.remote module. """