TestConnection.py 4.36 KB
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: