def test_create_topic(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = publisher_client.PublisherClient() # Mock request name = client.topic_path('[PROJECT]', '[TOPIC]') # Mock response name_2 = 'name2-1052831874' expected_response = pubsub_pb2.Topic(name=name_2) grpc_stub.CreateTopic.return_value = expected_response response = client.create_topic(name) self.assertEqual(expected_response, response) grpc_stub.CreateTopic.assert_called_once() args, kwargs = grpc_stub.CreateTopic.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = pubsub_pb2.Topic(name=name) self.assertEqual(expected_request, actual_request)
def test_list_topics(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = publisher_client.PublisherClient() # Mock request project = client.project_path('[PROJECT]') # Mock response next_page_token = '' topics_element = pubsub_pb2.Topic() topics = [topics_element] expected_response = pubsub_pb2.ListTopicsResponse( next_page_token=next_page_token, topics=topics) grpc_stub.ListTopics.return_value = expected_response paged_list_response = client.list_topics(project) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.topics[0], resources[0]) grpc_stub.ListTopics.assert_called_once() args, kwargs = grpc_stub.ListTopics.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = pubsub_pb2.ListTopicsRequest(project=project) self.assertEqual(expected_request, actual_request)
def create_topic(self, name, options=None): """ Creates the given topic with the given name. Example: >>> from google.cloud.gapic.pubsub.v1 import publisher_client >>> api = publisher_client.PublisherClient() >>> name = api.topic_path('[PROJECT]', '[TOPIC]') >>> response = api.create_topic(name) Args: name (string): The name of the topic. It must have the format ``\"projects/{project}/topics/{topic}\"``. ``{topic}`` must start with a letter, and contain only letters (``[A-Za-z]``), numbers (``[0-9]``), dashes (``-``), underscores (``_``), periods (``.``), tildes (``~``), plus (``+``) or percent signs (``%``). It must be between 3 and 255 characters in length, and it must not start with ``\"goog\"``. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.cloud.proto.pubsub.v1.pubsub_pb2.Topic` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ # Create the request object. request = pubsub_pb2.Topic(name=name) return self._create_topic(request, options)
def test_create_topic(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 name = client.topic_path('[PROJECT]', '[TOPIC]') # Mock response name_2 = 'name2-1052831874' expected_response = pubsub_pb2.Topic(name_2) grpc_stub.CreateTopic.return_value = expected_response response = client.create_topic(name) self.assertEqual(expected_response, response) grpc_stub.CreateTopic.assert_called_once() request = grpc_stub.CreateTopic.call_args[0] self.assertEqual(name, request.name)
def test_get_topic(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]') # Mock response name = 'name3373707' expected_response = pubsub_pb2.Topic(name) grpc_stub.GetTopic.return_value = expected_response response = client.get_topic(topic) self.assertEqual(expected_response, response) grpc_stub.GetTopic.assert_called_once() request = grpc_stub.GetTopic.call_args[0] self.assertEqual(topic, request.topic)