Exemple #1
0
    def test_publish(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data=data)
        messages = [messages_element]

        # Mock response
        message_ids_element = 'messageIdsElement-744837059'
        message_ids = [message_ids_element]
        expected_response = pubsub_pb2.PublishResponse(message_ids=message_ids)
        grpc_stub.Publish.return_value = expected_response

        response = client.publish(topic, messages)
        self.assertEqual(expected_response, response)

        grpc_stub.Publish.assert_called_once()
        args, kwargs = grpc_stub.Publish.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = pubsub_pb2.PublishRequest(topic=topic,
                                                     messages=messages)
        self.assertEqual(expected_request, actual_request)
Exemple #2
0
    def test_publish(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=pubsub_pb2.PublisherStub)
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data)
        messages = [messages_element]

        # Mock response
        message_ids_element = 'messageIdsElement-744837059'
        message_ids = [message_ids_element]
        expected_response = pubsub_pb2.PublishResponse(message_ids)
        grpc_stub.Publish.return_value = expected_response

        response = client.publish(topic, messages)
        self.assertEqual(expected_response, response)

        grpc_stub.Publish.assert_called_once()
        request = grpc_stub.Publish.call_args[0]

        self.assertEqual(topic, request.topic)
        self.assertEqual(messages, request.messages)
Exemple #3
0
    def _from_proto(proto_msg):
        """Construct from serialized form of ``PubsubMessage``.

    https://cloud.google.com/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PubsubMessage
    """
        msg = pubsub_pb2.PubsubMessage()
        msg.ParseFromString(proto_msg)
        # Convert ScalarMapContainer to dict.
        attributes = dict((key, msg.attributes[key]) for key in msg.attributes)
        return PubsubMessage(msg.data, attributes)
Exemple #4
0
    def _to_proto_str(self):
        """Get serialized form of ``PubsubMessage``.

    Args:
      proto_msg: str containing a serialized protobuf.

    Returns:
      A str containing a serialized protobuf of type
      https://cloud.google.com/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PubsubMessage
      containing the payload of this object.
    """
        msg = pubsub_pb2.PubsubMessage()
        msg.data = self.data
        for key, value in self.attributes.iteritems():
            msg.attributes[key] = value
        return msg.SerializeToString()
Exemple #5
0
    def test_publish_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = publisher_client.PublisherClient()

        # Mock request
        topic = client.topic_path('[PROJECT]', '[TOPIC]')
        data = b'-86'
        messages_element = pubsub_pb2.PubsubMessage(data=data)
        messages = [messages_element]

        # Mock exception response
        grpc_stub.Publish.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.publish, topic, messages)