TestEventHandler.py
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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: