Skip to content

Commit

Permalink
Merge branch 'feat/PB-4017-gcs-manager'
Browse files Browse the repository at this point in the history
  • Loading branch information
haristhohir committed Jun 11, 2024
2 parents 4e14bbd + 320803a commit af4b368
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 44 deletions.
20 changes: 10 additions & 10 deletions gcs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,34 @@ import (
)

type (
GCSManagerInterface interface {
UploadFile(bucketName, objectName, filePath string) error
GCSManager interface {
UploadFile(bucketName, objectName string, file os.File) error
DeleteFile(bucketName, objectName string) error
ListFiles(bucketName string) ([]string, error)
MoveFile(bucketName, srcObjectName, dstObjectName string) error
DeleteAllFilesInDirectory(bucketName, directory string) error
}

GCSManager struct {
gcsManager struct {
client *storage.Client
ctx context.Context
}
)

func NewGCSManager(credentialsFile string) (*GCSManager, error) {
func NewGCSManager(credentialsFile string) (GCSManager, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
return nil, fmt.Errorf("failed to create GCS client: %v", err)
}

return &GCSManager{
return &gcsManager{
client: client,
ctx: ctx,
}, nil
}

func (g *GCSManager) UploadFile(bucketName, objectName string, file os.File) error {
func (g gcsManager) UploadFile(bucketName, objectName string, file os.File) error {
bucket := g.client.Bucket(bucketName)
object := bucket.Object(objectName)
writer := object.NewWriter(g.ctx)
Expand All @@ -52,7 +52,7 @@ func (g *GCSManager) UploadFile(bucketName, objectName string, file os.File) err
return nil
}

func (g *GCSManager) DeleteFile(bucketName, objectName string) error {
func (g gcsManager) DeleteFile(bucketName, objectName string) error {
bucket := g.client.Bucket(bucketName)
object := bucket.Object(objectName)
if err := object.Delete(g.ctx); err != nil {
Expand All @@ -62,7 +62,7 @@ func (g *GCSManager) DeleteFile(bucketName, objectName string) error {
return nil
}

func (g *GCSManager) ListFiles(bucketName string) ([]string, error) {
func (g gcsManager) ListFiles(bucketName string) ([]string, error) {
var files []string
it := g.client.Bucket(bucketName).Objects(g.ctx, nil)
for {
Expand All @@ -78,7 +78,7 @@ func (g *GCSManager) ListFiles(bucketName string) ([]string, error) {
return files, nil
}

func (g *GCSManager) MoveFile(bucketName, srcObjectName, dstObjectName string) error {
func (g gcsManager) MoveFile(bucketName, srcObjectName, dstObjectName string) error {
src := g.client.Bucket(bucketName).Object(srcObjectName)
dst := g.client.Bucket(bucketName).Object(dstObjectName)

Expand All @@ -94,7 +94,7 @@ func (g *GCSManager) MoveFile(bucketName, srcObjectName, dstObjectName string) e
return nil
}

func (g *GCSManager) DeleteAllFilesInDirectory(bucketName, directory string) error {
func (g gcsManager) DeleteAllFilesInDirectory(bucketName, directory string) error {
bucket := g.client.Bucket(bucketName)
query := &storage.Query{Prefix: directory}
it := bucket.Objects(g.ctx, query)
Expand Down
55 changes: 28 additions & 27 deletions gcs_manager_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions gcs_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gcs

import (
"io"
"os"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -11,18 +13,24 @@ func TestUploadFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManagerInterface(ctrl)
mockGCS.EXPECT().UploadFile("bucketName", "objectName", "filePath").Return(nil)
mockGCS := NewMockGCSManager(ctrl)
fileContent := []byte("test content")
file := os.NewFile(0, "test-file")
defer file.Close()
file.Write(fileContent)
file.Seek(0, io.SeekStart)

err := mockGCS.UploadFile("bucketName", "objectName", "filePath")
mockGCS.EXPECT().UploadFile("bucketName", "objectName", *file).Return(nil)

err := mockGCS.UploadFile("bucketName", "objectName", *file)
assert.NoError(t, err)
}

func TestDeleteFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManagerInterface(ctrl)
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DeleteFile("bucketName", "objectName").Return(nil)

err := mockGCS.DeleteFile("bucketName", "objectName")
Expand All @@ -33,7 +41,7 @@ func TestListFiles(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManagerInterface(ctrl)
mockGCS := NewMockGCSManager(ctrl)
expectedFiles := []string{"file1", "file2"}
mockGCS.EXPECT().ListFiles("bucketName").Return(expectedFiles, nil)

Expand All @@ -46,7 +54,7 @@ func TestMoveFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManagerInterface(ctrl)
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().MoveFile("bucketName", "srcObjectName", "dstObjectName").Return(nil)

err := mockGCS.MoveFile("bucketName", "srcObjectName", "dstObjectName")
Expand All @@ -57,7 +65,7 @@ func TestDeleteAllFilesInDirectory(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManagerInterface(ctrl)
mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DeleteAllFilesInDirectory("bucketName", "directory/").Return(nil)

err := mockGCS.DeleteAllFilesInDirectory("bucketName", "directory/")
Expand Down

0 comments on commit af4b368

Please sign in to comment.