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'], {})
Exemple #3
0
    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'], {})
Exemple #4
0
    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
        })
Exemple #5
0
    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 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
        })
Exemple #7
0
    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 = object()
        client = Client(project=self.PROJECT, credentials=creds)
        client._connection = connection
        api = self._make_one(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})
Exemple #8
0
    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'], {})
Exemple #9
0
    def test_list_subscriptions_missing_key(self):
        from google.cloud.pubsub.client import Client

        client = Client(project=self.PROJECT,
                        credentials=_make_credentials(), use_gax=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'], {})