From 59525d8a05438ed105ebbe9d140f54c58e626ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Sat, 8 Feb 2025 10:06:43 +0800 Subject: [PATCH] fix: test case --- pkg/mock/repository/account.go | 9 +++++---- pkg/repository/account_test.go | 6 +++++- pkg/usecase/member_test.go | 14 +++++++------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pkg/mock/repository/account.go b/pkg/mock/repository/account.go index 00e6f56..9d15887 100644 --- a/pkg/mock/repository/account.go +++ b/pkg/mock/repository/account.go @@ -14,6 +14,7 @@ import ( reflect "reflect" entity "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/entity" + member "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/member" mongo "go.mongodb.org/mongo-driver/mongo" gomock "go.uber.org/mock/gomock" ) @@ -132,17 +133,17 @@ func (mr *MockAccountRepositoryMockRecorder) Update(ctx, data any) *gomock.Call } // UpdateTokenByLoginID mocks base method. -func (m *MockAccountRepository) UpdateTokenByLoginID(ctx context.Context, account, token string) error { +func (m *MockAccountRepository) UpdateTokenByLoginID(ctx context.Context, account, token string, platform member.Platform) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "UpdateTokenByLoginID", ctx, account, token) + ret := m.ctrl.Call(m, "UpdateTokenByLoginID", ctx, account, token, platform) ret0, _ := ret[0].(error) return ret0 } // UpdateTokenByLoginID indicates an expected call of UpdateTokenByLoginID. -func (mr *MockAccountRepositoryMockRecorder) UpdateTokenByLoginID(ctx, account, token any) *gomock.Call { +func (mr *MockAccountRepositoryMockRecorder) UpdateTokenByLoginID(ctx, account, token, platform any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTokenByLoginID", reflect.TypeOf((*MockAccountRepository)(nil).UpdateTokenByLoginID), ctx, account, token) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTokenByLoginID", reflect.TypeOf((*MockAccountRepository)(nil).UpdateTokenByLoginID), ctx, account, token, platform) } // MockAccountIndexUP is a mock of AccountIndexUP interface. diff --git a/pkg/repository/account_test.go b/pkg/repository/account_test.go index 71e6de0..7c946ca 100644 --- a/pkg/repository/account_test.go +++ b/pkg/repository/account_test.go @@ -1,6 +1,7 @@ package repository import ( + "code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/member" "context" "errors" "fmt" @@ -313,6 +314,7 @@ func TestAccountModel_UpdateTokenByLoginID(t *testing.T) { name string loginID string newToken string + platform int64 expectedErr error expectFound bool }{ @@ -320,6 +322,7 @@ func TestAccountModel_UpdateTokenByLoginID(t *testing.T) { name: "Valid Update Token", loginID: "testuser2", newToken: "newtoken123", + platform: 1, expectedErr: nil, expectFound: true, }, @@ -327,6 +330,7 @@ func TestAccountModel_UpdateTokenByLoginID(t *testing.T) { name: "Account Not Found for Update", loginID: "nonexistentuser", newToken: "newtoken456", + platform: 1, expectedErr: ErrNotFound, expectFound: false, }, @@ -334,7 +338,7 @@ func TestAccountModel_UpdateTokenByLoginID(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := repo.UpdateTokenByLoginID(context.Background(), tt.loginID, tt.newToken) + err := repo.UpdateTokenByLoginID(context.Background(), tt.loginID, tt.newToken, member.Platform(tt.platform)) if tt.expectFound { assert.NoError(t, err) diff --git a/pkg/usecase/member_test.go b/pkg/usecase/member_test.go index d177d8a..378372a 100644 --- a/pkg/usecase/member_test.go +++ b/pkg/usecase/member_test.go @@ -414,17 +414,17 @@ func TestUpdateUserToken(t *testing.T) { }{ { name: "Successful token update", - req: usecase.UpdateTokenRequest{Account: "testAccount", Token: "newPassword"}, + req: usecase.UpdateTokenRequest{Account: "testAccount", Token: "newPassword", Platform: 1}, mockSetup: func() { mockAccountRepo.EXPECT(). - UpdateTokenByLoginID(gomock.Any(), "testAccount", "encrypted-password"). + UpdateTokenByLoginID(gomock.Any(), "testAccount", "encrypted-password", gomock.Any()). Return(nil) }, wantErr: false, }, { name: "Password encryption failure", - req: usecase.UpdateTokenRequest{Account: "testAccount", Token: "fail"}, + req: usecase.UpdateTokenRequest{Account: "testAccount", Token: "fail", Platform: 1}, mockSetup: func() { // No repo call expected }, @@ -432,20 +432,20 @@ func TestUpdateUserToken(t *testing.T) { }, { name: "Account not found", - req: usecase.UpdateTokenRequest{Account: "nonExistentAccount", Token: "newPassword"}, + req: usecase.UpdateTokenRequest{Account: "nonExistentAccount", Token: "newPassword", Platform: 1}, mockSetup: func() { mockAccountRepo.EXPECT(). - UpdateTokenByLoginID(gomock.Any(), "nonExistentAccount", "encrypted-password"). + UpdateTokenByLoginID(gomock.Any(), "nonExistentAccount", "encrypted-password", gomock.Any()). Return(mon.ErrNotFound) }, wantErr: true, }, { name: "Database error during token update", - req: usecase.UpdateTokenRequest{Account: "errorAccount", Token: "newPassword"}, + req: usecase.UpdateTokenRequest{Account: "errorAccount", Token: "newPassword", Platform: 1}, mockSetup: func() { mockAccountRepo.EXPECT(). - UpdateTokenByLoginID(gomock.Any(), "errorAccount", "encrypted-password"). + UpdateTokenByLoginID(gomock.Any(), "errorAccount", "encrypted-password", gomock.Any()). Return(errors.New("database error")) }, wantErr: true,