def test_override_settings(self): with override_settings(GS_LOCATION='foo1'): storage = gcloud.GoogleCloudStorage() self.assertEqual(storage.location, 'foo1') with override_settings(GS_LOCATION='foo2'): storage = gcloud.GoogleCloudStorage() self.assertEqual(storage.location, 'foo2')
def test_location_leading_slash(self): msg = ( "GoogleCloudStorage.location cannot begin with a leading slash. " "Found '/'. Use '' instead." ) with self.assertRaises(ImproperlyConfigured, msg=msg): gcloud.GoogleCloudStorage(location='/')
def setUp(self): self.bucket_name = 'test_bucket' self.filename = 'test_file.txt' self.storage = gcloud.GoogleCloudStorage(bucket_name=self.bucket_name) self.client_patcher = mock.patch('storages.backends.gcloud.Client') self.client_patcher.start()
def test_deprecated_autocreate_bucket(self): with warnings.catch_warnings(record=True) as w: gcloud.GoogleCloudStorage(auto_create_bucket=True) assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) message = ( "Automatic bucket creation will be removed in version 1.10. It encourages " "using overly broad credentials with this library. Either create it before " "manually or use one of a myriad of automatic configuration management tools. " "Unset GS_AUTO_CREATE_BUCKET (it defaults to False) to silence this warning." ) assert str(w[-1].message) == message
def test_dupe_file_chunk_size(self): """ Tests that recreating a file that already exists in the bucket respects the `GS_BLOB_CHUNK_SIZE` setting """ chunk_size = 1024 * 256 with override_settings(GS_BLOB_CHUNK_SIZE=chunk_size): # Creating a new storage here since chunk-size is set as an # attribute on init storage = gcloud.GoogleCloudStorage() storage._bucket = mock.MagicMock() # Confirms that `get_blob` always returns a truthy value storage._bucket.get_blob.return_value = True storage.open(self.filename, 'wb') storage._bucket.get_blob.assert_called_with(self.filename, chunk_size=chunk_size)
def test_override_init_argument(self): storage = gcloud.GoogleCloudStorage(location='foo1') self.assertEqual(storage.location, 'foo1') storage = gcloud.GoogleCloudStorage(location='foo2') self.assertEqual(storage.location, 'foo2')
self.storage._bucket.get_blob.return_value = None self.storage.file_overwrite = False self.assertEqual(self.storage.get_available_name( self.filename), self.filename) self.storage._bucket.get_blob.assert_called_with(self.filename) def test_get_available_name_unicode(self): filename = 'ủⓝï℅ⅆℇ.txt' self.assertEqual(self.storage.get_available_name(filename), filename) def test_cache_control(self): data = 'This is some test content.' filename = 'cache_control_file.txt' content = ContentFile(data) cache_control = 'public, max-age=604800' self.storage.cache_control = cache_control self.storage.save(filename, content) bucket = self.storage.client.get_bucket(self.bucket_name) blob = bucket.get_blob(filename) self.assertEqual(blob.cache_control, cache_control) def test_location_leading_slash(self): msg = ( "GoogleCloudStorage.location cannot begin with a leading slash. " "Found '/'. Use '' instead." ) with self.assertRaises(ImproperlyConfigured, msg=msg): gcloud.GoogleCloudStorage(location='/')