TestConnection.py
4.36 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import unittest
import mock
from os.path import dirname, realpath
from sys import argv, path
path.append(dirname(dirname(realpath(__file__))) + '/lib')
from Communication.Connection import Connection
class TestConnection(unittest.TestCase):
def setUp(self):
self._message = mock.Mock()
self._transport = mock.Mock()
self._protocol = mock.Mock()
self._parser = mock.Mock()
self._composer = mock.Mock()
self._bufsize = 11773
self._connection = Connection(
self._transport, self._protocol, self._bufsize)
def testHasPendingData(self):
self.assertFalse(self._connection.hasPendingData())
self._connection._write_buffer = '1234'
self.assertTrue(self._connection.hasPendingData())
def testIterInit(self):
self.assertEqual(self._connection.__iter__(), self._connection)
def testMessageIterator(self):
self._transport.remote = 1212
self._protocol.createMessage.return_value = self._message
self._protocol.getParser.return_value = self._parser
self._parser.parse.return_value = 0
self.assertRaises(StopIteration, self._connection.next)
self._protocol.createMessage.assert_called_once_with(1212)
self._protocol.getParser.assert_called_once_with()
self._parser.parse.assert_called_once_with(self._message, '')
self._transport.reset_mock()
self._protocol.reset_mock()
self._parser.reset_mock()
self._connection.appendReadData(('111222333', 1212))
self._transport.remote = 1212
self._protocol.getParser.return_value = self._parser
self._parser.parse.return_value = 3
self._message.ready.return_value = False
self.assertRaises(StopIteration, self._connection.next)
self._protocol.getParser.assert_called_once_with()
self._parser.parse.assert_called_once_with(self._message, '111222333')
self.assertEqual(self._message.ready.call_count, 2)
self._transport.reset_mock()
self._protocol.reset_mock()
self._parser.reset_mock()
self._transport.remote = 1212
self._protocol.getParser.return_value = self._parser
self._parser.parse.return_value = 3
self._message.ready.return_value = True
self.assertEqual(self._connection.next(), self._message)
self._protocol.createMessage.assert_called_once_with(1212)
self._protocol.getParser.assert_called_once_with()
self._parser.parse.assert_called_once_with(self._message, '222333')
self.assertEqual(self._message.ready.call_count, 2)
def testCompose(self):
self._protocol.getComposer.return_value = self._composer
self._composer.compose.return_value = '111222333'
self.assertTrue(self._connection.compose(self._message))
self.assertEqual(self._connection._write_buffer, '111222333')
self._composer.compose.side_effect = Exception('BOOM!')
self.assertFalse(self._connection.compose(self._message))
self.assertEqual(self._connection._write_buffer, '111222333')
def testAppendReadData(self):
self._connection.appendReadData(('111', 1212))
self.assertEqual(self._connection._read_buffer, '111')
self._connection.appendReadData(('222', 1212))
self.assertEqual(self._connection._read_buffer, '111222')
self._connection.appendReadData(('333', 1212))
self.assertEqual(self._connection._read_buffer, '111222333')
def testNextWriteData(self):
self._connection._write_buffer = '111222333'
self.assertEqual(self._connection.nextWriteData(), ('111222333', None))
self.assertEqual(self._connection._write_buffer, '')
def testAppendWriteData(self):
self._connection.appendWriteData(('111', 1212))
self.assertEqual(self._connection._write_buffer, '111')
self._connection.appendWriteData(('222', 1212))
self.assertEqual(self._connection._write_buffer, '111222')
self._connection.appendWriteData(('333', 1212))
self.assertEqual(self._connection._write_buffer, '111222333')
def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestConnection)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
# vim: set ft=python et ts=8 sw=4 sts=4: