forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
421 lines (328 loc) · 14.2 KB
/
test_client.py
File metadata and controls
421 lines (328 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
def _make_credentials():
import google.auth.credentials
return mock.Mock(spec=google.auth.credentials.Credentials)
class TestClient(unittest.TestCase):
PROJECT = 'PROJECT'
TOPIC_NAME = 'topic_name'
TOPIC_PATH = 'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME)
SUB_NAME = 'subscription_name'
SUB_PATH = 'projects/%s/subscriptions/%s' % (PROJECT, SUB_NAME)
@staticmethod
def _get_target_class():
from google.cloud.pubsub.client import Client
return Client
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_publisher_api_wo_gax(self):
from google.cloud.pubsub._http import _PublisherAPI
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds,
use_gax=False)
conn = client._connection = _Connection()
api = client.publisher_api
self.assertIsInstance(api, _PublisherAPI)
self.assertEqual(api.api_request, conn.api_request)
# API instance is cached
again = client.publisher_api
self.assertIs(again, api)
def test_no_gax_ctor(self):
from google.cloud.pubsub._http import _PublisherAPI
creds = _make_credentials()
with mock.patch('google.cloud.pubsub.client._USE_GAX',
new=True):
client = self._make_one(project=self.PROJECT, credentials=creds,
use_gax=False)
self.assertFalse(client._use_gax)
api = client.publisher_api
self.assertIsInstance(api, _PublisherAPI)
def _publisher_api_w_gax_helper(self, emulator=False):
from google.cloud.pubsub import _http
wrapped = object()
_called_with = []
def _generated_api(*args, **kw):
_called_with.append((args, kw))
return wrapped
class _GaxPublisherAPI(object):
def __init__(self, _wrapped, client):
self._wrapped = _wrapped
self._client = client
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds,
use_gax=True)
client._connection.in_emulator = emulator
patch = mock.patch.multiple(
'google.cloud.pubsub.client',
make_gax_publisher_api=_generated_api,
GAXPublisherAPI=_GaxPublisherAPI)
with patch:
api = client.publisher_api
self.assertIsInstance(api, _GaxPublisherAPI)
self.assertIs(api._wrapped, wrapped)
self.assertIs(api._client, client)
# API instance is cached
again = client.publisher_api
self.assertIs(again, api)
if emulator:
kwargs = {'host': _http.Connection.API_BASE_URL}
else:
kwargs = {'credentials': creds}
self.assertEqual(_called_with, [((), kwargs)])
def test_publisher_api_w_gax(self):
self._publisher_api_w_gax_helper()
def test_publisher_api_w_gax_and_emulator(self):
self._publisher_api_w_gax_helper(emulator=True)
def test_subscriber_api_wo_gax(self):
from google.cloud.pubsub._http import _SubscriberAPI
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds,
use_gax=False)
conn = client._connection = _Connection()
api = client.subscriber_api
self.assertIsInstance(api, _SubscriberAPI)
self.assertEqual(api.api_request, conn.api_request)
# API instance is cached
again = client.subscriber_api
self.assertIs(again, api)
def _subscriber_api_w_gax_helper(self, emulator=False):
from google.cloud.pubsub import _http
wrapped = object()
_called_with = []
def _generated_api(*args, **kw):
_called_with.append((args, kw))
return wrapped
class _GaxSubscriberAPI(object):
def __init__(self, _wrapped, client):
self._wrapped = _wrapped
self._client = client
creds = _make_credentials()
client = self._make_one(
project=self.PROJECT, credentials=creds,
use_gax=True)
client._connection.in_emulator = emulator
patch = mock.patch.multiple(
'google.cloud.pubsub.client',
make_gax_subscriber_api=_generated_api,
GAXSubscriberAPI=_GaxSubscriberAPI)
with patch:
api = client.subscriber_api
self.assertIsInstance(api, _GaxSubscriberAPI)
self.assertIs(api._wrapped, wrapped)
self.assertIs(api._client, client)
# API instance is cached
again = client.subscriber_api
self.assertIs(again, api)
if emulator:
kwargs = {'host': _http.Connection.API_BASE_URL}
else:
kwargs = {'credentials': creds}
self.assertEqual(_called_with, [((), kwargs)])
def test_subscriber_api_w_gax(self):
self._subscriber_api_w_gax_helper()
def test_subscriber_api_w_gax_and_emulator(self):
self._subscriber_api_w_gax_helper(emulator=True)
def test_iam_policy_api(self):
from google.cloud.pubsub._http import _IAMPolicyAPI
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
conn = client._connection = _Connection()
api = client.iam_policy_api
self.assertIsInstance(api, _IAMPolicyAPI)
self.assertEqual(api.api_request, conn.api_request)
# API instance is cached
again = client.iam_policy_api
self.assertIs(again, api)
def test_list_topics_no_paging(self):
from google.cloud.pubsub.topic import Topic
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
client._connection = object()
api = _FauxPublisherAPI(items=[Topic(self.TOPIC_NAME, client)])
client._publisher_api = api
iterator = client.list_topics()
topics = list(iterator)
next_page_token = iterator.next_page_token
self.assertEqual(len(topics), 1)
self.assertIsInstance(topics[0], Topic)
self.assertEqual(topics[0].name, self.TOPIC_NAME)
self.assertIsNone(next_page_token)
self.assertEqual(api._listed_topics, (self.PROJECT, None, None))
def test_list_topics_with_paging(self):
from google.cloud.pubsub.topic import Topic
TOKEN1 = 'TOKEN1'
TOKEN2 = 'TOKEN2'
SIZE = 1
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
client._connection = object()
api = _FauxPublisherAPI([Topic(self.TOPIC_NAME, client)], TOKEN2)
client._publisher_api = api
iterator = client.list_topics(SIZE, TOKEN1)
topics = list(iterator)
next_page_token = iterator.next_page_token
self.assertEqual(len(topics), 1)
self.assertIsInstance(topics[0], Topic)
self.assertEqual(topics[0].name, self.TOPIC_NAME)
self.assertEqual(next_page_token, TOKEN2)
self.assertEqual(api._listed_topics, (self.PROJECT, 1, TOKEN1))
def test_list_topics_missing_key(self):
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds)
client._connection = object()
api = _FauxPublisherAPI()
client._publisher_api = api
iterator = client.list_topics()
topics = list(iterator)
next_page_token = iterator.next_page_token
self.assertEqual(len(topics), 0)
self.assertIsNone(next_page_token)
self.assertEqual(api._listed_topics, (self.PROJECT, None, None))
def test_list_subscriptions_no_paging(self):
from google.cloud.pubsub.subscription import Subscription
from google.cloud.pubsub.topic import Topic
SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds,
use_gax=False)
returned = {'subscriptions': [SUB_INFO]}
client._connection = _Connection(returned)
iterator = client.list_subscriptions()
subscriptions = list(iterator)
next_page_token = iterator.next_page_token
# Check the token returned.
self.assertIsNone(next_page_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)
called_with = client._connection._called_with
expected_path = '/projects/%s/subscriptions' % (self.PROJECT,)
self.assertEqual(called_with, {
'method': 'GET',
'path': expected_path,
'query_params': {},
})
def test_list_subscriptions_with_paging(self):
import six
from google.cloud.pubsub.subscription import Subscription
from google.cloud.pubsub.topic import Topic
SUB_INFO = {'name': self.SUB_PATH, 'topic': self.TOPIC_PATH}
creds = _make_credentials()
client = self._make_one(project=self.PROJECT, credentials=creds,
use_gax=False)
# Set up the mock response.
ACK_DEADLINE = 42
PUSH_ENDPOINT = 'https://push.example.com/endpoint'
SUB_INFO = {'name': self.SUB_PATH,
'topic': self.TOPIC_PATH,
'ackDeadlineSeconds': ACK_DEADLINE,
'pushConfig': {'pushEndpoint': PUSH_ENDPOINT}}
TOKEN1 = 'TOKEN1'
TOKEN2 = 'TOKEN2'
SIZE = 1
returned = {
'subscriptions': [SUB_INFO],
'nextPageToken': TOKEN2,
}
client._connection = _Connection(returned)
iterator = client.list_subscriptions(
SIZE, TOKEN1)
page = six.next(iterator.pages)
subscriptions = list(page)
next_page_token = iterator.next_page_token
# Check the token returned.
self.assertEqual(next_page_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.assertEqual(subscription.ack_deadline, ACK_DEADLINE)
self.assertEqual(subscription.push_endpoint, PUSH_ENDPOINT)
called_with = client._connection._called_with
expected_path = '/projects/%s/subscriptions' % (self.PROJECT,)
self.assertEqual(called_with, {
'method': 'GET',
'path': expected_path,
'query_params': {
'pageSize': SIZE,
'pageToken': TOKEN1,
},
})
def test_list_subscriptions_w_missing_key(self):
PROJECT = 'PROJECT'
creds = _make_credentials()
client = self._make_one(project=PROJECT, credentials=creds)
client._connection = object()
api = client._subscriber_api = _FauxSubscriberAPI()
api._list_subscriptions_response = (), None
subscriptions, next_page_token = client.list_subscriptions()
self.assertEqual(len(subscriptions), 0)
self.assertIsNone(next_page_token)
self.assertEqual(api._listed_subscriptions,
(self.PROJECT, None, None))
def test_topic(self):
PROJECT = 'PROJECT'
TOPIC_NAME = 'TOPIC_NAME'
creds = _make_credentials()
client_obj = self._make_one(project=PROJECT, credentials=creds)
new_topic = client_obj.topic(TOPIC_NAME)
self.assertEqual(new_topic.name, TOPIC_NAME)
self.assertIs(new_topic._client, client_obj)
self.assertEqual(new_topic.project, PROJECT)
self.assertEqual(new_topic.full_name,
'projects/%s/topics/%s' % (PROJECT, TOPIC_NAME))
self.assertFalse(new_topic.timestamp_messages)
class _Iterator(object):
def __init__(self, items, token):
self._items = items or ()
self.next_page_token = token
def __iter__(self):
return iter(self._items)
class _FauxPublisherAPI(object):
def __init__(self, items=None, token=None):
self._items = items
self._token = token
def list_topics(self, project, page_size, page_token):
self._listed_topics = (project, page_size, page_token)
return _Iterator(self._items, self._token)
class _FauxSubscriberAPI(object):
def list_subscriptions(self, project, page_size, page_token):
self._listed_subscriptions = (project, page_size, page_token)
return self._list_subscriptions_response
class _Connection(object):
_called_with = None
def __init__(self, *responses):
self._responses = responses
def api_request(self, **kw):
self._called_with = kw
response, self._responses = self._responses[0], self._responses[1:]
return response