Last pieces of test coverage for core classes

This commit is contained in:
Paulus Schoutsen 2014-11-22 22:37:53 -08:00
parent 5943f757a0
commit b94ab32d60
2 changed files with 74 additions and 13 deletions

View File

@ -318,11 +318,11 @@ class Event(object):
# pylint: disable=maybe-no-member # pylint: disable=maybe-no-member
if self.data: if self.data:
return "<Event {}[{}]: {}>".format( return "<Event {}[{}]: {}>".format(
self.event_type, self.origin.value[0], self.event_type, str(self.origin)[0],
util.repr_helper(self.data)) util.repr_helper(self.data))
else: else:
return "<Event {}[{}]>".format(self.event_type, return "<Event {}[{}]>".format(self.event_type,
self.origin.value[0]) str(self.origin)[0])
class EventBus(object): class EventBus(object):
@ -386,9 +386,9 @@ class EventBus(object):
if not self._listeners[event_type]: if not self._listeners[event_type]:
self._listeners.pop(event_type) self._listeners.pop(event_type)
except (KeyError, AttributeError): except (KeyError, ValueError):
# KeyError is key event_type listener did not exist # KeyError is key event_type listener did not exist
# AttributeError if listener did not exist within event_type # ValueError if listener did not exist within event_type
pass pass

View File

@ -5,11 +5,13 @@ homeassistant.test
Provides tests to verify that Home Assistant modules do what they should do. Provides tests to verify that Home Assistant modules do what they should do.
""" """
# pylint: disable=protected-access # pylint: disable=protected-access,too-many-public-methods
import os import os
import unittest import unittest
import time
import json import json
import threading
from datetime import datetime from datetime import datetime
import requests import requests
@ -76,7 +78,6 @@ def ensure_slave_started():
return HAHelper.slave return HAHelper.slave
# pylint: disable=too-many-public-methods
class TestHomeAssistant(unittest.TestCase): class TestHomeAssistant(unittest.TestCase):
""" """
Tests the Home Assistant core classes. Tests the Home Assistant core classes.
@ -99,7 +100,27 @@ class TestHomeAssistant(unittest.TestCase):
def test_block_till_stoped(self): def test_block_till_stoped(self):
""" Test if we can block till stop service is called. """ """ Test if we can block till stop service is called. """
pass blocking_thread = threading.Thread(target=self.hass.block_till_stopped)
self.assertFalse(blocking_thread.is_alive())
blocking_thread.start()
# Python will now give attention to the other thread
time.sleep(.01)
self.assertTrue(blocking_thread.is_alive())
self.hass.call_service(ha.DOMAIN, ha.SERVICE_HOMEASSISTANT_STOP)
self.hass._pool.block_till_done()
# hass.block_till_stopped checks every second if it should quit
# we have to wait worst case 1 second
wait_loops = 0
while blocking_thread.is_alive() and wait_loops < 10:
wait_loops += 1
time.sleep(0.1)
self.assertFalse(blocking_thread.is_alive())
def test_get_entity_ids(self): def test_get_entity_ids(self):
""" Test get_entity_ids method. """ """ Test get_entity_ids method. """
@ -224,7 +245,21 @@ class TestHomeAssistant(unittest.TestCase):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now}) self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
# pylint: disable=too-many-public-methods class TestEvent(unittest.TestCase):
""" Test Event class. """
def test_repr(self):
""" Test that repr method works. #MoreCoverage """
self.assertEqual(
"<Event TestEvent[L]>",
str(ha.Event("TestEvent")))
self.assertEqual(
"<Event TestEvent[R]: beer=nice>",
str(ha.Event("TestEvent",
{"beer": "nice"},
ha.EventOrigin.remote)))
class TestEventBus(unittest.TestCase): class TestEventBus(unittest.TestCase):
""" Test EventBus methods. """ """ Test EventBus methods. """
@ -243,12 +278,17 @@ class TestEventBus(unittest.TestCase):
self.assertEqual(old_count + 1, len(self.bus.listeners)) self.assertEqual(old_count + 1, len(self.bus.listeners))
self.bus.remove_listener('test', listener) # Try deleting a non registered listener, nothing should happen
self.bus.remove_listener('test', lambda x: len)
# Remove listener
self.bus.remove_listener('test', listener)
self.assertEqual(old_count, len(self.bus.listeners)) self.assertEqual(old_count, len(self.bus.listeners))
# Try deleting listener while category doesn't exist either
self.bus.remove_listener('test', listener)
# pylint: disable=too-many-public-methods
class TestState(unittest.TestCase): class TestState(unittest.TestCase):
""" Test EventBus methods. """ """ Test EventBus methods. """
@ -258,8 +298,18 @@ class TestState(unittest.TestCase):
ha.InvalidEntityFormatError, ha.State, ha.InvalidEntityFormatError, ha.State,
'invalid_entity_format', 'test_state') 'invalid_entity_format', 'test_state')
def test_repr(self):
""" Test state.repr """
self.assertEqual("<state on @ 12:00:00 08-12-1984>",
str(ha.State(
"happy.happy", "on",
last_changed=datetime(1984, 12, 8, 12, 0, 0))))
self.assertEqual("<state on:brightness=144 @ 12:00:00 08-12-1984>",
str(ha.State("happy.happy", "on", {"brightness": 144},
datetime(1984, 12, 8, 12, 0, 0))))
# pylint: disable=too-many-public-methods
class TestStateMachine(unittest.TestCase): class TestStateMachine(unittest.TestCase):
""" Test EventBus methods. """ """ Test EventBus methods. """
@ -286,7 +336,19 @@ class TestStateMachine(unittest.TestCase):
self.assertFalse(self.states.remove('light.Bowl')) self.assertFalse(self.states.remove('light.Bowl'))
# pylint: disable=too-many-public-methods class TestServiceCall(unittest.TestCase):
""" Test ServiceCall class. """
def test_repr(self):
""" Test repr method. """
self.assertEqual(
"<ServiceCall homeassistant.start>",
str(ha.ServiceCall('homeassistant', 'start')))
self.assertEqual(
"<ServiceCall homeassistant.start: fast=yes>",
str(ha.ServiceCall('homeassistant', 'start', {"fast": "yes"})))
class TestServiceRegistry(unittest.TestCase): class TestServiceRegistry(unittest.TestCase):
""" Test EventBus methods. """ """ Test EventBus methods. """
@ -303,7 +365,6 @@ class TestServiceRegistry(unittest.TestCase):
self.services.has_service("test_domain", "test_service")) self.services.has_service("test_domain", "test_service"))
# pylint: disable=too-many-public-methods
class TestHTTP(unittest.TestCase): class TestHTTP(unittest.TestCase):
""" Test the HTTP debug interface and API. """ """ Test the HTTP debug interface and API. """