-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4091b8b
commit d59a757
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ server | |
# Logs | ||
logs | ||
*.log | ||
api_logs | ||
|
||
# Static | ||
img2 | ||
|
171 changes: 171 additions & 0 deletions
171
internal/microservices/account/usecase/account_usecase_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,172 @@ | ||
package usecase | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger" | ||
mock "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/mocks" | ||
"github.com/go-park-mail-ru/2023_2_Hamster/internal/models" | ||
"github.com/golang/mock/gomock" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUsecase_CreateAccount(t *testing.T) { | ||
testUUID := uuid.New() | ||
testCases := []struct { | ||
name string | ||
expectedID uuid.UUID | ||
expectedErr error | ||
mockRepoFn func(*mock.MockRepository) | ||
}{ | ||
{ | ||
name: "Successful TestUsecase_CreateAccount", | ||
expectedID: testUUID, | ||
expectedErr: nil, | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CreateAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(testUUID, nil) | ||
}, | ||
}, | ||
{ | ||
name: "Error in TestUsecase_CreateAccount", | ||
expectedID: uuid.UUID{}, | ||
expectedErr: fmt.Errorf("[usecase] can't create account into repository: some error"), | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CreateAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(uuid.UUID{}, errors.New("some error")) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRepo := mock.NewMockRepository(ctrl) | ||
tc.mockRepoFn(mockRepo) | ||
|
||
mockUsecase := NewUsecase(mockRepo, *logger.NewLogger(context.TODO())) | ||
|
||
userID := uuid.New() | ||
account := &models.Accounts{} // You should create an instance of your account model here | ||
|
||
accountID, err := mockUsecase.CreateAccount(context.Background(), userID, account) | ||
|
||
assert.Equal(t, tc.expectedID, accountID) | ||
if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) { | ||
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUsecase_UpdateAccount(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
expectedErr error | ||
mockRepoFn func(*mock.MockRepository) | ||
}{ | ||
{ | ||
name: "Successful TestUsecase_UpdateAccount", | ||
expectedErr: nil, | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
mockRepository.EXPECT().UpdateAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
}, | ||
}, | ||
{ | ||
name: "Forbidden Error in TestUsecase_UpdateAccount", | ||
expectedErr: fmt.Errorf("[usecase] can't be update by user: some forbidden error"), | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("some forbidden error")) | ||
}, | ||
}, | ||
{ | ||
name: "Update Error in TestUsecase_UpdateAccount", | ||
expectedErr: fmt.Errorf("[usecase] can't update account into repository: some update error"), | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
mockRepository.EXPECT().UpdateAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("some update error")) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRepo := mock.NewMockRepository(ctrl) | ||
tc.mockRepoFn(mockRepo) | ||
|
||
mockUsecase := NewUsecase(mockRepo, *logger.NewLogger(context.TODO())) | ||
|
||
userID := uuid.New() | ||
account := &models.Accounts{ID: uuid.New()} // Assuming ID is a required field for account | ||
|
||
err := mockUsecase.UpdateAccount(context.Background(), userID, account) | ||
|
||
if (tc.expectedErr == nil && err != nil) || (tc.expectedErr != nil && err == nil) || (tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) { | ||
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestUsecase_DeleteAccount(t *testing.T) { | ||
userIDTest := uuid.New() | ||
accountIDTest := uuid.New() | ||
|
||
testCases := []struct { | ||
name string | ||
expectedErr error | ||
mockRepoFn func(*mock.MockRepository) | ||
}{ | ||
{ | ||
name: "Successful deletion", | ||
expectedErr: nil, | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
mockRepository.EXPECT().DeleteAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
}, | ||
}, | ||
{ | ||
name: "Error in CheckForbidden", | ||
expectedErr: fmt.Errorf("[usecase] can't be delete by user: %w", errors.New("forbidden")), | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("forbidden")) | ||
}, | ||
}, | ||
{ | ||
name: "Error in DeleteAccount", | ||
expectedErr: fmt.Errorf("[usecase] can't delete account into repository: %w", errors.New("repository error")), | ||
mockRepoFn: func(mockRepository *mock.MockRepository) { | ||
mockRepository.EXPECT().CheckForbidden(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) | ||
mockRepository.EXPECT().DeleteAccount(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("repository error")) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRepo := mock.NewMockRepository(ctrl) | ||
tc.mockRepoFn(mockRepo) | ||
|
||
mockUsecase := NewUsecase(mockRepo, *logger.NewLogger(context.TODO())) | ||
|
||
err := mockUsecase.DeleteAccount(context.Background(), userIDTest, accountIDTest) | ||
|
||
if (tc.expectedErr == nil && err != nil) || | ||
(tc.expectedErr != nil && err == nil) || | ||
(tc.expectedErr != nil && err != nil && tc.expectedErr.Error() != err.Error()) { | ||
t.Errorf("Expected error: %v, but got: %v", tc.expectedErr, err) | ||
} | ||
}) | ||
} | ||
} |