8000 STORAGE: Allowing Batch() constructor to fall back to implicit. · googleapis/google-cloud-python@2b53706 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b53706

Browse files
committed
STORAGE: Allowing Batch() constructor to fall back to implicit.
Also removing last explicit use of the implicit connection in storage regression test.
1 parent f21cacf commit 2b53706

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

gcloud/storage/batch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import six
2727

2828
from gcloud._localstack import _LocalStack
29+
from gcloud.storage import _implicit_environ
2930
from gcloud.storage.connection import Connection
3031

3132

@@ -78,7 +79,10 @@ class Batch(Connection):
7879
"""
7980
_MAX_BATCH_SIZE = 1000
8081

81-
def __init__(self, connection):
82+
def __init__(self, connection=None):
83+
if connection is None:
84+
connection = _implicit_environ.get_default_connection()
85+
8286
super(Batch, self).__init__(project=connection.project)
8387
self._connection = connection
8488
self._requests = []

gcloud/storage/test_batch.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def test_ctor_body_dict(self):
6868

6969
class TestBatch(unittest2.TestCase):
7070

71+
def setUp(self):
72+
from gcloud.storage._testing import _setup_defaults
73+
_setup_defaults(self)
74+
75+
def tearDown(self):
76+
from gcloud.storage._testing import _tear_down_defaults
77+
_tear_down_defaults(self)
78+
7179
def _getTargetClass(self):
7280
from gcloud.storage.batch import Batch
7381
return Batch
@@ -84,6 +92,19 @@ def test_ctor_w_explicit_connection(self):
8492
self.assertEqual(len(batch._requests), 0)
8593
self.assertEqual(len(batch._responses), 0)
8694

95+
def test_ctor_w_implicit_connection(self):
96+
from gcloud.storage._testing import _monkey_defaults
97+
98+
http = _HTTP()
99+
connection = _Connection(http=http)
100+
with _monkey_defaults(connection=connection):
101+
batch = self._makeOne()
102+
103+
self.assertTrue(batch._connection is connection)
104+
self.assertEqual(batch.project, connection.project)
105+
self.assertEqual(len(batch._requests), 0)
106+
self.assertEqual(len(batch._responses), 0)
107+
87108
def test__make_request_GET_forwarded_to_connection(self):
88109
URL = 'http://example.com/api'
89110
expected = _Response()

regression/storage.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
storage._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
3030
storage.set_defaults()
3131

32-
CONNECTION = storage.get_default_connection()
33-
3432

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

5452
def tearDown(self):
55-
with Batch(CONNECTION) as batch:
53+
with Batch() as batch:
5654
for bucket_name in self.case_buckets_to_delete:
5755
storage.Bucket(connection=batch, name=bucket_name).delete()
5856

0 commit comments

Comments
 (0)
0