From 8007c17233379b301c3d5bed5a1a384e89b8d0f3 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 12 Mar 2015 16:06:08 -0700 Subject: [PATCH] STORAGE: Allowing Batch() constructor to fall back to implicit. Also removing last explicit use of the implicit connection in storage regression test. --- gcloud/storage/batch.py | 6 +++++- gcloud/storage/test_batch.py | 21 +++++++++++++++++++++ regression/storage.py | 4 +--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/gcloud/storage/batch.py b/gcloud/storage/batch.py index e43c16892a6e..2713151308b1 100644 --- a/gcloud/storage/batch.py +++ b/gcloud/storage/batch.py @@ -26,6 +26,7 @@ import six from gcloud._localstack import _LocalStack +from gcloud.storage import _implicit_environ from gcloud.storage.connection import Connection @@ -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 = [] diff --git a/gcloud/storage/test_batch.py b/gcloud/storage/test_batch.py index c7dbbba53a5e..700a0aec0f52 100644 --- a/gcloud/storage/test_batch.py +++ b/gcloud/storage/test_batch.py @@ -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 @@ -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() diff --git a/regression/storage.py b/regression/storage.py index 9a3c26fbebb2..02a35058806c 100644 --- a/regression/storage.py +++ b/regression/storage.py @@ -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: @@ -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()