forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
412 lines (347 loc) · 14.2 KB
/
test_api.py
File metadata and controls
412 lines (347 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
# Copyright 2015 Google Inc. All rights reserved.
#
# 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 unittest2
class Test_lookup_bucket(unittest2.TestCase):
def _callFUT(self, bucket_name, connection=None):
from gcloud.storage.api import lookup_bucket
return lookup_bucket(bucket_name, connection=connection)
def test_miss(self):
from gcloud.storage.connection import Connection
NONESUCH = 'nonesuch'
conn = Connection()
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b',
'nonesuch?projection=noAcl',
])
http = conn._http = Http(
{'status': '404', 'content-type': 'application/json'},
b'{}',
)
bucket = self._callFUT(NONESUCH, connection=conn)
self.assertEqual(bucket, None)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def _lookup_bucket_hit_helper(self, use_default=False):
from gcloud.storage._testing import _monkey_defaults
from gcloud.storage.bucket import Bucket
from gcloud.storage.connection import Connection
BLOB_NAME = 'blob-name'
conn = Connection()
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b',
'%s?projection=noAcl' % (BLOB_NAME,),
])
http = conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
'{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'),
)
if use_default:
with _monkey_defaults(connection=conn):
bucket = self._callFUT(BLOB_NAME)
else:
bucket = self._callFUT(BLOB_NAME, connection=conn)
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is conn)
self.assertEqual(bucket.name, BLOB_NAME)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def test_hit(self):
self._lookup_bucket_hit_helper(use_default=False)
def test_use_default(self):
self._lookup_bucket_hit_helper(use_default=True)
class Test_list_buckets(unittest2.TestCase):
def _callFUT(self, *args, **kwargs):
from gcloud.storage.api import list_buckets
return list_buckets(*args, **kwargs)
def test_empty(self):
from six.moves.urllib.parse import parse_qs
from six.moves.urllib.parse import urlparse
from gcloud.storage.connection import Connection
PROJECT = 'project'
conn = Connection()
EXPECTED_QUERY = {
'project': [PROJECT],
'projection': ['noAcl'],
}
http = conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
b'{}',
)
buckets = list(self._callFUT(project=PROJECT, connection=conn))
self.assertEqual(len(buckets), 0)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['body'], None)
BASE_URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b',
])
URI = http._called_with['uri']
self.assertTrue(URI.startswith(BASE_URI))
uri_parts = urlparse(URI)
self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
def _list_buckets_non_empty_helper(self, project, use_default=False):
from six.moves.urllib.parse import urlencode
from gcloud._testing import _monkey_defaults as _base_monkey_defaults
from gcloud.storage._testing import _monkey_defaults
from gcloud.storage.connection import Connection
BUCKET_NAME = 'bucket-name'
conn = Connection()
query_params = urlencode({'project': project, 'projection': 'noAcl'})
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b?%s' % (query_params,),
])
http = conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
'{{"items": [{{"name": "{0}"}}]}}'.format(BUCKET_NAME)
.encode('utf-8'),
)
if use_default:
with _base_monkey_defaults(project=project):
with _monkey_defaults(connection=conn):
buckets = list(self._callFUT())
else:
buckets = list(self._callFUT(project=project, connection=conn))
self.assertEqual(len(buckets), 1)
self.assertEqual(buckets[0].name, BUCKET_NAME)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def test_non_empty(self):
self._list_buckets_non_empty_helper('PROJECT', use_default=False)
def test_non_use_default(self):
self._list_buckets_non_empty_helper('PROJECT', use_default=True)
def test_all_arguments(self):
from six.moves.urllib.parse import parse_qs
from six.moves.urllib.parse import urlparse
from gcloud.storage.connection import Connection
PROJECT = 'foo-bar'
MAX_RESULTS = 10
PAGE_TOKEN = 'ABCD'
PREFIX = 'subfolder'
PROJECTION = 'full'
FIELDS = 'items/id,nextPageToken'
EXPECTED_QUERY = {
'project': [PROJECT],
'maxResults': [str(MAX_RESULTS)],
'pageToken': [PAGE_TOKEN],
'prefix': [PREFIX],
'projection': [PROJECTION],
'fields': [FIELDS],
}
CONNECTION = Connection()
http = CONNECTION._http = Http(
{'status': '200', 'content-type': 'application/json'},
'{"items": []}',
)
iterator = self._callFUT(
project=PROJECT,
max_results=MAX_RESULTS,
page_token=PAGE_TOKEN,
prefix=PREFIX,
projection=PROJECTION,
fields=FIELDS,
connection=CONNECTION,
)
buckets = list(iterator)
self.assertEqual(buckets, [])
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['body'], None)
BASE_URI = '/'.join([
CONNECTION.API_BASE_URL,
'storage',
CONNECTION.API_VERSION,
'b'
])
URI = http._called_with['uri']
self.assertTrue(URI.startswith(BASE_URI))
uri_parts = urlparse(URI)
self.assertEqual(parse_qs(uri_parts.query), EXPECTED_QUERY)
class Test_get_bucket(unittest2.TestCase):
def _callFUT(self, bucket_name, connection=None):
from gcloud.storage.api import get_bucket
return get_bucket(bucket_name, connection=connection)
def test_miss(self):
from gcloud.exceptions import NotFound
from gcloud.storage.connection import Connection
NONESUCH = 'nonesuch'
conn = Connection()
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b',
'nonesuch?projection=noAcl',
])
http = conn._http = Http(
{'status': '404', 'content-type': 'application/json'},
b'{}',
)
self.assertRaises(NotFound, self._callFUT, NONESUCH, connection=conn)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def _get_bucket_hit_helper(self, use_default=False):
from gcloud.storage._testing import _monkey_defaults
from gcloud.storage.bucket import Bucket
from gcloud.storage.connection import Connection
BLOB_NAME = 'blob-name'
conn = Connection()
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b',
'%s?projection=noAcl' % (BLOB_NAME,),
])
http = conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
'{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'),
)
if use_default:
with _monkey_defaults(connection=conn):
bucket = self._callFUT(BLOB_NAME)
else:
bucket = self._callFUT(BLOB_NAME, connection=conn)
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is conn)
self.assertEqual(bucket.name, BLOB_NAME)
self.assertEqual(http._called_with['method'], 'GET')
self.assertEqual(http._called_with['uri'], URI)
def test_hit(self):
self._get_bucket_hit_helper(use_default=False)
def test_hit_use_default(self):
self._get_bucket_hit_helper(use_default=True)
class Test_create_bucket(unittest2.TestCase):
def _callFUT(self, bucket_name, project=None, connection=None):
from gcloud.storage.api import create_bucket
return create_bucket(bucket_name, project=project,
connection=connection)
def _create_bucket_success_helper(self, project, use_default=False):
from gcloud._testing import _monkey_defaults as _base_monkey_defaults
from gcloud.storage._testing import _monkey_defaults
from gcloud.storage.connection import Connection
from gcloud.storage.bucket import Bucket
BLOB_NAME = 'blob-name'
conn = Connection()
URI = '/'.join([
conn.API_BASE_URL,
'storage',
conn.API_VERSION,
'b?project=%s' % project,
])
http = conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
'{{"name": "{0}"}}'.format(BLOB_NAME).encode('utf-8'),
)
if use_default:
with _base_monkey_defaults(project=project):
with _monkey_defaults(connection=conn):
bucket = self._callFUT(BLOB_NAME)
else:
bucket = self._callFUT(BLOB_NAME, project=project, connection=conn)
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is conn)
self.assertEqual(bucket.name, BLOB_NAME)
self.assertEqual(http._called_with['method'], 'POST')
self.assertEqual(http._called_with['uri'], URI)
def test_success(self):
self._create_bucket_success_helper('PROJECT', use_default=False)
def test_success_use_default(self):
self._create_bucket_success_helper('PROJECT', use_default=True)
class Test__BucketIterator(unittest2.TestCase):
def _getTargetClass(self):
from gcloud.storage.api import _BucketIterator
return _BucketIterator
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor(self):
connection = object()
iterator = self._makeOne(connection)
self.assertTrue(iterator.connection is connection)
self.assertEqual(iterator.path, '/b')
self.assertEqual(iterator.page_number, 0)
self.assertEqual(iterator.next_page_token, None)
def test_get_items_from_response_empty(self):
connection = object()
iterator = self._makeOne(connection)
self.assertEqual(list(iterator.get_items_from_response({})), [])
def test_get_items_from_response_non_empty(self):
from gcloud.storage.bucket import Bucket
BLOB_NAME = 'blob-name'
response = {'items': [{'name': BLOB_NAME}]}
connection = object()
iterator = self._makeOne(connection)
buckets = list(iterator.get_items_from_response(response))
self.assertEqual(len(buckets), 1)
bucket = buckets[0]
self.assertTrue(isinstance(bucket, Bucket))
self.assertTrue(bucket.connection is connection)
self.assertEqual(bucket.name, BLOB_NAME)
class Test__require_connection(unittest2.TestCase):
def _callFUT(self, connection=None):
from gcloud.storage.api import _require_connection
return _require_connection(connection=connection)
def _monkey(self, connection):
from gcloud.storage._testing import _monkey_defaults
return _monkey_defaults(connection=connection)
def test_implicit_unset(self):
with self._monkey(None):
with self.assertRaises(EnvironmentError):
self._callFUT()
def test_implicit_unset_w_existing_batch(self):
CONNECTION = object()
with self._monkey(None):
with _NoCommitBatch(connection=CONNECTION):
self.assertEqual(self._callFUT(), CONNECTION)
def test_implicit_unset_passed_explicitly(self):
CONNECTION = object()
with self._monkey(None):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
def test_implicit_set(self):
IMPLICIT_CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT() is IMPLICIT_CONNECTION)
def test_implicit_set_passed_explicitly(self):
IMPLICIT_CONNECTION = object()
CONNECTION = object()
with self._monkey(IMPLICIT_CONNECTION):
self.assertTrue(self._callFUT(CONNECTION) is CONNECTION)
class Http(object):
_called_with = None
def __init__(self, headers, content):
from httplib2 import Response
self._response = Response(headers)
self._content = content
def request(self, **kw):
self._called_with = kw
return self._response, self._content
class _NoCommitBatch(object):
def __init__(self, connection):
self._connection = connection
def __enter__(self):
from gcloud.storage.batch import _BATCHES
_BATCHES.push(self._connection)
return self._connection
def __exit__(self, *args):
from gcloud.storage.batch import _BATCHES
_BATCHES.pop()