Skip to content

Commit

Permalink
Allow 'destination.content_type' to be None in 'Blob.compose'. (#6031)
Browse files Browse the repository at this point in the history
Closes #5834.
  • Loading branch information
tseaver committed Sep 19, 2018
1 parent dc5d144 commit 6a4a8a7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
6 changes: 0 additions & 6 deletions storage/google/cloud/storage/blob.py
Expand Up @@ -1374,13 +1374,7 @@ def compose(self, sources, client=None):
``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the blob's bucket.
:raises: :exc:`ValueError` if this blob does not have its
:attr:`content_type` set.
"""
if self.content_type is None:
raise ValueError("Destination 'content_type' not set.")

client = self._require_client(client)
query_params = {}

Expand Down
20 changes: 20 additions & 0 deletions storage/tests/system.py
Expand Up @@ -779,6 +779,26 @@ def test_compose_create_new_blob(self):
composed = destination.download_as_string()
self.assertEqual(composed, SOURCE_1 + SOURCE_2)

def test_compose_create_new_blob_wo_content_type(self):
SOURCE_1 = b'AAA\n'
source_1 = self.bucket.blob('source-1')
source_1.upload_from_string(SOURCE_1)
self.case_blobs_to_delete.append(source_1)

SOURCE_2 = b'BBB\n'
source_2 = self.bucket.blob('source-2')
source_2.upload_from_string(SOURCE_2)
self.case_blobs_to_delete.append(source_2)

destination = self.bucket.blob('destination')

destination.compose([source_1, source_2])
self.case_blobs_to_delete.append(destination)

self.assertIsNone(destination.content_type)
composed = destination.download_as_string()
self.assertEqual(composed, SOURCE_1 + SOURCE_2)

def test_compose_replace_existing_blob(self):
BEFORE = b'AAA\n'
original = self.bucket.blob('original')
Expand Down
26 changes: 23 additions & 3 deletions storage/tests/unit/test_blob.py
Expand Up @@ -2113,15 +2113,35 @@ def test_compose_wo_content_type_set(self):
SOURCE_1 = 'source-1'
SOURCE_2 = 'source-2'
DESTINATION = 'destinaton'
connection = _Connection()
RESOURCE = {}
after = ({'status': http_client.OK}, RESOURCE)
connection = _Connection(after)
client = _Client(connection)
bucket = _Bucket(client=client)
source_1 = self._make_one(SOURCE_1, bucket=bucket)
source_2 = self._make_one(SOURCE_2, bucket=bucket)
destination = self._make_one(DESTINATION, bucket=bucket)
# no destination.content_type set

with self.assertRaises(ValueError):
destination.compose(sources=[source_1, source_2])
destination.compose(sources=[source_1, source_2])

self.assertIsNone(destination.content_type)

kw = connection._requested
self.assertEqual(len(kw), 1)
self.assertEqual(kw[0], {
'method': 'POST',
'path': '/b/name/o/%s/compose' % DESTINATION,
'query_params': {},
'data': {
'sourceObjects': [
{'name': source_1.name},
{'name': source_2.name},
],
'destination': {},
},
'_target_object': destination,
})

def test_compose_minimal_w_user_project(self):
SOURCE_1 = 'source-1'
Expand Down

0 comments on commit 6a4a8a7

Please sign in to comment.