Ejemplo n.º 1
0
class TCPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertRaises(TypeError, self.connection.remaining)

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with self.assertRaises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.send.assert_called_once_with(bytearray.fromhex("7FAA"))
Ejemplo n.º 2
0
class TCPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertRaises(TypeError, self.connection.remaining)

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with self.assertRaises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.send.assert_called_once_with(
            bytearray.fromhex("7FAA"))
Ejemplo n.º 3
0
class TCPSocketConnectionTest:
    def setup_method(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        with pytest.raises(TypeError):
            self.connection.flush()

    def test_receive(self):
        with pytest.raises(TypeError):
            self.connection.receive("")

    def test_remaining(self):
        with pytest.raises(TypeError):
            self.connection.remaining()

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        assert self.connection.read(2) == bytearray.fromhex("7FAA")

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with pytest.raises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        # pytype: disable=attribute-error
        self.connection.socket.send.assert_called_once_with(
            bytearray.fromhex("7FAA"))