Skip to content

Commit

Permalink
STORAGE: Allowing Batch() constructor to fall back to implicit.
Browse files Browse the repository at this point in the history
Also removing last explicit use of the implicit connection in
storage regression test.
  • Loading branch information
dhermes committed Mar 12, 2015
1 parent f21cacf commit 2b53706
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 5 additions & 1 deletion gcloud/storage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import six

from gcloud._localstack import _LocalStack
from gcloud.storage import _implicit_environ
from gcloud.storage.connection import Connection


Expand Down Expand Up @@ -78,7 +79,10 @@ class Batch(Connection):
"""
_MAX_BATCH_SIZE = 1000

def __init__(self, connection):
def __init__(self, connection=None):
if connection is None:
connection = _implicit_environ.get_default_connection()

super(Batch, self).__init__(project=connection.project)
self._connection = connection
self._requests = []
Expand Down
21 changes: 21 additions & 0 deletions gcloud/storage/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def test_ctor_body_dict(self):

class TestBatch(unittest2.TestCase):

def setUp(self):
from gcloud.storage._testing import _setup_defaults
_setup_defaults(self)

def tearDown(self):
from gcloud.storage._testing import _tear_down_defaults
_tear_down_defaults(self)

def _getTargetClass(self):
from gcloud.storage.batch import Batch
return Batch
Expand All @@ -84,6 +92,19 @@ def test_ctor_w_explicit_connection(self):
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._responses), 0)

def test_ctor_w_implicit_connection(self):
from gcloud.storage._testing import _monkey_defaults

http = _HTTP()
connection = _Connection(http=http)
with _monkey_defaults(connection=connection):
batch = self._makeOne()

self.assertTrue(batch._connection is connection)
self.assertEqual(batch.project, connection.project)
self.assertEqual(len(batch._requests), 0)
self.assertEqual(len(batch._responses), 0)

def test__make_request_GET_forwarded_to_connection(self):
URL = 'http://example.com/api'
expected = _Response()
Expand Down
4 changes: 1 addition & 3 deletions regression/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
storage._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
storage.set_defaults()

CONNECTION = storage.get_default_connection()


def setUpModule():
if 'test_bucket' not in SHARED_BUCKETS:
Expand All @@ -52,7 +50,7 @@ def setUp(self):
self.case_buckets_to_delete = []

def tearDown(self):
with Batch(CONNECTION) as batch:
with Batch() as batch:
for bucket_name in self.case_buckets_to_delete:
storage.Bucket(connection=batch, name=bucket_name).delete()

Expand Down

0 comments on commit 2b53706

Please sign in to comment.