Skip to content

Commit 062bba5

Browse files
committed
Testing interaction with page token and extra params in iterator.
Also making sure tests pass by using the `upload_` prefix instead of the `set_contents_` prefix.
1 parent c54cd2d commit 062bba5

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

gcloud/storage/bucket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def upload_file(self, filename, key=None):
284284
if key is None:
285285
key = os.path.basename(filename)
286286
key = self.new_key(key)
287-
return key.set_contents_from_filename(filename)
287+
return key.upload_from_filename(filename)
288288

289289
def upload_file_object(self, file_obj, key=None):
290290
"""Shortcut method to upload a file object into this bucket.
@@ -324,7 +324,7 @@ def upload_file_object(self, file_obj, key=None):
324324
key = self.new_key(key)
325325
else:
326326
key = self.new_key(os.path.basename(file_obj.name))
327-
return key.set_contents_from_file(file_obj)
327+
return key.upload_from_file(file_obj)
328328

329329
def has_metadata(self, field=None):
330330
"""Check if metadata is available locally.

gcloud/storage/iterator.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ class Iterator(object):
3838
:type path: string
3939
:param path: The path to query for the list of items.
4040
"""
41+
42+
PAGE_TOKEN = 'pageToken'
43+
RESERVED_PARAMS = frozenset([PAGE_TOKEN])
44+
4145
def __init__(self, connection, path, extra_params=None):
4246
self.connection = connection
4347
self.path = path
4448
self.page_number = 0
4549
self.next_page_token = None
4650
self.extra_params = extra_params
51+
if self.extra_params is not None:
52+
reserved_in_use = self.RESERVED_PARAMS.intersection(
53+
extra_params.keys())
54+
if reserved_in_use:
55+
raise ValueError(('Using a reserved parameter',
56+
reserved_in_use))
4757

4858
def __iter__(self):
4959
"""Iterate through the list of items."""
@@ -72,7 +82,7 @@ def get_query_params(self):
7282
"""
7383
result = None
7484
if self.next_page_token:
75-
result = {'pageToken': self.next_page_token}
85+
result = {self.PAGE_TOKEN: self.next_page_token}
7686
if self.extra_params is not None:
7787
result = result or {}
7888
result.update(self.extra_params)

gcloud/storage/test_bucket.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ def __init__(self, bucket, name):
376376
self._bucket = bucket
377377
self._name = name
378378

379-
def set_contents_from_file(self, fh):
379+
def upload_from_file(self, fh):
380380
_uploaded.append((self._bucket, self._name, fh))
381+
381382
bucket = self._makeOne()
382383
with _Monkey(MUT, Key=_Key):
383384
bucket.upload_file_object(FILEOBJECT)
@@ -397,8 +398,9 @@ def __init__(self, bucket, name):
397398
self._bucket = bucket
398399
self._name = name
399400

400-
def set_contents_from_file(self, fh):
401+
def upload_from_file(self, fh):
401402
_uploaded.append((self._bucket, self._name, fh))
403+
402404
bucket = self._makeOne()
403405
with _Monkey(MUT, Key=_Key):
404406
bucket.upload_file_object(FILEOBJECT, KEY)

gcloud/storage/test_iterator.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ def test_get_query_params_extra_params(self):
8282
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
8383
self.assertEqual(iterator.get_query_params(), extra_params)
8484

85+
def test_get_query_params_w_token_and_extra_params(self):
86+
connection = _Connection()
87+
PATH = '/foo'
88+
TOKEN = 'token'
89+
extra_params = {'key': 'val'}
90+
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
91+
iterator.next_page_token = TOKEN
92+
93+
expected_query = extra_params.copy()
94+
expected_query.update({'pageToken': TOKEN})
95+
self.assertEqual(iterator.get_query_params(), expected_query)
96+
97+
def test_get_query_params_w_token_collision(self):
98+
connection = _Connection()
99+
PATH = '/foo'
100+
extra_params = {'pageToken': 'val'}
101+
self.assertRaises(ValueError, self._makeOne, connection, PATH,
102+
extra_params=extra_params)
103+
85104
def test_get_next_page_response_new_no_token_in_response(self):
86105
PATH = '/foo'
87106
TOKEN = 'token'

0 commit comments

Comments
 (0)