From 798845d98088b3b963986baca026f287de5fa147 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 10 Jul 2015 17:22:07 -0700 Subject: [PATCH] Updating storage.Batch to accept a client. Towards #952, removing connection from methods / constructors. --- gcloud/storage/batch.py | 14 +++++-------- gcloud/storage/test_batch.py | 38 ++++++++++++++++++------------------ system_tests/storage.py | 2 +- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/gcloud/storage/batch.py b/gcloud/storage/batch.py index 3d6806fb603e..8404bef1c587 100644 --- a/gcloud/storage/batch.py +++ b/gcloud/storage/batch.py @@ -28,7 +28,6 @@ from gcloud._helpers import _LocalStack from gcloud.exceptions import make_exception -from gcloud.storage import _implicit_environ from gcloud.storage.connection import Connection @@ -130,17 +129,14 @@ def __setitem__(self, key, value): class Batch(Connection): """Proxy an underlying connection, batching up change operations. - :type connection: :class:`gcloud.storage.connection.Connection` - :param connection: the connection for which the batch proxies. + :type client: :class:`gcloud.storage.client.Client` + :param client: The client to use for making connections. """ _MAX_BATCH_SIZE = 1000 - def __init__(self, connection=None): - if connection is None: - connection = _implicit_environ.get_default_connection() - + def __init__(self, client): super(Batch, self).__init__() - self._connection = connection + self._client = client self._requests = [] self._target_objects = [] @@ -248,7 +244,7 @@ def finish(self): url = '%s/batch' % self.API_BASE_URL - response, content = self._connection._make_request( + response, content = self._client.connection._make_request( 'POST', url, data=body, headers=headers) responses = list(_unpack_batch_response(response, content)) self._finish_futures(responses) diff --git a/gcloud/storage/test_batch.py b/gcloud/storage/test_batch.py index 94695671090e..d9b332dbf776 100644 --- a/gcloud/storage/test_batch.py +++ b/gcloud/storage/test_batch.py @@ -86,20 +86,9 @@ def _makeOne(self, *args, **kw): def test_ctor_w_explicit_connection(self): http = _HTTP() connection = _Connection(http=http) - batch = self._makeOne(connection) - self.assertTrue(batch._connection is connection) - self.assertEqual(len(batch._requests), 0) - self.assertEqual(len(batch._target_objects), 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) + client = _Client(connection) + batch = self._makeOne(client) + self.assertTrue(batch._client is client) self.assertEqual(len(batch._requests), 0) self.assertEqual(len(batch._target_objects), 0) @@ -262,7 +251,8 @@ def test_finish_nonempty(self): expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="' http = _HTTP((expected, _THREE_PART_MIME_RESPONSE)) connection = _Connection(http=http) - batch = self._makeOne(connection) + client = _Client(connection) + batch = self._makeOne(client) batch.API_BASE_URL = 'http://api.example.com' batch._do_request('POST', URL, {}, {'foo': 1, 'bar': 2}, None) batch._do_request('PATCH', URL, {}, {'bar': 3}, None) @@ -311,7 +301,8 @@ def test_finish_responses_mismatch(self): expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="' http = _HTTP((expected, _TWO_PART_MIME_RESPONSE_WITH_FAIL)) connection = _Connection(http=http) - batch = self._makeOne(connection) + client = _Client(connection) + batch = self._makeOne(client) batch.API_BASE_URL = 'http://api.example.com' batch._requests.append(('GET', URL, {}, None)) self.assertRaises(ValueError, batch.finish) @@ -323,7 +314,8 @@ def test_finish_nonempty_with_status_failure(self): expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="' http = _HTTP((expected, _TWO_PART_MIME_RESPONSE_WITH_FAIL)) connection = _Connection(http=http) - batch = self._makeOne(connection) + client = _Client(connection) + batch = self._makeOne(client) batch.API_BASE_URL = 'http://api.example.com' target1 = _MockObject() target2 = _MockObject() @@ -363,7 +355,8 @@ def test_finish_nonempty_non_multipart_response(self): expected['content-type'] = 'text/plain' http = _HTTP((expected, 'NOT A MIME_RESPONSE')) connection = _Connection(http=http) - batch = self._makeOne(connection) + client = _Client(connection) + batch = self._makeOne(client) batch._requests.append(('POST', URL, {}, {'foo': 1, 'bar': 2})) batch._requests.append(('PATCH', URL, {}, {'bar': 3})) batch._requests.append(('DELETE', URL, {}, None)) @@ -376,13 +369,14 @@ def test_as_context_mgr_wo_error(self): expected['content-type'] = 'multipart/mixed; boundary="DEADBEEF="' http = _HTTP((expected, _THREE_PART_MIME_RESPONSE)) connection = _Connection(http=http) + client = _Client(connection) self.assertEqual(list(_BATCHES), []) target1 = _MockObject() target2 = _MockObject() target3 = _MockObject() - with self._makeOne(connection) as batch: + with self._makeOne(client) as batch: self.assertEqual(list(_BATCHES), [batch]) batch._make_request('POST', URL, {'foo': 1, 'bar': 2}, target_object=target1) @@ -596,3 +590,9 @@ def request(self, method, uri, headers, body): class _MockObject(object): pass + + +class _Client(object): + + def __init__(self, connection): + self.connection = connection diff --git a/system_tests/storage.py b/system_tests/storage.py index b0551b3a87fe..b1d51791b439 100644 --- a/system_tests/storage.py +++ b/system_tests/storage.py @@ -51,7 +51,7 @@ def setUp(self): self.case_buckets_to_delete = [] def tearDown(self): - with storage.Batch(): + with storage.Batch(CLIENT): for bucket_name in self.case_buckets_to_delete: storage.Bucket(bucket_name).delete()