Example #1
0
 def test_json_ndarray(self):
     # Arrange
     m = 10
     expected = np.arange(m)
     
     # Act
     dumped = dumps(expected)
     result = loads(dumped)
     
     # Assert
     self.assertEqual(expected.shape, result.shape, "Shape mismatch")
     self.assertEqual(expected.dtype, result.dtype, "Type mismatch %s != %s" % (expected.dtype, result.dtype))
     self.assertTrue(np.allclose(expected, result), "Value mismatch")
Example #2
0
 def test_json_ndarray_in_list(self):
     # Arrange
     m = 10
     expected = [np.arange(i) for i in range(m)]
     
     # Act
     dumped = dumps(expected)
     result = loads(dumped)
     
     # Assert
     for i, expected_array in enumerate(expected):
         result_array = result[i]
         self.assertEqual(expected_array.shape, result_array.shape, "Shape mismatch")
         self.assertEqual(expected_array.dtype, result_array.dtype, "Type mismatch %s != %s" % (expected_array.dtype, result_array.dtype))
         self.assertTrue(np.allclose(expected_array, result_array), "Value mismatch")
Example #3
0
    def test_json_ndarray_in_dict(self):
        # Arrange
        m = 10
        expected = {'0':np.arange(0),
                    '10':np.arange(10)}
        
        # Act
        dumped = dumps(expected)
        result = loads(dumped)

        # Assert
        for i, expected_array in expected.iteritems():
            result_array = result[i]
            self.assertEqual(expected_array.shape, result_array.shape, "Shape mismatch")
            self.assertEqual(expected_array.dtype, result_array.dtype, "Type mismatch %s != %s" % (expected_array.dtype, result_array.dtype))
            self.assertTrue(np.allclose(expected_array, result_array), "Value mismatch")
    def test_handleFrame(self):
        # Arrange
        data = ["this", "is", "just", 1, "Test"]
        expected = jsonEncoder.dumps(data, separators=(',', ':'), sort_keys=True)
        com = self.comunicator

        # pipe write to read buffer
        def write(bytes):
            if isinstance(bytes, str):
                bytes = six.b(bytes)
            com.connection.readBuffer += bytes
            com._handleReadyRead()
            return len(bytes)

        com.connection.write = write
        com.handshakeDone = True

        # Assert
        def handleNewPacket(result):
            self.assertEqual(result, expected, "Unexpected Data: %s != %s" % (result, expected))
        com._handleNewPacket = handleNewPacket

        # Act
        com._sendPacket(data, binary=False)
Example #5
0
 def _sendPacket(self, data, binary=False):
     if len(data) <= 0:
         return
     opCode = websocket.OPCODE_BINARY if binary else websocket.OPCODE_ASCII
     dataSer = jsonEncoder.dumps(data, separators=(',', ':'), sort_keys=True)
     self._sendFrame_(dataSer, opCode)