109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package socialnetworkservicelogic
|
|
|
|
import (
|
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
|
mocklib "app-cloudep-tweeting-service/internal/mock/lib"
|
|
mockRepo "app-cloudep-tweeting-service/internal/mock/repository"
|
|
"app-cloudep-tweeting-service/internal/svc"
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/mock/gomock"
|
|
)
|
|
|
|
func TestMarkFollowRelationLogic_MarkFollowRelation(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
// 初始化 mock 依賴
|
|
mockSocialNetworkRepository := mockRepo.NewMockSocialNetworkRepository(ctrl)
|
|
mockValidate := mocklib.NewMockValidate(ctrl)
|
|
|
|
// 初始化服務上下文
|
|
svcCtx := &svc.ServiceContext{
|
|
SocialNetworkRepository: mockSocialNetworkRepository,
|
|
Validate: mockValidate,
|
|
}
|
|
|
|
// 測試數據集
|
|
tests := []struct {
|
|
name string
|
|
input *tweeting.DoFollowerRelationReq
|
|
prepare func()
|
|
expectErr bool
|
|
wantResp *tweeting.OKResp
|
|
}{
|
|
{
|
|
name: "成功建立關注關係",
|
|
input: &tweeting.DoFollowerRelationReq{
|
|
FollowerUid: "follower123",
|
|
FolloweeUid: "followee456",
|
|
},
|
|
prepare: func() {
|
|
// 模擬驗證通過
|
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
|
// 模擬成功建立關注關係
|
|
mockSocialNetworkRepository.EXPECT().MarkFollowerRelation(gomock.Any(), "follower123", "followee456").Return(nil).Times(1)
|
|
},
|
|
expectErr: false,
|
|
wantResp: &tweeting.OKResp{},
|
|
},
|
|
{
|
|
name: "驗證失敗",
|
|
input: &tweeting.DoFollowerRelationReq{
|
|
FollowerUid: "",
|
|
FolloweeUid: "",
|
|
},
|
|
prepare: func() {
|
|
// 模擬驗證失敗
|
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
|
},
|
|
expectErr: true,
|
|
wantResp: nil,
|
|
},
|
|
{
|
|
name: "建立關注關係失敗",
|
|
input: &tweeting.DoFollowerRelationReq{
|
|
FollowerUid: "follower123",
|
|
FolloweeUid: "followee456",
|
|
},
|
|
prepare: func() {
|
|
// 模擬驗證通過
|
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
|
// 模擬建立關注關係失敗
|
|
mockSocialNetworkRepository.EXPECT().MarkFollowerRelation(gomock.Any(), "follower123", "followee456").Return(errors.New("repository error")).Times(1)
|
|
},
|
|
expectErr: true,
|
|
wantResp: nil,
|
|
},
|
|
}
|
|
|
|
// 執行測試
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// 設置測試環境
|
|
tt.prepare()
|
|
|
|
// 初始化 MarkFollowRelationLogic
|
|
logic := MarkFollowRelationLogic{
|
|
svcCtx: svcCtx,
|
|
ctx: context.TODO(),
|
|
}
|
|
|
|
// 執行 MarkFollowRelation
|
|
got, err := logic.MarkFollowRelation(tt.input)
|
|
|
|
// 驗證結果
|
|
if tt.expectErr {
|
|
assert.Error(t, err)
|
|
assert.Nil(t, got)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.wantResp, got)
|
|
}
|
|
})
|
|
}
|
|
}
|