Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STORAGE: Allowing Batch() constructor to fall back to implicit. #722

Merged
merged 1 commit into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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