def test_cloud_storage_get_bucket__not_found_error(self): """Test the get_bucket API method for an invalid project.""" test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock.Mock()) test_cloud_storage_api._client.get_bucket.side_effect = exceptions.NotFound( 'The bucket requested was not found.') with self.assertRaises(storage.NotFoundError): test_cloud_storage_api.get_bucket()
def test_cloud_storage_api_insert_bucket__already_exists_error(self): """Test the Cloud Storage Bucket creation for an existing bucket.""" test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock.Mock()) test_cloud_storage_api._client.create_bucket.side_effect = ( exceptions.Conflict('This bucket already exists.')) with self.assertRaises(storage.AlreadyExistsError): test_cloud_storage_api.insert_bucket()
def test_cloud_storage_insert_bucket(self, test_bucket, expected_bucket): """Test the insert_bucket method for the Cloud Storage API.""" mock_client = mock.Mock() mock_client.create_bucket.return_value = _FAKE_BUCKET_OBJECT test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) self.assertEqual(_FAKE_BUCKET_OBJECT, test_cloud_storage_api.insert_bucket(test_bucket)) mock_client.create_bucket.assert_called_once_with(expected_bucket)
def test_cloud_storage_get_bucket__not_found_error(self): """Test the get_bucket API method for an invalid project.""" test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock.Mock()) test_cloud_storage_api._client.buckets.side_effect = errors.HttpError( httplib2.Response({ 'reason': 'Not found.', 'status': httplib.NOT_FOUND }), 'Bucket not found.') with self.assertRaises(storage.NotFoundError): test_cloud_storage_api.get_bucket()
def test_cloud_storage_get_bucket(self): """Test the get_bucket API method for the Google Cloud Storage API.""" mock_client = mock.Mock() mock_get = mock_client.buckets.return_value.get mock_get.return_value.execute.return_value = _FAKE_BUCKET_OBJECT test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) self.assertEqual(_FAKE_BUCKET_OBJECT, test_cloud_storage_api.get_bucket()) mock_get.assert_called_once_with(bucket=self.config.bucket)
def test_cloud_storage_api_insert_bucket__insertion_error(self): """Test the Cloud Storage Bucket insert unknown error..""" test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock.Mock()) test_cloud_storage_api._client.buckets.side_effect = errors.HttpError( httplib2.Response({ 'reason': 'Not found', 'status': httplib.NOT_FOUND }), 'Resource not found.') with self.assertRaises(storage.InsertionError): test_cloud_storage_api.insert_bucket()
def test_cloud_storage_api_insert_bucket__already_exists_error(self): """Test the Cloud Storage Bucket creation for an existing bucket.""" test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock.Mock()) test_cloud_storage_api._client.buckets.side_effect = errors.HttpError( httplib2.Response({ 'reason': 'Conflict', 'status': httplib.CONFLICT }), 'Bucket already exists.') with self.assertRaises(storage.AlreadyExistsError): test_cloud_storage_api.insert_bucket()
def test_cloud_storage_insert_bucket(self): """Test the insert_bucket method for the Cloud Storage API.""" mock_client = mock.Mock() mock_insert = mock_client.buckets.return_value.insert mock_insert.return_value.execute.return_value = _FAKE_BUCKET_OBJECT test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) self.assertEqual(_FAKE_BUCKET_OBJECT, test_cloud_storage_api.insert_bucket()) mock_insert.assert_called_once_with(body={'name': self.config.bucket}, project=self.config.project)
def test_cloud_storage_get_blob__not_found_error(self, test_error, mock_blob_class): """Test the retrieval of a Blob that cannot be found.""" mock_blob = mock_blob_class.return_value mock_blob.download_as_string.side_effect = test_error mock_client = mock.Mock() mock_client.get_bucket.return_value.get_blob.return_value = mock_blob test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) with self.assertRaises(storage.NotFoundError): test_cloud_storage_api.get_blob('TEST_BLOB')
def test_cloud_storage_get_blob(self, test_bucket, expected_bucket): """Test the retrieval of a Blob from Google Cloud Storage.""" with mock.patch.object(storage_client, 'Blob', autospec=True) as mock_blob_class: mock_blob = mock_blob_class.return_value mock_blob.download_as_string.return_value = '{"key": "value"}' mock_client = mock.Mock() mock_bucket = mock_client.get_bucket.return_value mock_bucket.get_blob.return_value = mock_blob test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) self.assertEqual({'key': 'value'}, test_cloud_storage_api.get_blob( 'TEST_BLOB', test_bucket)) mock_client.get_bucket.assert_called_once_with(expected_bucket) mock_bucket.get_blob.assert_called_once_with( 'TEST_BLOB', mock_client) mock_blob.download_as_string.assert_called_once_with(mock_client)
def test_cloud_storage_insert_blob(self, test_bucket, expected_bucket, mock_blob_class): """Test the creation of a Blob on Google Cloud Storage.""" mock_blob = mock_blob_class.return_value mock_client = mock.Mock() test_cloud_storage_api = storage.CloudStorageAPI( self.config, mock_client) with mock.patch.object( test_cloud_storage_api, 'get_bucket', return_value=_FAKE_BUCKET_OBJECT) as mock_get_bucket: test_cloud_storage_api.insert_blob('TEST_PATH', {'key': 'value'}, test_bucket) mock_get_bucket.assert_called_once_with(expected_bucket) mock_blob.upload_from_string.assert_called_once_with( data='{"key": "value"}', content_type='application/json', client=mock_client, )
def test_create_storage_api(self): """Test the initialization of the Cloud Storage API helper class.""" test_storage_api = storage.CloudStorageAPI(self.config, mock.Mock()) self.assertEqual(self.config, test_storage_api._config) self.assertEqual('CloudStorageAPI for project: TEST_PROJECT.', str(test_storage_api))