TestDatagramService.py 4.96 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.DatagramService import DatagramService

class TestDatagramService(unittest.TestCase):
    def setUp(self):
        self._transport = mock.Mock()
        self._protocol  = mock.Mock()
        self._message   = mock.Mock()
        self._parser    = mock.Mock()
        self._composer  = mock.Mock()
        self._bufsize   = 22655
        self._msginfo   = ('111222333', 1212)
        self._datagram  = DatagramService(
            self._transport, self._protocol, self._bufsize)

        self._protocol.getParser.return_value   = self._parser
        self._protocol.getComposer.return_value = self._composer
        self._message.getRemote.return_value    = self._msginfo[1]

    def testHasPendingData(self):
        self.assertFalse(self._datagram.hasPendingData())
        self._datagram._write_buffer = '12345'
        self.assertTrue(self._datagram.hasPendingData())

    def testIterInit(self):
        self.assertEqual(self._datagram.__iter__(), self._datagram)

    def testMessageIteratorNoData(self):
        self.assertRaises(StopIteration, self._datagram.next)

    def testMessageIteratorCreateMessageFails(self):
        self._datagram._read_buffer = mock.Mock()
        self._datagram._read_buffer.popleft.return_value = self._msginfo
        self._protocol.createMessage.return_value = None
        self.assertRaises(StopIteration, self._datagram.next)
        self._datagram._read_buffer.popleft.assert_called_once_with()
        self._protocol.createMessage.assert_called_once_with(self._msginfo[1])

    def testMessageIteratorNoDataParsed(self):
        self._datagram._read_buffer = mock.Mock()
        self._datagram._read_buffer.popleft.return_value = self._msginfo
        self._protocol.createMessage.return_value = self._message
        self._parser.parse.return_value = 0
        self.assertRaises(StopIteration, self._datagram.next)
        self._datagram._read_buffer.popleft.assert_called_once_with()
        self._protocol.createMessage.assert_called_once_with(self._msginfo[1])
        self._parser.parse.assert_called_once_with(
            self._message, self._msginfo[0])

    def testMessageIteratorGetMessage(self):
        self._datagram._read_buffer = mock.Mock()
        self._datagram._read_buffer.popleft.return_value = self._msginfo
        self._protocol.createMessage.return_value = self._message
        self._parser.parse.return_value = 10
        self.assertEqual(self._datagram.next(), self._message)
        self._datagram._read_buffer.popleft.assert_called_once_with()
        self._protocol.createMessage.assert_called_once_with(self._msginfo[1])
        self._parser.parse.assert_called_once_with(
            self._message, self._msginfo[0])

    def testComposeSuccess(self):
        self._composer.compose.return_value = '111222333'
        self.assertTrue(self._datagram.compose(self._message))
        self.assertIn(
            ('111222333', self._msginfo[1]),
            self._datagram._write_buffer)

    def testComposeFail(self):
        self._composer.compose.side_effect = Exception('Boom!')
        self.assertFalse(self._datagram.compose(self._message))
        self.assertFalse(self._datagram._write_buffer)

    def testAppendReadData(self):
        self._datagram.appendReadData(('111', 1212))
        self.assertIn(('111', 1212), self._datagram._read_buffer)
        self._datagram.appendReadData(('222', 1212))
        self.assertIn(('111', 1212), self._datagram._read_buffer)
        self.assertIn(('222', 1212), self._datagram._read_buffer)
        self._datagram.appendReadData(('333', 1212))
        self.assertIn(('111', 1212), self._datagram._read_buffer)
        self.assertIn(('222', 1212), self._datagram._read_buffer)
        self.assertIn(('333', 1212), self._datagram._read_buffer)

    def testNextWriteData(self):
        self.assertEqual(self._datagram.nextWriteData(), ('', None))
        self._datagram._write_buffer.append(('111222333', 1212))
        self.assertEqual(self._datagram.nextWriteData(), ('111222333', 1212))
        self.assertNotIn(('111222333', 1212), self._datagram._write_buffer)

    def testAppendWriteData(self):
        self._datagram.appendWriteData(('111', 1212))
        self.assertIn(('111', 1212), self._datagram._write_buffer)
        self._datagram.appendWriteData(('222', 1212))
        self.assertIn(('111', 1212), self._datagram._write_buffer)
        self.assertIn(('222', 1212), self._datagram._write_buffer)
        self._datagram.appendWriteData(('333', 1212))
        self.assertIn(('111', 1212), self._datagram._write_buffer)
        self.assertIn(('222', 1212), self._datagram._write_buffer)
        self.assertIn(('333', 1212), self._datagram._write_buffer)

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

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

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