Skip to content

Commit

Permalink
Merge pull request #720 from dhermes/use-fields-in-bucket-exists
Browse files Browse the repository at this point in the history
Using 'fields' in Bucket.exists().
  • Loading branch information
dhermes committed Mar 13, 2015
2 parents d9e2156 + 6186059 commit ab642a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
6 changes: 5 additions & 1 deletion gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def exists(self):
:returns: True if the bucket exists in Cloud Storage.
"""
try:
self.connection.get_bucket(self.name)
# We only need the status code (200 or not) so we seek to
# minimize the returned payload.
query_params = {'fields': 'name'}
self.connection.api_request(method='GET', path=self.path,
query_params=query_params)
return True
except NotFound:
return False
Expand Down
38 changes: 27 additions & 11 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,46 @@ class _FakeConnection(object):
_called_with = []

@classmethod
def get_bucket(cls, bucket_name):
cls._called_with.append(bucket_name)
raise NotFound(bucket_name)
def api_request(cls, *args, **kwargs):
cls._called_with.append((args, kwargs))
raise NotFound(args)

NAME = 'name'
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME)
self.assertFalse(bucket.exists())
self.assertEqual(_FakeConnection._called_with, [NAME])
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
'query_params': {
'fields': 'name',
},
}
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_exists_hit(self):
class _FakeConnection(object):

_called_with = []

@classmethod
def get_bucket(cls, bucket_name):
cls._called_with.append(bucket_name)
def api_request(cls, *args, **kwargs):
cls._called_with.append((args, kwargs))
# exists() does not use the return value
return object()

NAME = 'name'
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME)
self.assertTrue(bucket.exists())
self.assertEqual(_FakeConnection._called_with, [NAME])
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
'query_params': {
'fields': 'name',
},
}
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_acl_property(self):
from gcloud.storage.acl import BucketACL
Expand Down

0 comments on commit ab642a3

Please sign in to comment.