TestEventHandler.py 2.95 KB
import unittest
import mock

from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')

from Event.EventHandler import EventHandler

class HandlerOne(EventHandler):
    def __init__(self):
        super(HandlerOne, self).__init__()

        self._event_methods = {
                1 : self._handleOne,
                2 : self._handleTwo }

    def _handleOne(self, event):
        return 'one'

    def _handleTwo(self, event):
        return 'two'

class TestEventHandler(unittest.TestCase):
    def setUp(self):
        self._handler     = EventHandler()
        self._handler_one = HandlerOne()

        self._event_mock1       = mock.Mock()
        self._event_mock2       = mock.Mock()
        self._event_source_mock = mock.Mock()
        self._dispatcher_mock1  = mock.Mock()
        self._dispatcher_mock2  = mock.Mock()

        self._event_mock1.name    = 'a'
        self._event_mock1.type    = 1
        self._event_mock1.subject = self._event_source_mock
        self._event_mock1.data    = None

        self._event_mock2.name    = 'b'
        self._event_mock2.type    = 2
        self._event_mock2.subject = self._event_source_mock
        self._event_mock2.data    = 'arbitrary data'

        self._event_source_mock.emit.return_value = self._event_mock1

    def testEmptyHandlerSetDispatcher(self):
        self._handler.setDispatcher(self._dispatcher_mock1)
        self._handler.setDispatcher(self._dispatcher_mock2)
        self.assertIn(self._dispatcher_mock1, self._handler._dispatcher)
        self.assertIn(self._dispatcher_mock2, self._handler._dispatcher)

    def testEmptyHandlerGetHandledIds(self):
        self.assertEqual(self._handler.getHandledIds(), [])

    def testEmptyHandlerNoDispatcherIssueEvent(self):
        self._handler.issueEvent(self._event_source_mock, 'a', None)
        self._event_source_mock.emit.assert_called_once_with('a', None)

    def testEmptyHandlerIssueEvent(self):
        self._handler.setDispatcher(self._dispatcher_mock1)
        self._handler.setDispatcher(self._dispatcher_mock2)
        self._handler.issueEvent(self._event_source_mock, 'a', None)
        self._event_source_mock.emit.assert_called_once_with('a', None)
        self._dispatcher_mock1.queueEvent.called_once_with(self._event_mock1)
        self._dispatcher_mock2.queueEvent.called_once_with(self._event_mock1)

    def testEmptyHandlerHandleEvent(self):
        self.assertFalse(self._handler.handleEvent(self._event_mock1))
        self.assertFalse(self._handler.handleEvent(self._event_mock2))

    def testHandlerOneHandleEvent(self):
        self.assertEqual(self._handler_one.handleEvent(self._event_mock1), 'one')
        self.assertEqual(self._handler_one.handleEvent(self._event_mock2), 'two')

def suite():
    return unittest.TestLoader().loadTestsFromTestCase(TestEventHandler)

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

# vim: set ft=python et ts=8 sw=4 sts=4: