Skip to content

Commit

Permalink
[gcloud] Fix saving already gzipped files & tests (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Mar 15, 2024
1 parent 1e1bfd5 commit ce34cf1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
7 changes: 5 additions & 2 deletions storages/backends/gcloud.py
Expand Up @@ -39,7 +39,7 @@
class GoogleCloudFile(CompressedFileMixin, File):
def __init__(self, name, mode, storage):
self.name = name
self.mime_type = mimetypes.guess_type(name)[0]
self.mime_type, self.mime_encoding = mimetypes.guess_type(name)
self._mode = mode
self._storage = storage
self.blob = storage.bucket.get_blob(name, chunk_size=storage.blob_chunk_size)
Expand Down Expand Up @@ -190,8 +190,11 @@ def _save(self, name, content):
content.name = cleaned_name
file_object = GoogleCloudFile(name, "rw", self)

upload_params = {}
blob_params = self.get_object_parameters(name)
if file_object.mime_encoding and CONTENT_ENCODING not in blob_params:
blob_params[CONTENT_ENCODING] = file_object.mime_encoding

upload_params = {}
upload_params["predefined_acl"] = blob_params.pop("acl", self.default_acl)
upload_params[CONTENT_TYPE] = blob_params.pop(
CONTENT_TYPE, file_object.mime_type
Expand Down
16 changes: 6 additions & 10 deletions tests/test_gcloud.py
Expand Up @@ -21,17 +21,15 @@ def setUp(self):
self.bucket_name = "test_bucket"
self.filename = "test_file.txt"
self.storage = gcloud.GoogleCloudStorage(bucket_name=self.bucket_name)


class GCloudStorageTests(GCloudTestCase):
def setUp(self):
super().setUp()
self.client_patcher = mock.patch("storages.backends.gcloud.Client")
self.client_patcher.start()

def tearDown(self):
super().tearDown()
self.client_patcher.stop()


class GCloudStorageTests(GCloudTestCase):
def test_open_read(self):
"""
Test opening a file and reading from it
Expand Down Expand Up @@ -507,7 +505,6 @@ def setUp(self):
self.storage.gzip = True

@mock.patch("google.cloud.storage.blob.Blob._do_upload")
@mock.patch("google.auth.default", return_value=["foo", None])
def test_storage_save_gzipped(self, *args):
"""
Test saving a gzipped file
Expand All @@ -521,24 +518,23 @@ def test_storage_save_gzipped(self, *args):
try:
patcher.start()
self.storage.save(name, content)
blob.upload_from_file.assert_called_with(
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
retry=DEFAULT_RETRY,
size=None,
size=11,
predefined_acl=None,
content_type="application/javascript",
)
finally:
patcher.stop()

@mock.patch("google.cloud.storage.blob.Blob._do_upload")
@mock.patch("google.auth.default", return_value=["foo", None])
def test_storage_save_gzip(self, *args):
"""
Test saving a file with gzip enabled.
"""
self.storage.gzip = True
name = "test_storage_save.css"
content = ContentFile("I should be gzip'd")

Expand Down

0 comments on commit ce34cf1

Please sign in to comment.