Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Gutiérrez committed Oct 20, 2021
1 parent 004c1e2 commit 9492414
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
4 changes: 2 additions & 2 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ def _save(self, name, content):
if self.gzip and upload_params.get(CONTENT_TYPE) in self.gzip_content_types:
content = self._compress_content(content)
file_object.blob.content_encoding = 'gzip'
CONTENT_TYPE[CONTENT_ENCODING] = 'gzip'
upload_params[CONTENT_ENCODING] = 'gzip'

for prop, val in blob_params.items():
setattr(file_object.blob, prop, val)

file_object.blob.upload_from_file(content, rewind=True, size=content.size, **upload_params)
file_object.blob.upload_from_file(content, rewind=True, size=getattr(content, 'size', None), **upload_params)
return cleaned_name

def get_object_parameters(self, name):
Expand Down
1 change: 0 additions & 1 deletion storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def close(self):

@deconstructible
class S3Boto3Storage(CompressStorageMixin, BaseStorage):

"""
Amazon Simple Storage Service using Boto3
Expand Down
70 changes: 57 additions & 13 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from google.cloud.storage.blob import Blob

from storages.backends import gcloud
from tests.test_s3boto3 import NonSeekableContentFile


class GCloudTestCase(TestCase):
Expand Down Expand Up @@ -102,7 +103,7 @@ def test_save(self):
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(self.filename)[0],
content_encoding=None, predefined_acl=None)
predefined_acl=None)

def test_save2(self):
data = 'This is some test ủⓝï℅ⅆℇ content.'
Expand All @@ -114,7 +115,7 @@ def test_save2(self):
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0],
content_encoding=None, predefined_acl=None)
predefined_acl=None)

def test_save_with_default_acl(self):
data = 'This is some test ủⓝï℅ⅆℇ content.'
Expand All @@ -131,7 +132,7 @@ def test_save_with_default_acl(self):
self.storage._client.bucket.assert_called_with(self.bucket_name)
self.storage._bucket.get_blob().upload_from_file.assert_called_with(
content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0],
content_encoding=None, predefined_acl='publicRead')
predefined_acl='publicRead')

def test_delete(self):
self.storage.delete(self.filename)
Expand Down Expand Up @@ -403,26 +404,62 @@ def test_cache_control(self):
blob = bucket.get_blob(filename)
self.assertEqual(blob.cache_control, cache_control)

def test_save_gzip(self):
def test_storage_save_gzipped(self):
"""
Test saving a file with gzip enabled.
Test saving a gzipped file
"""
self.storage.gzip = True
name = 'test_storage_save.gz'
content = ContentFile("I am gzip'd")
self.storage.save(name, content)
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
size=11,
predefined_acl=None,
content_type=None
)

data = "I should be gzip'd"
filename = 'test_storage_save.css'
content = ContentFile(data)
self.storage.save(filename, content)
def test_storage_save_gzipped_non_seekable(self):
"""
Test saving a gzipped file
"""
name = 'test_storage_save.gz'
content = NonSeekableContentFile("I am gzip'd")
self.storage.save(name, content)
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
size=11,
predefined_acl=None,
content_type=None
)

def test_storage_save_gzip(self):
"""
Test saving a file with gzip enabled.
"""
self.storage.gzip = True
name = 'test_storage_save.css'
content = ContentFile("I should be gzip'd")
self.storage.save(name, content)
self.storage._client.bucket.assert_called_with(self.bucket_name)
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
size=None,
predefined_acl=None,
content_type='text/css',
content_encoding='gzip'
)
args, kwargs = obj.upload_from_file.call_args
content = args[0]
zfile = gzip.GzipFile(mode='rb', fileobj=content)
self.assertEqual(zfile.read(), b"I should be gzip'd")

def test__save_gzip_twice(self):
def test_storage_save_gzip_twice(self):
"""
Test saving the same file content twice with gzip enabled.
"""
Expand All @@ -438,7 +475,14 @@ def test__save_gzip_twice(self):
# Then
self.storage._client.bucket.assert_called_with(self.bucket_name)
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
size=None,
predefined_acl=None,
content_type='text/css',
content_encoding='gzip'
)
args, kwargs = obj.upload_from_file.call_args
content = args[0]
zfile = gzip.GzipFile(mode='rb', fileobj=content)
Expand Down

0 comments on commit 9492414

Please sign in to comment.