From fa6d6604b0079bdbfe4f6196f780be9ff4689994 Mon Sep 17 00:00:00 2001 From: Haris Thohir Date: Tue, 11 Jun 2024 11:59:19 +0700 Subject: [PATCH 1/2] refactor: naming --- gcs_manager.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gcs_manager.go b/gcs_manager.go index e26f6d1..da2aaa5 100644 --- a/gcs_manager.go +++ b/gcs_manager.go @@ -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) @@ -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 { @@ -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 { @@ -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) @@ -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) From 320803af5a4b6da74581ae27d34d6c37d970692a Mon Sep 17 00:00:00 2001 From: Haris Thohir Date: Tue, 11 Jun 2024 11:59:34 +0700 Subject: [PATCH 2/2] test: adjust unit test --- gcs_manager_mock.go | 55 +++++++++++++++++++++++---------------------- gcs_manager_test.go | 22 ++++++++++++------ 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/gcs_manager_mock.go b/gcs_manager_mock.go index 14dc4f1..06a3230 100644 --- a/gcs_manager_mock.go +++ b/gcs_manager_mock.go @@ -5,36 +5,37 @@ package gcs import ( + os "os" reflect "reflect" gomock "github.com/golang/mock/gomock" ) -// MockGCSManagerInterface is a mock of GCSManagerInterface interface. -type MockGCSManagerInterface struct { +// MockGCSManager is a mock of GCSManager interface. +type MockGCSManager struct { ctrl *gomock.Controller - recorder *MockGCSManagerInterfaceMockRecorder + recorder *MockGCSManagerMockRecorder } -// MockGCSManagerInterfaceMockRecorder is the mock recorder for MockGCSManagerInterface. -type MockGCSManagerInterfaceMockRecorder struct { - mock *MockGCSManagerInterface +// MockGCSManagerMockRecorder is the mock recorder for MockGCSManager. +type MockGCSManagerMockRecorder struct { + mock *MockGCSManager } -// NewMockGCSManagerInterface creates a new mock instance. -func NewMockGCSManagerInterface(ctrl *gomock.Controller) *MockGCSManagerInterface { - mock := &MockGCSManagerInterface{ctrl: ctrl} - mock.recorder = &MockGCSManagerInterfaceMockRecorder{mock} +// NewMockGCSManager creates a new mock instance. +func NewMockGCSManager(ctrl *gomock.Controller) *MockGCSManager { + mock := &MockGCSManager{ctrl: ctrl} + mock.recorder = &MockGCSManagerMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockGCSManagerInterface) EXPECT() *MockGCSManagerInterfaceMockRecorder { +func (m *MockGCSManager) EXPECT() *MockGCSManagerMockRecorder { return m.recorder } // DeleteAllFilesInDirectory mocks base method. -func (m *MockGCSManagerInterface) DeleteAllFilesInDirectory(bucketName, directory string) error { +func (m *MockGCSManager) DeleteAllFilesInDirectory(bucketName, directory string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteAllFilesInDirectory", bucketName, directory) ret0, _ := ret[0].(error) @@ -42,13 +43,13 @@ func (m *MockGCSManagerInterface) DeleteAllFilesInDirectory(bucketName, director } // DeleteAllFilesInDirectory indicates an expected call of DeleteAllFilesInDirectory. -func (mr *MockGCSManagerInterfaceMockRecorder) DeleteAllFilesInDirectory(bucketName, directory interface{}) *gomock.Call { +func (mr *MockGCSManagerMockRecorder) DeleteAllFilesInDirectory(bucketName, directory interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAllFilesInDirectory", reflect.TypeOf((*MockGCSManagerInterface)(nil).DeleteAllFilesInDirectory), bucketName, directory) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteAllFilesInDirectory", reflect.TypeOf((*MockGCSManager)(nil).DeleteAllFilesInDirectory), bucketName, directory) } // DeleteFile mocks base method. -func (m *MockGCSManagerInterface) DeleteFile(bucketName, objectName string) error { +func (m *MockGCSManager) DeleteFile(bucketName, objectName string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteFile", bucketName, objectName) ret0, _ := ret[0].(error) @@ -56,13 +57,13 @@ func (m *MockGCSManagerInterface) DeleteFile(bucketName, objectName string) erro } // DeleteFile indicates an expected call of DeleteFile. -func (mr *MockGCSManagerInterfaceMockRecorder) DeleteFile(bucketName, objectName interface{}) *gomock.Call { +func (mr *MockGCSManagerMockRecorder) DeleteFile(bucketName, objectName interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteFile", reflect.TypeOf((*MockGCSManagerInterface)(nil).DeleteFile), bucketName, objectName) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteFile", reflect.TypeOf((*MockGCSManager)(nil).DeleteFile), bucketName, objectName) } // ListFiles mocks base method. -func (m *MockGCSManagerInterface) ListFiles(bucketName string) ([]string, error) { +func (m *MockGCSManager) ListFiles(bucketName string) ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ListFiles", bucketName) ret0, _ := ret[0].([]string) @@ -71,13 +72,13 @@ func (m *MockGCSManagerInterface) ListFiles(bucketName string) ([]string, error) } // ListFiles indicates an expected call of ListFiles. -func (mr *MockGCSManagerInterfaceMockRecorder) ListFiles(bucketName interface{}) *gomock.Call { +func (mr *MockGCSManagerMockRecorder) ListFiles(bucketName interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFiles", reflect.TypeOf((*MockGCSManagerInterface)(nil).ListFiles), bucketName) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFiles", reflect.TypeOf((*MockGCSManager)(nil).ListFiles), bucketName) } // MoveFile mocks base method. -func (m *MockGCSManagerInterface) MoveFile(bucketName, srcObjectName, dstObjectName string) error { +func (m *MockGCSManager) MoveFile(bucketName, srcObjectName, dstObjectName string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "MoveFile", bucketName, srcObjectName, dstObjectName) ret0, _ := ret[0].(error) @@ -85,21 +86,21 @@ func (m *MockGCSManagerInterface) MoveFile(bucketName, srcObjectName, dstObjectN } // MoveFile indicates an expected call of MoveFile. -func (mr *MockGCSManagerInterfaceMockRecorder) MoveFile(bucketName, srcObjectName, dstObjectName interface{}) *gomock.Call { +func (mr *MockGCSManagerMockRecorder) MoveFile(bucketName, srcObjectName, dstObjectName interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MoveFile", reflect.TypeOf((*MockGCSManagerInterface)(nil).MoveFile), bucketName, srcObjectName, dstObjectName) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MoveFile", reflect.TypeOf((*MockGCSManager)(nil).MoveFile), bucketName, srcObjectName, dstObjectName) } // UploadFile mocks base method. -func (m *MockGCSManagerInterface) UploadFile(bucketName, objectName, filePath string) error { +func (m *MockGCSManager) UploadFile(bucketName, objectName string, file os.File) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UploadFile", bucketName, objectName, filePath) + ret := m.ctrl.Call(m, "UploadFile", bucketName, objectName, file) ret0, _ := ret[0].(error) return ret0 } // UploadFile indicates an expected call of UploadFile. -func (mr *MockGCSManagerInterfaceMockRecorder) UploadFile(bucketName, objectName, filePath interface{}) *gomock.Call { +func (mr *MockGCSManagerMockRecorder) UploadFile(bucketName, objectName, file interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UploadFile", reflect.TypeOf((*MockGCSManagerInterface)(nil).UploadFile), bucketName, objectName, filePath) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UploadFile", reflect.TypeOf((*MockGCSManager)(nil).UploadFile), bucketName, objectName, file) } diff --git a/gcs_manager_test.go b/gcs_manager_test.go index b426d7e..d874df1 100644 --- a/gcs_manager_test.go +++ b/gcs_manager_test.go @@ -1,6 +1,8 @@ package gcs import ( + "io" + "os" "testing" "github.com/golang/mock/gomock" @@ -11,10 +13,16 @@ 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) } @@ -22,7 +30,7 @@ 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") @@ -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) @@ -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") @@ -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/")