def test_pull_wo_return_immediately_max_messages_w_implicit_conn(self):
     import base64
     from gcloud.pubsub.message import Message
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID = 'DEADBEEF'
     MSG_ID = 'BEADCAFE'
     PAYLOAD = b'This is the message text'
     B64 = base64.b64encode(PAYLOAD)
     MESSAGE = {'messageId': MSG_ID, 'data': B64}
     REC_MESSAGE = {'ackId': ACK_ID, 'message': MESSAGE}
     conn = _Connection({'receivedMessages': [REC_MESSAGE]})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         pulled = subscription.pull()
     self.assertEqual(len(pulled), 1)
     ack_id, message = pulled[0]
     self.assertEqual(ack_id, ACK_ID)
     self.assertTrue(isinstance(message, Message))
     self.assertEqual(message.data, PAYLOAD)
     self.assertEqual(message.message_id, MSG_ID)
     self.assertEqual(message.attributes, {})
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:pull' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'returnImmediately': False, 'maxMessages': 1})
Beispiel #2
0
 def test_pull_wo_return_immediately_max_messages_w_implicit_conn(self):
     import base64
     from gcloud.pubsub.message import Message
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID = 'DEADBEEF'
     MSG_ID = 'BEADCAFE'
     PAYLOAD = b'This is the message text'
     B64 = base64.b64encode(PAYLOAD)
     MESSAGE = {'messageId': MSG_ID, 'data': B64}
     REC_MESSAGE = {'ackId': ACK_ID, 'message': MESSAGE}
     conn = _Connection({'receivedMessages': [REC_MESSAGE]})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         pulled = subscription.pull()
     self.assertEqual(len(pulled), 1)
     ack_id, message = pulled[0]
     self.assertEqual(ack_id, ACK_ID)
     self.assertTrue(isinstance(message, Message))
     self.assertEqual(message.data, PAYLOAD)
     self.assertEqual(message.message_id, MSG_ID)
     self.assertEqual(message.attributes, {})
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:pull' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'returnImmediately': False, 'maxMessages': 1})
Beispiel #3
0
 def test_w_implicit_connection_wo_paging(self):
     from gcloud._testing import _monkey_defaults as _monkey_base_defaults
     from gcloud.pubsub._testing import _monkey_defaults
     from gcloud.pubsub.subscription import Subscription
     PROJECT = 'PROJECT'
     SUB_NAME = 'subscription_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     SUB_INFO = [{'name': SUB_PATH, 'topic': TOPIC_PATH}]
     returned = {'subscriptions': SUB_INFO}
     conn = _Connection(returned)
     with _monkey_base_defaults(project=PROJECT):
         with _monkey_defaults(connection=conn):
             subscriptions, next_page_token = self._callFUT()
     self.assertEqual(len(subscriptions), 1)
     self.assertTrue(isinstance(subscriptions[0], Subscription))
     self.assertEqual(subscriptions[0].name, SUB_NAME)
     self.assertEqual(subscriptions[0].topic.name, TOPIC_NAME)
     self.assertEqual(next_page_token, None)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/projects/%s/subscriptions' % PROJECT)
     self.assertEqual(req['query_params'], {})
 def test_from_api_repr_no_topics_no_connection(self):
     from gcloud.pubsub.topic import Topic
     from gcloud.pubsub._testing import _monkey_defaults
     TOPIC_NAME = 'topic_name'
     PROJECT = 'PROJECT'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     DEADLINE = 42
     ENDPOINT = 'https://api.example.com/push'
     resource = {'topic': TOPIC_PATH,
                 'name': SUB_PATH,
                 'ackDeadlineSeconds': DEADLINE,
                 'pushConfig': {'pushEndpoint': ENDPOINT}}
     conn = _Connection()
     klass = self._getTargetClass()
     with _monkey_defaults(connection=conn):
         subscription = klass.from_api_repr(resource, connection=conn)
     self.assertEqual(subscription.name, SUB_NAME)
     topic = subscription.topic
     self.assertTrue(isinstance(topic, Topic))
     self.assertEqual(topic.name, TOPIC_NAME)
     self.assertEqual(topic.project, PROJECT)
     self.assertTrue(topic.connection is conn)
     self.assertEqual(subscription.ack_deadline, DEADLINE)
     self.assertEqual(subscription.push_endpoint, ENDPOINT)
Beispiel #5
0
 def test_ctor_wo_inferred_project_or_connection(self):
     from gcloud._testing import _monkey_defaults as _monkey_base_defaults
     from gcloud.pubsub._testing import _monkey_defaults
     TOPIC_NAME = 'topic_name'
     PROJECT = 'PROJECT'
     conn = _Connection()
     with _monkey_base_defaults(project=PROJECT):
         with _monkey_defaults(connection=conn):
             topic = self._makeOne(TOPIC_NAME)
     self.assertEqual(topic.name, TOPIC_NAME)
     self.assertEqual(topic.project, PROJECT)
     self.assertTrue(topic.connection is conn)
Beispiel #6
0
 def test_from_api_repr_wo_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     TOPIC_NAME = 'topic_name'
     PROJECT = 'PROJECT'
     PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     resource = {'name': PATH}
     klass = self._getTargetClass()
     conn = _Connection()
     with _monkey_defaults(connection=conn):
         topic = klass.from_api_repr(resource)
     self.assertEqual(topic.name, TOPIC_NAME)
     self.assertEqual(topic.project, PROJECT)
     self.assertEqual(topic.full_name, PATH)
     self.assertTrue(topic.connection is conn)
