Skip to content

Commit

Permalink
Testing interaction with page token and extra params in iterator.
Browse files Browse the repository at this point in the history
Also making sure tests pass by using the `upload_` prefix
instead of the `set_contents_` prefix.
  • Loading branch information
dhermes committed Nov 4, 2014
1 parent c54cd2d commit 062bba5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def upload_file(self, filename, key=None):
if key is None:
key = os.path.basename(filename)
key = self.new_key(key)
return key.set_contents_from_filename(filename)
return key.upload_from_filename(filename)

def upload_file_object(self, file_obj, key=None):
"""Shortcut method to upload a file object into this bucket.
Expand Down Expand Up @@ -324,7 +324,7 @@ def upload_file_object(self, file_obj, key=None):
key = self.new_key(key)
else:
key = self.new_key(os.path.basename(file_obj.name))
return key.set_contents_from_file(file_obj)
return key.upload_from_file(file_obj)

def has_metadata(self, field=None):
"""Check if metadata is available locally.
Expand Down
12 changes: 11 additions & 1 deletion gcloud/storage/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,22 @@ class Iterator(object):
:type path: string
:param path: The path to query for the list of items.
"""

PAGE_TOKEN = 'pageToken'
RESERVED_PARAMS = frozenset([PAGE_TOKEN])

def __init__(self, connection, path, extra_params=None):
self.connection = connection
self.path = path
self.page_number = 0
self.next_page_token = None
self.extra_params = extra_params
if self.extra_params is not None:
reserved_in_use = self.RESERVED_PARAMS.intersection(
extra_params.keys())
if reserved_in_use:
raise ValueError(('Using a reserved parameter',
reserved_in_use))

def __iter__(self):
"""Iterate through the list of items."""
Expand Down Expand Up @@ -72,7 +82,7 @@ def get_query_params(self):
"""
result = None
if self.next_page_token:
result = {'pageToken': self.next_page_token}
result = {self.PAGE_TOKEN: self.next_page_token}
if self.extra_params is not None:
result = result or {}
result.update(self.extra_params)
Expand Down
6 changes: 4 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,9 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def set_contents_from_file(self, fh):
def upload_from_file(self, fh):
_uploaded.append((self._bucket, self._name, fh))

bucket = self._makeOne()
with _Monkey(MUT, Key=_Key):
bucket.upload_file_object(FILEOBJECT)
Expand All @@ -397,8 +398,9 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def set_contents_from_file(self, fh):
def upload_from_file(self, fh):
_uploaded.append((self._bucket, self._name, fh))

bucket = self._makeOne()
with _Monkey(MUT, Key=_Key):
bucket.upload_file_object(FILEOBJECT, KEY)
Expand Down
19 changes: 19 additions & 0 deletions gcloud/storage/test_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ def test_get_query_params_extra_params(self):
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
self.assertEqual(iterator.get_query_params(), extra_params)

def test_get_query_params_w_token_and_extra_params(self):
connection = _Connection()
PATH = '/foo'
TOKEN = 'token'
extra_params = {'key': 'val'}
iterator = self._makeOne(connection, PATH, extra_params=extra_params)
iterator.next_page_token = TOKEN

expected_query = extra_params.copy()
expected_query.update({'pageToken': TOKEN})
self.assertEqual(iterator.get_query_params(), expected_query)

def test_get_query_params_w_token_collision(self):
connection = _Connection()
PATH = '/foo'
extra_params = {'pageToken': 'val'}
self.assertRaises(ValueError, self._makeOne, connection, PATH,
extra_params=extra_params)

def test_get_next_page_response_new_no_token_in_response(self):
PATH = '/foo'
TOKEN = 'token'
Expand Down

0 comments on commit 062bba5

Please sign in to comment.