def test_list_subscriptions_no_paging(self): from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH} RETURNED = {'subscriptions': [SUB_INFO]} connection = _Connection(RETURNED) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) client._connection = connection api = self._make_one(client) iterator = api.list_subscriptions(self.PROJECT) subscriptions = list(iterator) next_token = iterator.next_page_token # Check the token returned. self.assertIsNone(next_token) # Check the subscription object returned. self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertIsInstance(subscription.topic, Topic) self.assertEqual(subscription.topic.name, self.TOPIC_NAME) self.assertIs(subscription._client, client) self.assertEqual(subscription.project, self.PROJECT) self.assertIsNone(subscription.ack_deadline) self.assertIsNone(subscription.push_endpoint) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_SUBSCRIPTIONS_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], {})
def test_list_snapshots_no_paging(self): from google.cloud.pubsub.client import Client from google.cloud.pubsub.snapshot import Snapshot local_snapshot_path = 'projects/%s/snapshots/%s' % ( self.PROJECT, self.SNAPSHOT_NAME) local_topic_path = 'projects/%s/topics/%s' % ( self.PROJECT, self.TOPIC_NAME) RETURNED = {'snapshots': [{ 'name': local_snapshot_path, 'topic': local_topic_path, }], } connection = _Connection(RETURNED) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) client._connection = connection api = self._make_one(client) iterator = api.list_snapshots(self.PROJECT) snapshots = list(iterator) next_token = iterator.next_page_token self.assertIsNone(next_token) self.assertEqual(len(snapshots), 1) snapshot = snapshots[0] self.assertIsInstance(snapshot, Snapshot) self.assertEqual(snapshot.topic.name, self.TOPIC_NAME) self.assertIs(snapshot._client, client) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_SNAPSHOTS_PATH,) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], {})
def test_list_subscriptions_with_paging(self): from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription client = Client(project=self.PROJECT, credentials=_make_credentials(), use_gax=False) SUB_NAME_1 = 'subscription_1' SUB_PATH_1 = 'projects/%s/subscriptions/%s' % (self.PROJECT, SUB_NAME_1) SUB_NAME_2 = 'subscription_2' SUB_PATH_2 = 'projects/%s/subscriptions/%s' % (self.PROJECT, SUB_NAME_2) SUBS_LIST = [SUB_PATH_1, SUB_PATH_2] PAGE_SIZE = 10 TOKEN = 'TOKEN' returned = { 'subscriptions': SUBS_LIST, } client._connection = _Connection(returned) topic = self._make_one(self.TOPIC_NAME, client=client) iterator = topic.list_subscriptions(page_size=PAGE_SIZE, page_token=TOKEN) subscriptions = list(iterator) next_page_token = iterator.next_page_token self.assertEqual(len(subscriptions), 2) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscriptions[0].name, SUB_NAME_1) self.assertIs(subscription.topic, topic) subscription = subscriptions[1] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscriptions[1].name, SUB_NAME_2) self.assertIs(subscription.topic, topic) self.assertIsNone(next_page_token) # Verify the mock. called_with = client._connection._called_with self.assertEqual(len(called_with), 3) self.assertEqual(called_with['method'], 'GET') path = '/%s/subscriptions' % (self.TOPIC_PATH, ) self.assertEqual(called_with['path'], path) self.assertEqual(called_with['query_params'], { 'pageSize': PAGE_SIZE, 'pageToken': TOKEN })
def main(): client = Client() for example in _find_examples(): to_delete = [] print('%-25s: %s' % _name_and_doc(example)) try: example(client, to_delete) except AssertionError as e: print(' FAIL: %s' % (e,)) except Exception as e: # pylint: disable=broad-except print(' ERROR: %r' % (e,)) for item in to_delete: item.delete()
def test_list_subscriptions_with_paging(self): from google.cloud.grpc.pubsub.v1.pubsub_pb2 import PushConfig from google.cloud.grpc.pubsub.v1.pubsub_pb2 import (Subscription as SubscriptionPB) from google.cloud._testing import _GAXPageIterator from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic SIZE = 23 TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' push_cfg_pb = PushConfig(push_endpoint=self.PUSH_ENDPOINT) local_sub_path = '%s/subscriptions/%s' % (self.PROJECT_PATH, self.SUB_NAME) sub_pb = SubscriptionPB(name=local_sub_path, topic=self.TOPIC_PATH, push_config=push_cfg_pb) response = _GAXPageIterator([sub_pb], page_token=NEW_TOKEN) gax_api = _GAXSubscriberAPI(_list_subscriptions_response=response) client = _Client(self.PROJECT) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) api = self._make_one(gax_api, client) iterator = api.list_subscriptions(self.PROJECT, page_size=SIZE, page_token=TOKEN) subscriptions = list(iterator) next_token = iterator.next_page_token # Check the token returned. self.assertEqual(next_token, NEW_TOKEN) # Check the subscription object returned. self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertIsInstance(subscription.topic, Topic) self.assertEqual(subscription.topic.name, self.TOPIC_NAME) self.assertIs(subscription._client, client) self.assertEqual(subscription._project, self.PROJECT) self.assertIsNone(subscription.ack_deadline) self.assertEqual(subscription.push_endpoint, self.PUSH_ENDPOINT) name, page_size, options = gax_api._list_subscriptions_called_with self.assertEqual(name, self.PROJECT_PATH) self.assertEqual(page_size, 23) self.assertEqual(options.page_token, TOKEN)
def test_list_snapshots_with_paging(self): import six from google.cloud.pubsub.client import Client from google.cloud.pubsub.snapshot import Snapshot TOKEN1 = 'TOKEN1' TOKEN2 = 'TOKEN2' SIZE = 1 local_snapshot_path = 'projects/%s/snapshots/%s' % (self.PROJECT, self.SNAPSHOT_NAME) local_topic_path = 'projects/%s/topics/%s' % (self.PROJECT, self.TOPIC_NAME) RETURNED = { 'snapshots': [{ 'name': local_snapshot_path, 'topic': local_topic_path, }], 'nextPageToken': TOKEN2, } connection = _Connection(RETURNED) creds = _make_credentials() client = Client(project=self.PROJECT, credentials=creds) client._connection = connection api = self._make_one(client) iterator = api.list_snapshots(self.PROJECT, page_token=TOKEN1, page_size=SIZE) page = six.next(iterator.pages) snapshots = list(page) next_token = iterator.next_page_token self.assertEqual(next_token, TOKEN2) self.assertEqual(len(snapshots), 1) snapshot = snapshots[0] self.assertIsInstance(snapshot, Snapshot) self.assertEqual(snapshot.topic.name, self.TOPIC_NAME) self.assertIs(snapshot._client, client) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_SNAPSHOTS_PATH, ) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], { 'pageToken': TOKEN1, 'pageSize': SIZE })
def test_list_subscriptions_with_paging(self): import six from google.cloud.pubsub.client import Client from google.cloud.pubsub.subscription import Subscription from google.cloud.pubsub.topic import Topic TOKEN1 = 'TOKEN1' TOKEN2 = 'TOKEN2' SIZE = 1 SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH} RETURNED = { 'subscriptions': [SUB_INFO], 'nextPageToken': 'TOKEN2', } connection = _Connection(RETURNED) creds = _Credentials() client = Client(project=self.PROJECT, credentials=creds) client.connection = connection api = self._makeOne(client) iterator = api.list_subscriptions(self.PROJECT, page_token=TOKEN1, page_size=SIZE) page = six.next(iterator.pages) subscriptions = list(page) next_token = iterator.next_page_token # Check the token returned. self.assertEqual(next_token, TOKEN2) # Check the subscription object returned. self.assertEqual(len(subscriptions), 1) subscription = subscriptions[0] self.assertIsInstance(subscription, Subscription) self.assertEqual(subscription.name, self.SUB_NAME) self.assertIsInstance(subscription.topic, Topic) self.assertEqual(subscription.topic.name, self.TOPIC_NAME) self.assertIs(subscription._client, client) self.assertEqual(subscription._project, self.PROJECT) self.assertIsNone(subscription.ack_deadline) self.assertIsNone(subscription.push_endpoint) self.assertEqual(connection._called_with['method'], 'GET') path = '/%s' % (self.LIST_SUBSCRIPTIONS_PATH, ) self.assertEqual(connection._called_with['path'], path) self.assertEqual(connection._called_with['query_params'], { 'pageToken': TOKEN1, 'pageSize': SIZE })
def test_list_subscriptions_missing_key(self): from google.cloud.pubsub.client import Client client = Client(project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False) client._connection = _Connection({}) topic = self._make_one(self.TOPIC_NAME, client=client) iterator = topic.list_subscriptions() subscriptions = list(iterator) next_page_token = iterator.next_page_token self.assertEqual(len(subscriptions), 0) self.assertIsNone(next_page_token) # Verify the mock. called_with = client._connection._called_with self.assertEqual(len(called_with), 3) self.assertEqual(called_with['method'], 'GET') path = '/%s/subscriptions' % (self.TOPIC_PATH,) self.assertEqual(called_with['path'], path) self.assertEqual(called_with['query_params'], {})