From adc470b952719237f4b2606a15e58a3fcaa5eb30 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 25 Mar 2015 14:58:03 -0700 Subject: [PATCH] Make storage.get_all_buckets() respect implicit/explicit cnxn. --- gcloud/storage/api.py | 12 ++++++++---- gcloud/storage/test_api.py | 13 ++++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index 44647a1b2917..7a16105163dc 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -76,13 +76,12 @@ def get_all_buckets(project=None, connection=None): :type connection: :class:`gcloud.storage.connection.Connection` or ``NoneType`` :param connection: Optional. The connection to use when sending requests. - If not provided, falls back to default. + If not provided, _BucketIterator() will fall back to + default. :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects. :returns: All buckets belonging to this project. """ - if connection is None: - connection = get_default_connection() if project is None: project = get_default_project() extra_params = {'project': project} @@ -171,6 +170,11 @@ class _BucketIterator(Iterator): """ def __init__(self, connection, extra_params=None): + # If an implicit connection was intended, we pass along `None` to the + # Bucket() constructor as well. + self._ctor_connection = connection + if connection is None: + connection = get_default_connection() super(_BucketIterator, self).__init__(connection=connection, path='/b', extra_params=extra_params) @@ -182,6 +186,6 @@ def get_items_from_response(self, response): """ for item in response.get('items', []): name = item.get('name') - bucket = Bucket(name, connection=self.connection) + bucket = Bucket(name, connection=self._ctor_connection) bucket._properties = item yield bucket diff --git a/gcloud/storage/test_api.py b/gcloud/storage/test_api.py index f3b642015be0..35b03653e2be 100644 --- a/gcloud/storage/test_api.py +++ b/gcloud/storage/test_api.py @@ -282,19 +282,26 @@ def test_get_items_from_response_empty(self): iterator = self._makeOne(connection) self.assertEqual(list(iterator.get_items_from_response({})), []) - def test_get_items_from_response_non_empty(self): + def _get_items_helper(self, connection): 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.assertTrue(bucket._connection is connection) self.assertEqual(bucket.name, BLOB_NAME) + def test_get_items_from_response_non_empty(self): + connection = object() + self._get_items_helper(connection) + + def test_get_items_from_response_implicit_connection(self): + connection = None + self._get_items_helper(connection) + class Http(object):