Beispiel #7
0
 def test_delete_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.delete()
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'DELETE')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
 def test_delete_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.delete()
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'DELETE')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
Beispiel #9
0
 def test_exists_miss_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     conn = _Connection()
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         self.assertFalse(subscription.exists())
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
     self.assertEqual(req.get('query_params'), None)
 def test_exists_miss_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     conn = _Connection()
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         self.assertFalse(subscription.exists())
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
     self.assertEqual(req.get('query_params'), None)
Beispiel #11
0
 def test_acknowledge_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID1 = 'DEADBEEF'
     ACK_ID2 = 'BEADCAFE'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.acknowledge([ACK_ID1, ACK_ID2])
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:acknowledge' % SUB_PATH)
     self.assertEqual(req['data'], {'ackIds': [ACK_ID1, ACK_ID2]})
Beispiel #12
0
 def test_create_pull_wo_ack_deadline_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     BODY = {'topic': TOPIC_PATH}
     conn = _Connection({'name': SUB_PATH})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.create()
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'PUT')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
     self.assertEqual(req['data'], BODY)
 def test_create_pull_wo_ack_deadline_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     BODY = {'topic': TOPIC_PATH}
     conn = _Connection({'name': SUB_PATH})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.create()
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'PUT')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
     self.assertEqual(req['data'], BODY)
 def test_acknowledge_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID1 = 'DEADBEEF'
     ACK_ID2 = 'BEADCAFE'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.acknowledge([ACK_ID1, ACK_ID2])
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:acknowledge' % SUB_PATH)
     self.assertEqual(req['data'], {'ackIds': [ACK_ID1, ACK_ID2]})
Beispiel #15
0
 def test_modify_push_config_w_endpoint_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ENDPOINT = 'https://api.example.com/push'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.modify_push_configuration(push_endpoint=ENDPOINT)
     self.assertEqual(subscription.push_endpoint, ENDPOINT)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:modifyPushConfig' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'pushConfig': {'pushEndpoint': ENDPOINT}})
Beispiel #16
0
 def test_modify_ack_deadline_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID = 'DEADBEEF'
     DEADLINE = 42
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.modify_ack_deadline(ACK_ID, DEADLINE)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:modifyAckDeadline' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'ackId': ACK_ID, 'ackDeadlineSeconds': DEADLINE})
 def test_modify_ack_deadline_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ACK_ID = 'DEADBEEF'
     DEADLINE = 42
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.modify_ack_deadline(ACK_ID, DEADLINE)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:modifyAckDeadline' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'ackId': ACK_ID, 'ackDeadlineSeconds': DEADLINE})
 def test_modify_push_config_w_endpoint_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     ENDPOINT = 'https://api.example.com/push'
     conn = _Connection({})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.modify_push_configuration(push_endpoint=ENDPOINT)
     self.assertEqual(subscription.push_endpoint, ENDPOINT)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'POST')
     self.assertEqual(req['path'], '/%s:modifyPushConfig' % SUB_PATH)
     self.assertEqual(req['data'],
                      {'pushConfig': {'pushEndpoint': ENDPOINT}})
Beispiel #19
0
 def test_reload_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     DEADLINE = 42
     ENDPOINT = 'https://api.example.com/push'
     conn = _Connection({'name': SUB_PATH,
                         'topic': TOPIC_PATH,
                         'ackDeadline': DEADLINE,
                         'pushConfig': {'pushEndpoint': ENDPOINT}})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.reload()
     self.assertEqual(subscription.ack_deadline, DEADLINE)
     self.assertEqual(subscription.push_endpoint, ENDPOINT)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
Beispiel #20
0
 def test_w_implicit_connection_and_project_wo_paging(self):
     from gcloud._testing import _monkey_defaults as _monkey_base_defaults
     from gcloud.pubsub._testing import _monkey_defaults
     from gcloud.pubsub.topic import Topic
     TOPIC_NAME = 'topic_name'
     PROJECT = 'PROJECT'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     TOKEN = 'TOKEN'
     returned = {'topics': [{'name': TOPIC_PATH}],
                 'nextPageToken': TOKEN}
     conn = _Connection(returned)
     with _monkey_base_defaults(project=PROJECT):
         with _monkey_defaults(connection=conn):
             topics, next_page_token = self._callFUT()
     self.assertEqual(len(topics), 1)
     self.assertTrue(isinstance(topics[0], Topic))
     self.assertEqual(topics[0].name, TOPIC_NAME)
     self.assertEqual(next_page_token, TOKEN)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/projects/%s/topics' % PROJECT)
     self.assertEqual(req['query_params'], {})
 def test_reload_w_implicit_connection(self):
     from gcloud.pubsub._testing import _monkey_defaults
     PROJECT = 'PROJECT'
     SUB_NAME = 'sub_name'
     SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
     TOPIC_NAME = 'topic_name'
     TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
     DEADLINE = 42
     ENDPOINT = 'https://api.example.com/push'
     conn = _Connection({'name': SUB_PATH,
                         'topic': TOPIC_PATH,
                         'ackDeadline': DEADLINE,
                         'pushConfig': {'pushEndpoint': ENDPOINT}})
     topic = _Topic(TOPIC_NAME, project=PROJECT)
     subscription = self._makeOne(SUB_NAME, topic)
     with _monkey_defaults(connection=conn):
         subscription.reload()
     self.assertEqual(subscription.ack_deadline, DEADLINE)
     self.assertEqual(subscription.push_endpoint, ENDPOINT)
     self.assertEqual(len(conn._requested), 1)
     req = conn._requested[0]
     self.assertEqual(req['method'], 'GET')
     self.assertEqual(req['path'], '/%s' % SUB_PATH)
Beispiel #22
0
 def _monkey(self, connection):
     from gcloud.pubsub._testing import _monkey_defaults
     return _monkey_defaults(connection=connection)