add timeline test
This commit is contained in:
parent
184586cdff
commit
a72c013982
|
@ -0,0 +1,137 @@
|
|||
package timelineservicelogic
|
||||
|
||||
import (
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/domain/repository"
|
||||
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 TestAddPostLogic_AddPost(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// 初始化 mock 依賴
|
||||
mockTimelineRepo := mockRepo.NewMockTimelineRepository(ctrl)
|
||||
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||
|
||||
// 初始化服務上下文
|
||||
svcCtx := &svc.ServiceContext{
|
||||
TimelineRepo: mockTimelineRepo,
|
||||
Validate: mockValidate,
|
||||
}
|
||||
|
||||
// 測試數據集
|
||||
tests := []struct {
|
||||
name string
|
||||
input *tweeting.AddPostToTimelineReq
|
||||
prepare func()
|
||||
expectErr bool
|
||||
wantResp *tweeting.OKResp
|
||||
}{
|
||||
{
|
||||
name: "成功加入貼文",
|
||||
input: &tweeting.AddPostToTimelineReq{
|
||||
Uid: "user123",
|
||||
Posts: []*tweeting.PostTimelineItem{
|
||||
{PostId: "post1", CreatedAt: 1627890123},
|
||||
{PostId: "post2", CreatedAt: 1627890124},
|
||||
},
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬成功加入貼文
|
||||
mockTimelineRepo.EXPECT().AddPost(gomock.Any(), repository.AddPostRequest{
|
||||
UID: "user123",
|
||||
PostItems: []repository.TimelineItem{
|
||||
{PostID: "post1", Score: 1627890123},
|
||||
{PostID: "post2", Score: 1627890124},
|
||||
},
|
||||
}).Return(nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.OKResp{},
|
||||
},
|
||||
{
|
||||
name: "驗證失敗",
|
||||
input: &tweeting.AddPostToTimelineReq{
|
||||
Uid: "",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證失敗
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
{
|
||||
name: "沒有貼文資料",
|
||||
input: &tweeting.AddPostToTimelineReq{
|
||||
Uid: "user123",
|
||||
Posts: []*tweeting.PostTimelineItem{},
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.OKResp{},
|
||||
},
|
||||
{
|
||||
name: "加入貼文失敗",
|
||||
input: &tweeting.AddPostToTimelineReq{
|
||||
Uid: "user123",
|
||||
Posts: []*tweeting.PostTimelineItem{
|
||||
{PostId: "post1", CreatedAt: 1627890123},
|
||||
},
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬加入貼文失敗
|
||||
mockTimelineRepo.EXPECT().AddPost(gomock.Any(), repository.AddPostRequest{
|
||||
UID: "user123",
|
||||
PostItems: []repository.TimelineItem{
|
||||
{PostID: "post1", Score: 1627890123},
|
||||
},
|
||||
}).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()
|
||||
|
||||
// 初始化 AddPostLogic
|
||||
logic := AddPostLogic{
|
||||
svcCtx: svcCtx,
|
||||
ctx: context.TODO(),
|
||||
}
|
||||
|
||||
// 執行 AddPost
|
||||
got, err := logic.AddPost(tt.input)
|
||||
|
||||
// 驗證結果
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantResp, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package timelineservicelogic
|
||||
|
||||
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 TestClearNoMoreDataFlagLogic_ClearNoMoreDataFlag(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// 初始化 mock 依賴
|
||||
mockTimelineRepo := mockRepo.NewMockTimelineRepository(ctrl)
|
||||
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||
|
||||
// 初始化服務上下文
|
||||
svcCtx := &svc.ServiceContext{
|
||||
TimelineRepo: mockTimelineRepo,
|
||||
Validate: mockValidate,
|
||||
}
|
||||
|
||||
// 測試數據集
|
||||
tests := []struct {
|
||||
name string
|
||||
input *tweeting.DoNoMoreDataReq
|
||||
prepare func()
|
||||
expectErr bool
|
||||
wantResp *tweeting.OKResp
|
||||
}{
|
||||
{
|
||||
name: "成功清除 NoMoreData 標誌",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬成功清除 NoMoreData 標誌
|
||||
mockTimelineRepo.EXPECT().ClearNoMoreDataFlag(gomock.Any(), "user123").Return(nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.OKResp{},
|
||||
},
|
||||
{
|
||||
name: "驗證失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證失敗
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
{
|
||||
name: "清除 NoMoreData 標誌失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬清除 NoMoreData 標誌失敗
|
||||
mockTimelineRepo.EXPECT().ClearNoMoreDataFlag(gomock.Any(), "user123").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()
|
||||
|
||||
// 初始化 ClearNoMoreDataFlagLogic
|
||||
logic := ClearNoMoreDataFlagLogic{
|
||||
svcCtx: svcCtx,
|
||||
ctx: context.TODO(),
|
||||
}
|
||||
|
||||
// 執行 ClearNoMoreDataFlag
|
||||
got, err := logic.ClearNoMoreDataFlag(tt.input)
|
||||
|
||||
// 驗證結果
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantResp, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
package timelineservicelogic
|
||||
|
||||
import (
|
||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||
"app-cloudep-tweeting-service/internal/domain/repository"
|
||||
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 TestFetchTimelineLogic_FetchTimeline(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// 初始化 mock 依賴
|
||||
mockTimelineRepo := mockRepo.NewMockTimelineRepository(ctrl)
|
||||
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||
|
||||
// 初始化服務上下文
|
||||
svcCtx := &svc.ServiceContext{
|
||||
TimelineRepo: mockTimelineRepo,
|
||||
Validate: mockValidate,
|
||||
}
|
||||
|
||||
// 測試數據集
|
||||
tests := []struct {
|
||||
name string
|
||||
input *tweeting.GetTimelineReq
|
||||
prepare func()
|
||||
expectErr bool
|
||||
wantResp *tweeting.FetchTimelineResponse
|
||||
}{
|
||||
{
|
||||
name: "成功獲取動態時報",
|
||||
input: &tweeting.GetTimelineReq{
|
||||
Uid: "user123",
|
||||
PageSize: 10,
|
||||
PageIndex: 1,
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬成功獲取動態時報
|
||||
mockTimelineRepo.EXPECT().FetchTimeline(gomock.Any(), repository.FetchTimelineRequest{
|
||||
UID: "user123",
|
||||
PageSize: 10,
|
||||
PageIndex: 1,
|
||||
}).Return(repository.FetchTimelineResponse{
|
||||
Items: []repository.TimelineItem{
|
||||
{PostID: "post1", Score: 100},
|
||||
{PostID: "post2", Score: 200},
|
||||
},
|
||||
Page: tweeting.Pager{
|
||||
Total: 2,
|
||||
Size: 10,
|
||||
Index: 1,
|
||||
},
|
||||
}, nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.FetchTimelineResponse{
|
||||
Posts: []*tweeting.FetchTimelineItem{
|
||||
{PostId: "post1", Score: 100},
|
||||
{PostId: "post2", Score: 200},
|
||||
},
|
||||
Page: &tweeting.Pager{
|
||||
Total: 2,
|
||||
Index: 1,
|
||||
Size: 10,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "驗證失敗",
|
||||
input: &tweeting.GetTimelineReq{
|
||||
Uid: "",
|
||||
PageSize: 10,
|
||||
PageIndex: 1,
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證失敗
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
{
|
||||
name: "獲取動態時報失敗",
|
||||
input: &tweeting.GetTimelineReq{
|
||||
Uid: "user123",
|
||||
PageSize: 10,
|
||||
PageIndex: 1,
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬獲取動態時報失敗
|
||||
mockTimelineRepo.EXPECT().FetchTimeline(gomock.Any(), repository.FetchTimelineRequest{
|
||||
UID: "user123",
|
||||
PageSize: 10,
|
||||
PageIndex: 1,
|
||||
}).Return(repository.FetchTimelineResponse{}, errors.New("repository error")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 設置測試環境
|
||||
tt.prepare()
|
||||
|
||||
// 初始化 FetchTimelineLogic
|
||||
logic := FetchTimelineLogic{
|
||||
svcCtx: svcCtx,
|
||||
ctx: context.TODO(),
|
||||
}
|
||||
|
||||
// 執行 FetchTimeline
|
||||
got, err := logic.FetchTimeline(tt.input)
|
||||
|
||||
// 驗證結果
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantResp, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package timelineservicelogic
|
||||
|
||||
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 TestHasNoMoreDataLogic_HasNoMoreData(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// 初始化 mock 依賴
|
||||
mockTimelineRepo := mockRepo.NewMockTimelineRepository(ctrl)
|
||||
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||
|
||||
// 初始化服務上下文
|
||||
svcCtx := &svc.ServiceContext{
|
||||
TimelineRepo: mockTimelineRepo,
|
||||
Validate: mockValidate,
|
||||
}
|
||||
|
||||
// 測試數據集
|
||||
tests := []struct {
|
||||
name string
|
||||
input *tweeting.DoNoMoreDataReq
|
||||
prepare func()
|
||||
expectErr bool
|
||||
wantResp *tweeting.HasNoMoreDataResp
|
||||
}{
|
||||
{
|
||||
name: "成功檢查時間線是否完整",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬成功檢查時間線狀態
|
||||
mockTimelineRepo.EXPECT().HasNoMoreData(gomock.Any(), "user123").Return(true, nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.HasNoMoreDataResp{
|
||||
Status: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "驗證失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證失敗
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
{
|
||||
name: "檢查時間線狀態失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬檢查時間線狀態失敗
|
||||
mockTimelineRepo.EXPECT().HasNoMoreData(gomock.Any(), "user123").Return(false, errors.New("repository error")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// 設置測試環境
|
||||
tt.prepare()
|
||||
|
||||
// 初始化 HasNoMoreDataLogic
|
||||
logic := HasNoMoreDataLogic{
|
||||
svcCtx: svcCtx,
|
||||
ctx: context.TODO(),
|
||||
}
|
||||
|
||||
// 執行 HasNoMoreData
|
||||
got, err := logic.HasNoMoreData(tt.input)
|
||||
|
||||
// 驗證結果
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantResp, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package timelineservicelogic
|
||||
|
||||
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 TestSetNoMoreDataFlagLogic_SetNoMoreDataFlag(t *testing.T) {
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
// 初始化 mock 依賴
|
||||
mockTimelineRepo := mockRepo.NewMockTimelineRepository(ctrl)
|
||||
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||
|
||||
// 初始化服務上下文
|
||||
svcCtx := &svc.ServiceContext{
|
||||
TimelineRepo: mockTimelineRepo,
|
||||
Validate: mockValidate,
|
||||
}
|
||||
|
||||
// 測試數據集
|
||||
tests := []struct {
|
||||
name string
|
||||
input *tweeting.DoNoMoreDataReq
|
||||
prepare func()
|
||||
expectErr bool
|
||||
wantResp *tweeting.OKResp
|
||||
}{
|
||||
{
|
||||
name: "成功標記時間線已完整",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬成功標記時間線狀態
|
||||
mockTimelineRepo.EXPECT().SetNoMoreDataFlag(gomock.Any(), "user123").Return(nil).Times(1)
|
||||
},
|
||||
expectErr: false,
|
||||
wantResp: &tweeting.OKResp{},
|
||||
},
|
||||
{
|
||||
name: "驗證失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證失敗
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||
},
|
||||
expectErr: true,
|
||||
wantResp: nil,
|
||||
},
|
||||
{
|
||||
name: "標記時間線狀態失敗",
|
||||
input: &tweeting.DoNoMoreDataReq{
|
||||
Uid: "user123",
|
||||
},
|
||||
prepare: func() {
|
||||
// 模擬驗證通過
|
||||
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||
// 模擬標記時間線狀態失敗
|
||||
mockTimelineRepo.EXPECT().SetNoMoreDataFlag(gomock.Any(), "user123").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()
|
||||
|
||||
// 初始化 SetNoMoreDataFlagLogic
|
||||
logic := SetNoMoreDataFlagLogic{
|
||||
svcCtx: svcCtx,
|
||||
ctx: context.TODO(),
|
||||
}
|
||||
|
||||
// 執行 SetNoMoreDataFlag
|
||||
got, err := logic.SetNoMoreDataFlag(tt.input)
|
||||
|
||||
// 驗證結果
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, got)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.wantResp, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue