diff --git a/gcloud/storage/__init__.py b/gcloud/storage/__init__.py index 3679fcc45709..214571d0008e 100644 --- a/gcloud/storage/__init__.py +++ b/gcloud/storage/__init__.py @@ -78,10 +78,9 @@ def set_default_bucket(bucket=None): """ if bucket is None: bucket_name = os.getenv(_BUCKET_ENV_VAR_NAME) - connection = get_default_connection() - if bucket_name is not None and connection is not None: - bucket = Bucket(bucket_name, connection=connection) + if bucket_name is not None: + bucket = Bucket(bucket_name) if bucket is not None: _implicit_environ._DEFAULTS.bucket = bucket diff --git a/gcloud/storage/api.py b/gcloud/storage/api.py index b16ce1842e12..1fdd6ad2e484 100644 --- a/gcloud/storage/api.py +++ b/gcloud/storage/api.py @@ -50,7 +50,6 @@ def lookup_bucket(bucket_name, connection=None): :rtype: :class:`gcloud.storage.bucket.Bucket` :returns: The bucket matching the name provided or None if not found. """ - connection = _require_connection(connection) try: return get_bucket(bucket_name, connection=connection) except NotFound: @@ -104,7 +103,6 @@ def list_buckets(project=None, max_results=None, page_token=None, prefix=None, :rtype: iterable of :class:`gcloud.storage.bucket.Bucket` objects. :returns: All buckets belonging to this project. """ - connection = _require_connection(connection) if project is None: project = get_default_project() extra_params = {'project': project} @@ -159,7 +157,7 @@ def get_bucket(bucket_name, connection=None): :raises: :class:`gcloud.exceptions.NotFound` """ connection = _require_connection(connection) - bucket = Bucket(bucket_name, connection=connection) + bucket = Bucket(bucket_name) bucket.reload(connection=connection) return bucket @@ -195,7 +193,7 @@ def create_bucket(bucket_name, project=None, connection=None): :returns: The newly created bucket. """ connection = _require_connection(connection) - bucket = Bucket(bucket_name, connection=connection) + bucket = Bucket(bucket_name) bucket.create(project, connection=connection) return bucket @@ -212,6 +210,7 @@ class _BucketIterator(Iterator): """ def __init__(self, connection, extra_params=None): + connection = _require_connection(connection) super(_BucketIterator, self).__init__(connection=connection, path='/b', extra_params=extra_params) @@ -223,6 +222,6 @@ def get_items_from_response(self, response): """ for item in response.get('items', []): name = item.get('name') - bucket = Bucket(name, connection=self.connection) + bucket = Bucket(name) bucket._set_properties(item) yield bucket diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 99e37a5056d3..5190528115bb 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -250,6 +250,7 @@ def rename(self, new_name, connection=None): :rtype: :class:`Blob` :returns: The newly-copied blob. """ + connection = _require_connection(connection) new_blob = self.bucket.copy_blob(self, self.bucket, new_name, connection=connection) self.delete(connection=connection) @@ -269,6 +270,7 @@ def delete(self, connection=None): (propagated from :meth:`gcloud.storage.bucket.Bucket.delete_blob`). """ + connection = _require_connection(connection) return self.bucket.delete_blob(self.name, connection=connection) def download_to_file(self, file_obj, connection=None): diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index f75d866df456..99146ecfa745 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -96,9 +96,6 @@ class Bucket(_PropertyMixin): :type name: string :param name: The name of the bucket. - :type connection: :class:`gcloud.storage.connection.Connection` - :param connection: The connection to use when sending requests. - :type properties: dictionary or ``NoneType`` :param properties: The properties associated with the bucket. """ @@ -196,15 +193,6 @@ def default_object_acl(self): """Create our defaultObjectACL on demand.""" return self._default_object_acl - @property - def connection(self): - """Getter property for the connection to use with this Bucket. - - :rtype: :class:`gcloud.storage.connection.Connection` - :returns: The connection to use. - """ - return self._connection - @staticmethod def path_helper(bucket_name): """Relative URL path for a bucket. diff --git a/gcloud/storage/test___init__.py b/gcloud/storage/test___init__.py index 46263469a110..e47f98fd777f 100644 --- a/gcloud/storage/test___init__.py +++ b/gcloud/storage/test___init__.py @@ -58,7 +58,6 @@ def test_set_from_env_var(self): default_bucket = _implicit_environ.get_default_bucket() self.assertEqual(default_bucket.name, IMPLICIT_BUCKET_NAME) - self.assertEqual(default_bucket.connection, CONNECTION) def test_set_explicit_w_env_var_set(self): from gcloud.storage._testing import _monkey_defaults @@ -113,7 +112,6 @@ def test_set_explicit_None_w_env_var_set(self): default_bucket = _implicit_environ.get_default_bucket() self.assertEqual(default_bucket.name, IMPLICIT_BUCKET_NAME) - self.assertEqual(default_bucket.connection, CONNECTION) class Test_set_defaults(unittest2.TestCase): diff --git a/gcloud/storage/test_api.py b/gcloud/storage/test_api.py index 3874a518a4e6..c3b098e243fa 100644 --- a/gcloud/storage/test_api.py +++ b/gcloud/storage/test_api.py @@ -66,7 +66,6 @@ def _lookup_bucket_hit_helper(self, use_default=False): bucket = self._callFUT(BLOB_NAME, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is conn) self.assertEqual(bucket.name, BLOB_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI) @@ -256,7 +255,6 @@ def _get_bucket_hit_helper(self, use_default=False): bucket = self._callFUT(BLOB_NAME, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is conn) self.assertEqual(bucket.name, BLOB_NAME) self.assertEqual(http._called_with['method'], 'GET') self.assertEqual(http._called_with['uri'], URI) @@ -301,7 +299,6 @@ def _create_bucket_success_helper(self, project, use_default=False): bucket = self._callFUT(BLOB_NAME, project=project, connection=conn) self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is conn) self.assertEqual(bucket.name, BLOB_NAME) self.assertEqual(http._called_with['method'], 'POST') self.assertEqual(http._called_with['uri'], URI) @@ -325,7 +322,6 @@ def _makeOne(self, *args, **kw): def test_ctor(self): connection = object() iterator = self._makeOne(connection) - self.assertTrue(iterator.connection is connection) self.assertEqual(iterator.path, '/b') self.assertEqual(iterator.page_number, 0) self.assertEqual(iterator.next_page_token, None) @@ -345,7 +341,6 @@ def test_get_items_from_response_non_empty(self): self.assertEqual(len(buckets), 1) bucket = buckets[0] self.assertTrue(isinstance(bucket, Bucket)) - self.assertTrue(bucket.connection is connection) self.assertEqual(bucket.name, BLOB_NAME) diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 05bd5e6cf228..71b508870b4c 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -31,7 +31,7 @@ def test_ctor_implicit_bucket(self): from gcloud._testing import _Monkey from gcloud.storage import _implicit_environ - FAKE_BUCKET = _Bucket(None) + FAKE_BUCKET = _Bucket() def mock_get_bucket(): return FAKE_BUCKET @@ -46,7 +46,7 @@ def mock_get_bucket(): self.assertTrue(blob._acl.blob is blob) def test_ctor_defaults(self): - FAKE_BUCKET = _Bucket(None) + FAKE_BUCKET = _Bucket() blob = self._makeOne(None, bucket=FAKE_BUCKET) self.assertEqual(blob.bucket, FAKE_BUCKET) self.assertEqual(blob.name, None) @@ -56,8 +56,7 @@ def test_ctor_defaults(self): def test_ctor_explicit(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() properties = {'key': 'value'} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) self.assertTrue(blob.bucket is bucket) @@ -103,7 +102,7 @@ def test_chunk_size_setter_bad_value(self): def test_acl_property(self): from gcloud.storage.acl import ObjectACL - FAKE_BUCKET = _Bucket(None) + FAKE_BUCKET = _Bucket() blob = self._makeOne(None, bucket=FAKE_BUCKET) acl = blob.acl self.assertTrue(isinstance(acl, ObjectACL)) @@ -115,29 +114,25 @@ def test_path_no_bucket(self): self.assertRaises(ValueError, getattr, blob, 'path') def test_path_no_name(self): - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(None, bucket=bucket) self.assertRaises(ValueError, getattr, blob, 'path') def test_path_normal(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.path, '/b/name/o/%s' % BLOB_NAME) def test_path_w_slash_in_name(self): BLOB_NAME = 'parent/child' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.path, '/b/name/o/parent%2Fchild') def test_public_url(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.public_url, 'https://storage.googleapis.com/name/%s' % @@ -145,8 +140,7 @@ def test_public_url(self): def test_public_url_w_slash_in_name(self): BLOB_NAME = 'parent/child' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual( blob.public_url, @@ -159,7 +153,7 @@ def _basic_generate_signed_url_helper(self, credentials=None): BLOB_NAME = 'blob-name' EXPIRATION = '2014-10-16T20:34:37.000Z' connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF' '&Expiration=2014-10-16T20:34:37.000Z') @@ -210,7 +204,7 @@ def test_generate_signed_url_w_slash_in_name(self): BLOB_NAME = 'parent/child' EXPIRATION = '2014-10-16T20:34:37.000Z' connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF' '&Expiration=2014-10-16T20:34:37.000Z') @@ -237,7 +231,7 @@ def test_generate_signed_url_w_explicit_method(self): BLOB_NAME = 'blob-name' EXPIRATION = '2014-10-16T20:34:37.000Z' connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) URI = ('http://example.com/abucket/a-blob-name?Signature=DEADBEEF' '&Expiration=2014-10-16T20:34:37.000Z') @@ -263,7 +257,7 @@ def test_exists_miss(self): NONESUCH = 'nonesuch' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(NONESUCH, bucket=bucket) self.assertFalse(blob.exists(connection=connection)) @@ -272,19 +266,21 @@ def test_exists_hit(self): BLOB_NAME = 'blob-name' found_response = {'status': OK} connection = _Connection(found_response) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 self.assertTrue(blob.exists(connection=connection)) - def test_rename(self): + def test_rename_w_implicit_connection(self): + from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' NEW_NAME = 'new-name' connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 - new_blob = blob.rename(NEW_NAME) + with _monkey_defaults(connection=connection): + new_blob = blob.rename(NEW_NAME) self.assertEqual(blob.name, BLOB_NAME) self.assertEqual(new_blob.name, NEW_NAME) self.assertFalse(BLOB_NAME in bucket._blobs) @@ -295,7 +291,7 @@ def test_rename_w_explicit_connection(self): BLOB_NAME = 'blob-name' NEW_NAME = 'new-name' connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 new_blob = blob.rename(NEW_NAME, connection=connection) @@ -305,15 +301,17 @@ def test_rename_w_explicit_connection(self): self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) self.assertTrue(NEW_NAME in bucket._blobs) - def test_delete(self): + def test_delete_w_implicit_connection(self): from six.moves.http_client import NOT_FOUND + from gcloud.storage._testing import _monkey_defaults BLOB_NAME = 'blob-name' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 - blob.delete() + with _monkey_defaults(connection=connection): + blob.delete() self.assertFalse(blob.exists(connection=connection)) self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) @@ -322,7 +320,7 @@ def test_delete_w_explicit_connection(self): BLOB_NAME = 'blob-name' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 blob.delete(connection=connection) @@ -342,7 +340,7 @@ def _download_to_file_helper(self, chunk_size=None): (chunk1_response, b'abc'), (chunk2_response, b'def'), ) - bucket = _Bucket(None) + bucket = _Bucket() MEDIA_LINK = 'http://example.com/media/' properties = {'mediaLink': MEDIA_LINK} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -374,7 +372,7 @@ def test_download_to_filename(self): (chunk1_response, b'abc'), (chunk2_response, b'def'), ) - bucket = _Bucket(None) + bucket = _Bucket() MEDIA_LINK = 'http://example.com/media/' properties = {'mediaLink': MEDIA_LINK, 'updated': '2014-12-06T13:13:50.690Z'} @@ -403,7 +401,7 @@ def test_download_as_string(self): (chunk1_response, b'abc'), (chunk2_response, b'def'), ) - bucket = _Bucket(None) + bucket = _Bucket() MEDIA_LINK = 'http://example.com/media/' properties = {'mediaLink': MEDIA_LINK} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -425,7 +423,7 @@ def _upload_from_file_simple_test_helper(self, properties=None, connection = _Connection( (response, b'{}'), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) blob._CHUNK_SIZE_MULTIPLE = 1 blob.chunk_size = 5 @@ -495,7 +493,7 @@ def test_upload_from_file_resumable(self): (chunk1_response, b''), (chunk2_response, b'{}'), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob._CHUNK_SIZE_MULTIPLE = 1 blob.chunk_size = 5 @@ -553,7 +551,7 @@ def test_upload_from_file_w_slash_in_name(self): (chunk1_response, ''), (chunk2_response, ''), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob._CHUNK_SIZE_MULTIPLE = 1 blob.chunk_size = 5 @@ -600,7 +598,7 @@ def _upload_from_filename_test_helper(self, properties=None, (chunk1_response, ''), (chunk2_response, ''), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) blob._CHUNK_SIZE_MULTIPLE = 1 @@ -666,7 +664,7 @@ def test_upload_from_string_w_bytes(self): (chunk1_response, ''), (chunk2_response, ''), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob._CHUNK_SIZE_MULTIPLE = 1 blob.chunk_size = 5 @@ -705,7 +703,7 @@ def test_upload_from_string_w_text(self): (chunk1_response, ''), (chunk2_response, ''), ) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob._CHUNK_SIZE_MULTIPLE = 1 blob.chunk_size = 5 @@ -733,7 +731,7 @@ def test_make_public_w_implicit_ocnnection(self): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive} connection = _Connection(after) - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob.acl.loaded = True with _monkey_defaults(connection=connection): @@ -752,7 +750,7 @@ def test_make_public_w_explicit_connection(self): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive} connection = _Connection(after) - bucket = _Bucket(None) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) blob.acl.loaded = True blob.make_public(connection=connection) @@ -766,8 +764,7 @@ def test_make_public_w_explicit_connection(self): def test_cache_control_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CACHE_CONTROL = 'no-cache' properties = {'cacheControl': CACHE_CONTROL} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -776,13 +773,11 @@ def test_cache_control_getter(self): def test_cache_control_setter(self): BLOB_NAME = 'blob-name' CACHE_CONTROL = 'no-cache' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.cache_control, None) blob.cache_control = CACHE_CONTROL self.assertEqual(blob.cache_control, CACHE_CONTROL) - self.assertEqual(len(connection._requested), 0) def test_component_count(self): BUCKET = object() @@ -806,8 +801,7 @@ def test_component_count_string_val(self): def test_content_disposition_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CONTENT_DISPOSITION = 'Attachment; filename=example.jpg' properties = {'contentDisposition': CONTENT_DISPOSITION} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -816,18 +810,15 @@ def test_content_disposition_getter(self): def test_content_disposition_setter(self): BLOB_NAME = 'blob-name' CONTENT_DISPOSITION = 'Attachment; filename=example.jpg' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.content_disposition, None) blob.content_disposition = CONTENT_DISPOSITION self.assertEqual(blob.content_disposition, CONTENT_DISPOSITION) - self.assertEqual(len(connection._requested), 0) def test_content_encoding_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CONTENT_ENCODING = 'gzip' properties = {'contentEncoding': CONTENT_ENCODING} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -836,18 +827,15 @@ def test_content_encoding_getter(self): def test_content_encoding_setter(self): BLOB_NAME = 'blob-name' CONTENT_ENCODING = 'gzip' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.content_encoding, None) blob.content_encoding = CONTENT_ENCODING self.assertEqual(blob.content_encoding, CONTENT_ENCODING) - self.assertEqual(len(connection._requested), 0) def test_content_language_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CONTENT_LANGUAGE = 'pt-BR' properties = {'contentLanguage': CONTENT_LANGUAGE} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -856,18 +844,15 @@ def test_content_language_getter(self): def test_content_language_setter(self): BLOB_NAME = 'blob-name' CONTENT_LANGUAGE = 'pt-BR' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.content_language, None) blob.content_language = CONTENT_LANGUAGE self.assertEqual(blob.content_language, CONTENT_LANGUAGE) - self.assertEqual(len(connection._requested), 0) def test_content_type_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CONTENT_TYPE = 'image/jpeg' properties = {'contentType': CONTENT_TYPE} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -876,18 +861,15 @@ def test_content_type_getter(self): def test_content_type_setter(self): BLOB_NAME = 'blob-name' CONTENT_TYPE = 'image/jpeg' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.content_type, None) blob.content_type = CONTENT_TYPE self.assertEqual(blob.content_type, CONTENT_TYPE) - self.assertEqual(len(connection._requested), 0) def test_crc32c_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() CRC32C = 'DEADBEEF' properties = {'crc32c': CRC32C} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -896,18 +878,15 @@ def test_crc32c_getter(self): def test_crc32c_setter(self): BLOB_NAME = 'blob-name' CRC32C = 'DEADBEEF' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.crc32c, None) blob.crc32c = CRC32C self.assertEqual(blob.crc32c, CRC32C) - self.assertEqual(len(connection._requested), 0) def test_etag(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() ETAG = 'ETAG' properties = {'etag': ETAG} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -934,8 +913,7 @@ def test_generation_string_val(self): def test_id(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() ID = 'ID' properties = {'id': ID} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -943,8 +921,7 @@ def test_id(self): def test_md5_hash_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() MD5_HASH = 'DEADBEEF' properties = {'md5Hash': MD5_HASH} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -953,18 +930,15 @@ def test_md5_hash_getter(self): def test_md5_hash_setter(self): BLOB_NAME = 'blob-name' MD5_HASH = 'DEADBEEF' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.md5_hash, None) blob.md5_hash = MD5_HASH self.assertEqual(blob.md5_hash, MD5_HASH) - self.assertEqual(len(connection._requested), 0) def test_media_link(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() MEDIA_LINK = 'http://example.com/media/' properties = {'mediaLink': MEDIA_LINK} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -972,8 +946,7 @@ def test_media_link(self): def test_metadata_getter(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() METADATA = {'foo': 'Foo'} properties = {'metadata': METADATA} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -982,13 +955,11 @@ def test_metadata_getter(self): def test_metadata_setter(self): BLOB_NAME = 'blob-name' METADATA = {'foo': 'Foo'} - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() blob = self._makeOne(BLOB_NAME, bucket=bucket) self.assertEqual(blob.metadata, None) blob.metadata = METADATA self.assertEqual(blob.metadata, METADATA) - self.assertEqual(len(connection._requested), 0) def test_metageneration(self): BUCKET = object() @@ -1012,8 +983,7 @@ def test_metageneration_string_val(self): def test_owner(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() OWNER = {'entity': 'project-owner-12345', 'entityId': '23456'} properties = {'owner': OWNER} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -1023,8 +993,7 @@ def test_owner(self): def test_self_link(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() SELF_LINK = 'http://example.com/self/' properties = {'selfLink': SELF_LINK} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -1051,8 +1020,7 @@ def test_size_string_val(self): def test_storage_class(self): BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() STORAGE_CLASS = 'http://example.com/self/' properties = {'storageClass': STORAGE_CLASS} blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties) @@ -1063,8 +1031,7 @@ def test_time_deleted(self): from pytz import utc from gcloud._helpers import _RFC3339_MICROS BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc) TIME_DELETED = TIMESTAMP.strftime(_RFC3339_MICROS) properties = {'timeDeleted': TIME_DELETED} @@ -1081,8 +1048,7 @@ def test_updated(self): from pytz import utc from gcloud._helpers import _RFC3339_MICROS BLOB_NAME = 'blob-name' - connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() TIMESTAMP = datetime.datetime(2014, 11, 5, 20, 34, 37, tzinfo=utc) UPDATED = TIMESTAMP.strftime(_RFC3339_MICROS) properties = {'updated': UPDATED} @@ -1152,22 +1118,17 @@ class _Bucket(object): path = '/b/name' name = 'name' - def __init__(self, connection): - self.connection = connection + def __init__(self): self._blobs = {} self._copied = [] self._deleted = [] def copy_blob(self, blob, destination_bucket, new_name, connection=None): - if connection is None: - connection = self.connection self._copied.append((blob, destination_bucket, new_name, connection)) destination_bucket._blobs[new_name] = self._blobs[blob.name] return blob.__class__(new_name, bucket=destination_bucket) def delete_blob(self, blob_name, connection=None): - if connection is None: - connection = self.connection del self._blobs[blob_name] self._deleted.append((blob_name, connection)) diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 55030739a388..77e4e8df9e3f 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -29,7 +29,7 @@ def _makeOne(self, *args, **kw): def test_ctor_w_implicit_connection(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) self.assertTrue(iterator.bucket is bucket) @@ -41,7 +41,7 @@ def test_ctor_w_implicit_connection(self): def test_ctor_w_explicit_connection(self): connection = _Connection() - bucket = _Bucket(None) + bucket = _Bucket() iterator = self._makeOne(bucket, connection=connection) self.assertTrue(iterator.bucket is bucket) self.assertTrue(iterator.connection is connection) @@ -53,7 +53,7 @@ def test_ctor_w_explicit_connection(self): def test_get_items_from_response_empty(self): from gcloud.storage._testing import _monkey_defaults connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) blobs = list(iterator.get_items_from_response({})) @@ -66,7 +66,7 @@ def test_get_items_from_response_non_empty(self): BLOB_NAME = 'blob-name' response = {'items': [{'name': BLOB_NAME}], 'prefixes': ['foo']} connection = _Connection() - bucket = _Bucket(connection) + bucket = _Bucket() with _monkey_defaults(connection=connection): iterator = self._makeOne(bucket) blobs = list(iterator.get_items_from_response(response)) @@ -88,7 +88,6 @@ def _makeOne(self, *args, **kw): def test_ctor_defaults(self): bucket = self._makeOne() - self.assertEqual(bucket.connection, None) self.assertEqual(bucket.name, None) self.assertEqual(bucket._properties, {}) self.assertFalse(bucket._acl.loaded) @@ -98,10 +97,8 @@ def test_ctor_defaults(self): def test_ctor_explicit(self): NAME = 'name' - connection = _Connection() properties = {'key': 'value'} - bucket = self._makeOne(NAME, connection, properties=properties) - self.assertTrue(bucket.connection is connection) + bucket = self._makeOne(NAME, properties=properties) self.assertEqual(bucket.name, NAME) self.assertEqual(bucket._properties, properties) self.assertFalse(bucket._acl.loaded) @@ -272,8 +269,7 @@ def test_path_no_name(self): def test_path_w_name(self): NAME = 'name' - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertEqual(bucket.path, '/b/%s' % NAME) def test_get_blob_miss(self): @@ -354,7 +350,7 @@ def test_list_blobs_explicit(self): def test_list_blobs_w_explicit_connection(self): NAME = 'name' connection = _Connection({'items': []}) - bucket = self._makeOne(NAME, None) + bucket = self._makeOne(NAME) iterator = bucket.list_blobs(connection=connection) blobs = list(iterator) self.assertEqual(blobs, []) @@ -381,7 +377,7 @@ def test_delete_explicit_hit(self): GET_BLOBS_RESP = {'items': []} connection = _Connection(GET_BLOBS_RESP) connection._delete_bucket = True - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) result = bucket.delete(force=True, connection=connection) self.assertTrue(result is None) expected_cw = [{ @@ -405,7 +401,7 @@ def test_delete_explicit_force_delete_blobs(self): connection = _Connection(GET_BLOBS_RESP, DELETE_BLOB1_RESP, DELETE_BLOB2_RESP) connection._delete_bucket = True - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) result = bucket.delete(force=True, connection=connection) self.assertTrue(result is None) expected_cw = [{ @@ -422,7 +418,7 @@ def test_delete_explicit_force_miss_blobs(self): # Note the connection does not have a response for the blob. connection = _Connection(GET_BLOBS_RESP) connection._delete_bucket = True - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) result = bucket.delete(force=True, connection=connection) self.assertTrue(result is None) expected_cw = [{ @@ -444,7 +440,7 @@ def test_delete_explicit_too_many(self): } connection = _Connection(GET_BLOBS_RESP) connection._delete_bucket = True - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) # Make the Bucket refuse to delete with 2 objects. bucket._MAX_OBJECTS_FOR_BUCKET_DELETE = 1 @@ -679,21 +675,16 @@ def test_id(self): def test_location_getter(self): NAME = 'name' - connection = _Connection() before = {'location': 'AS'} - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) self.assertEqual(bucket.location, 'AS') - kw = connection._requested - self.assertEqual(len(kw), 0) def test_location_setter(self): NAME = 'name' - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertEqual(bucket.location, None) bucket.location = 'AS' self.assertEqual(bucket.location, 'AS') - self.assertEqual(len(connection._requested), 0) def test_lifecycle_rules_getter(self): NAME = 'name' @@ -709,13 +700,10 @@ def test_lifecycle_rules_setter(self): NAME = 'name' LC_RULE = {'action': {'type': 'Delete'}, 'condition': {'age': 42}} rules = [LC_RULE] - connection = _Connection() - - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertEqual(bucket.lifecycle_rules, []) bucket.lifecycle_rules = rules self.assertEqual(bucket.lifecycle_rules, rules) - self.assertEqual(len(connection._requested), 0) def test_cors_getter(self): NAME = 'name' @@ -742,13 +730,11 @@ def test_cors_setter(self): 'origin': ['127.0.0.1'], 'responseHeader': ['Content-Type'], } - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertEqual(bucket.cors, []) bucket.cors = [CORS_ENTRY] self.assertEqual(bucket.cors, [CORS_ENTRY]) - self.assertEqual(len(connection._requested), 0) def test_get_logging_w_prefix(self): NAME = 'name' @@ -760,49 +746,41 @@ def test_get_logging_w_prefix(self): 'logObjectPrefix': LOG_PREFIX, }, } - connection = _Connection() - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) info = bucket.get_logging() self.assertEqual(info['logBucket'], LOG_BUCKET) self.assertEqual(info['logObjectPrefix'], LOG_PREFIX) - self.assertEqual(len(connection._requested), 0) def test_enable_logging_defaults(self): NAME = 'name' LOG_BUCKET = 'logs' before = {'logging': None} - connection = _Connection() - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) self.assertTrue(bucket.get_logging() is None) bucket.enable_logging(LOG_BUCKET) info = bucket.get_logging() self.assertEqual(info['logBucket'], LOG_BUCKET) self.assertEqual(info['logObjectPrefix'], '') - self.assertEqual(len(connection._requested), 0) def test_enable_logging_explicit(self): NAME = 'name' LOG_BUCKET = 'logs' LOG_PFX = 'pfx' before = {'logging': None} - connection = _Connection() - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) self.assertTrue(bucket.get_logging() is None) bucket.enable_logging(LOG_BUCKET, LOG_PFX) info = bucket.get_logging() self.assertEqual(info['logBucket'], LOG_BUCKET) self.assertEqual(info['logObjectPrefix'], LOG_PFX) - self.assertEqual(len(connection._requested), 0) def test_disable_logging(self): NAME = 'name' before = {'logging': {'logBucket': 'logs', 'logObjectPrefix': 'pfx'}} - connection = _Connection() - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) self.assertTrue(bucket.get_logging() is not None) bucket.disable_logging() self.assertTrue(bucket.get_logging() is None) - self.assertEqual(len(connection._requested), 0) def test_metageneration(self): METAGENERATION = 42 @@ -872,58 +850,45 @@ def test_time_created_unset(self): def test_versioning_enabled_getter_missing(self): NAME = 'name' - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertEqual(bucket.versioning_enabled, False) - self.assertEqual(len(connection._requested), 0) def test_versioning_enabled_getter(self): NAME = 'name' before = {'versioning': {'enabled': True}} - connection = _Connection() - bucket = self._makeOne(NAME, connection, properties=before) + bucket = self._makeOne(NAME, properties=before) self.assertEqual(bucket.versioning_enabled, True) - kw = connection._requested - self.assertEqual(len(kw), 0) def test_versioning_enabled_setter(self): NAME = 'name' - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) self.assertFalse(bucket.versioning_enabled) bucket.versioning_enabled = True self.assertTrue(bucket.versioning_enabled) - self.assertEqual(len(connection._requested), 0) def test_configure_website_defaults(self): NAME = 'name' UNSET = {'website': {'mainPageSuffix': None, 'notFoundPage': None}} - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) bucket.configure_website() self.assertEqual(bucket._properties, UNSET) - self.assertEqual(len(connection._requested), 0) def test_configure_website_explicit(self): NAME = 'name' WEBSITE_VAL = {'website': {'mainPageSuffix': 'html', 'notFoundPage': '404.html'}} - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) bucket.configure_website('html', '404.html') self.assertEqual(bucket._properties, WEBSITE_VAL) - self.assertEqual(len(connection._requested), 0) def test_disable_website(self): NAME = 'name' UNSET = {'website': {'mainPageSuffix': None, 'notFoundPage': None}} - connection = _Connection() - bucket = self._makeOne(NAME, connection) + bucket = self._makeOne(NAME) bucket.disable_website() self.assertEqual(bucket._properties, UNSET) - self.assertEqual(len(connection._requested), 0) def test_make_public_defaults(self): from gcloud.storage.acl import _ACLEntity @@ -932,7 +897,7 @@ def test_make_public_defaults(self): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after) - bucket = self._makeOne(NAME, None) + bucket = self._makeOne(NAME) bucket.acl.loaded = True bucket.default_object_acl.loaded = True with _monkey_defaults(connection=connection): @@ -961,7 +926,7 @@ def _make_public_w_future_helper(self, default_object_acl_loaded=True): # We return the same value for default_object_acl.reload() # to consume. connection = _Connection(after1, after1, after2) - bucket = self._makeOne(NAME, None) + bucket = self._makeOne(NAME) bucket.acl.loaded = True bucket.default_object_acl.loaded = default_object_acl_loaded with _monkey_defaults(connection=connection): @@ -1027,7 +992,7 @@ def get_items_from_response(self, response): permissive = [{'entity': 'allUsers', 'role': _ACLEntity.READER_ROLE}] after = {'acl': permissive, 'defaultObjectAcl': []} connection = _Connection(after, {'items': [{'name': BLOB_NAME}]}) - bucket = self._makeOne(NAME, None) + bucket = self._makeOne(NAME) bucket.acl.loaded = True bucket.default_object_acl.loaded = True bucket._iterator_class = _Iterator @@ -1087,9 +1052,6 @@ class _Bucket(object): path = '/b/name' name = 'name' - def __init__(self, connection): - self.connection = connection - class MockFile(io.StringIO): name = None diff --git a/regression/storage.py b/regression/storage.py index 4a3cb1695f39..15212f3e1d75 100644 --- a/regression/storage.py +++ b/regression/storage.py @@ -50,9 +50,9 @@ def setUp(self): self.case_buckets_to_delete = [] def tearDown(self): - with storage.Batch() as batch: + with storage.Batch(): for bucket_name in self.case_buckets_to_delete: - storage.Bucket(bucket_name, connection=batch).delete() + storage.Bucket(bucket_name).delete() def test_create_bucket(self): new_bucket_name = 'a-new-bucket' @@ -186,7 +186,7 @@ class TestStorageListFiles(TestStorageFiles): def setUpClass(cls): super(TestStorageListFiles, cls).setUpClass() # Make sure bucket empty before beginning. - for blob in cls.bucket: + for blob in cls.bucket.list_blobs(): blob.delete() logo_path = cls.FILES['logo']['path'] @@ -237,7 +237,7 @@ class TestStoragePseudoHierarchy(TestStorageFiles): def setUpClass(cls): super(TestStoragePseudoHierarchy, cls).setUpClass() # Make sure bucket empty before beginning. - for blob in cls.bucket: + for blob in cls.bucket.list_blobs(): blob.delete() simple_path = cls.FILES['simple']['path'] @@ -337,4 +337,4 @@ def test_create_signed_delete_url(self): self.assertEqual(content, b'') # Check that the blob has actually been deleted. - self.assertFalse(blob.name in self.bucket) + self.assertFalse(blob.exists())