feature/post_v2 #2
2
Makefile
2
Makefile
|
@ -60,6 +60,8 @@ gen-mongo-model: # 建立 rpc 資料庫
|
||||||
mock-gen: # 建立 mock 資料
|
mock-gen: # 建立 mock 資料
|
||||||
mockgen -source=./internal/model/mongo/post_model_gen.go -destination=./internal/mock/model/post_model_gen.go -package=mock
|
mockgen -source=./internal/model/mongo/post_model_gen.go -destination=./internal/mock/model/post_model_gen.go -package=mock
|
||||||
mockgen -source=./internal/model/mongo/post_model.go -destination=./internal/mock/model/post_model.go -package=mock
|
mockgen -source=./internal/model/mongo/post_model.go -destination=./internal/mock/model/post_model.go -package=mock
|
||||||
|
mockgen -source=./internal/model/mongo/comment_model_gen.go -destination=./internal/mock/model/comment_model_gen.go -package=mock
|
||||||
|
mockgen -source=./internal/model/mongo/comment_model.go -destination=./internal/mock/model/comment_model.go -package=mock
|
||||||
@echo "Generate mock files successfully"
|
@echo "Generate mock files successfully"
|
||||||
|
|
||||||
.PHONY: migrate-database
|
.PHONY: migrate-database
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
ers "code.30cm.net/digimon/library-go/errs"
|
ers "code.30cm.net/digimon/library-go/errs"
|
||||||
"code.30cm.net/digimon/library-go/errs/code"
|
"code.30cm.net/digimon/library-go/errs/code"
|
||||||
"fmt"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorCode uint32
|
type ErrorCode uint32
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
package commentservicelogic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"go.uber.org/mock/gomock"
|
||||||
|
|
||||||
|
mockmodel "app-cloudep-tweeting-service/internal/mock/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDeleteComment(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
// 初始化 mock 依賴
|
||||||
|
mockCommentModel := mockmodel.NewMockCommentModel(ctrl)
|
||||||
|
|
||||||
|
// 初始化服務上下文
|
||||||
|
svcCtx := &svc.ServiceContext{
|
||||||
|
CommentModel: mockCommentModel,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據
|
||||||
|
commentReq := &tweeting.DeleteCommentReq{
|
||||||
|
CommentId: []string{"12345", "67890"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據集
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *tweeting.DeleteCommentReq
|
||||||
|
prepare func()
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "成功刪除評論",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 DeleteMany 成功
|
||||||
|
mockCommentModel.EXPECT().DeleteMany(gomock.Any(), "12345", "67890").Return(int64(2), nil).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "刪除評論失敗",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 DeleteMany 失敗
|
||||||
|
mockCommentModel.EXPECT().DeleteMany(gomock.Any(), "12345", "67890").Return(int64(0), errors.New("delete failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行測試
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 設置測試環境
|
||||||
|
tt.prepare()
|
||||||
|
|
||||||
|
// 初始化 DeleteCommentLogic
|
||||||
|
logic := DeleteCommentLogic{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
ctx: context.TODO(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行 DeleteComment
|
||||||
|
resp, err := logic.DeleteComment(tt.input)
|
||||||
|
|
||||||
|
// 驗證結果
|
||||||
|
if tt.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, resp)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,9 +3,10 @@ package commentservicelogic
|
||||||
import (
|
import (
|
||||||
"app-cloudep-tweeting-service/internal/domain"
|
"app-cloudep-tweeting-service/internal/domain"
|
||||||
model "app-cloudep-tweeting-service/internal/model/mongo"
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
ers "code.30cm.net/digimon/library-go/errs"
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
ers "code.30cm.net/digimon/library-go/errs"
|
||||||
|
|
||||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
"app-cloudep-tweeting-service/internal/svc"
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,119 @@
|
||||||
|
package commentservicelogic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.uber.org/mock/gomock"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
mocklib "app-cloudep-tweeting-service/internal/mock/lib"
|
||||||
|
mockmodel "app-cloudep-tweeting-service/internal/mock/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetComments(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
// 初始化 mock 依賴
|
||||||
|
mockCommentModel := mockmodel.NewMockCommentModel(ctrl)
|
||||||
|
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||||
|
|
||||||
|
// 初始化服務上下文
|
||||||
|
svcCtx := &svc.ServiceContext{
|
||||||
|
CommentModel: mockCommentModel,
|
||||||
|
Validate: mockValidate,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據
|
||||||
|
getCommentsReq := &tweeting.GetCommentsReq{
|
||||||
|
PostId: "12345",
|
||||||
|
PageSize: 10,
|
||||||
|
PageIndex: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
mockComments := []*model.Comment{
|
||||||
|
{
|
||||||
|
ID: primitive.NewObjectID(),
|
||||||
|
PostID: "12345",
|
||||||
|
UID: "54321",
|
||||||
|
Content: "This is a comment",
|
||||||
|
LikeCount: 10,
|
||||||
|
DisLikeCount: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: primitive.NewObjectID(),
|
||||||
|
PostID: "12345",
|
||||||
|
UID: "67890",
|
||||||
|
Content: "This is another comment",
|
||||||
|
LikeCount: 5,
|
||||||
|
DisLikeCount: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據集
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *tweeting.GetCommentsReq
|
||||||
|
prepare func()
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "成功查詢評論",
|
||||||
|
input: getCommentsReq,
|
||||||
|
prepare: func() {
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
mockCommentModel.EXPECT().Find(gomock.Any(), gomock.Any()).Return(mockComments, int64(len(mockComments)), nil).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "查詢評論失敗",
|
||||||
|
input: getCommentsReq,
|
||||||
|
prepare: func() {
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
mockCommentModel.EXPECT().Find(gomock.Any(), gomock.Any()).Return(nil, int64(0), errors.New("find failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "驗證失敗",
|
||||||
|
input: getCommentsReq,
|
||||||
|
prepare: func() {
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行測試
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 設置測試環境
|
||||||
|
tt.prepare()
|
||||||
|
|
||||||
|
// 初始化 GetCommentsLogic
|
||||||
|
logic := GetCommentsLogic{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
ctx: context.TODO(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行 GetComments
|
||||||
|
resp, err := logic.GetComments(tt.input)
|
||||||
|
|
||||||
|
// 驗證結果
|
||||||
|
if tt.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, resp)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
assert.Len(t, resp.Comments, len(mockComments))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,10 +5,11 @@ import (
|
||||||
"app-cloudep-tweeting-service/internal/domain"
|
"app-cloudep-tweeting-service/internal/domain"
|
||||||
model "app-cloudep-tweeting-service/internal/model/mongo"
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
"app-cloudep-tweeting-service/internal/svc"
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
ers "code.30cm.net/digimon/library-go/errs"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
ers "code.30cm.net/digimon/library-go/errs"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1 +1,123 @@
|
||||||
package commentservicelogic
|
package commentservicelogic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.uber.org/mock/gomock"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
mocklib "app-cloudep-tweeting-service/internal/mock/lib"
|
||||||
|
mockmodel "app-cloudep-tweeting-service/internal/mock/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewComment(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
// 初始化 mock 依賴
|
||||||
|
mockPostModel := mockmodel.NewMockPostModel(ctrl)
|
||||||
|
mockCommentModel := mockmodel.NewMockCommentModel(ctrl)
|
||||||
|
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||||
|
|
||||||
|
// 初始化服務上下文
|
||||||
|
svcCtx := &svc.ServiceContext{
|
||||||
|
PostModel: mockPostModel,
|
||||||
|
CommentModel: mockCommentModel,
|
||||||
|
Validate: mockValidate,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據
|
||||||
|
postID := primitive.NewObjectID().Hex()
|
||||||
|
commentReq := &tweeting.CommentPostReq{
|
||||||
|
Uid: "12345",
|
||||||
|
Content: "This is a comment",
|
||||||
|
PostId: postID,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據集
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *tweeting.CommentPostReq
|
||||||
|
prepare func()
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "成功發表評論",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
// 模擬 FindOne 成功找到文章
|
||||||
|
mockPostModel.EXPECT().FindOne(gomock.Any(), postID).Return(&model.Post{}, nil).Times(1)
|
||||||
|
// 模擬 Insert 成功
|
||||||
|
mockCommentModel.EXPECT().Insert(gomock.Any(), gomock.Any()).Return(nil).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "驗證失敗",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 失敗
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "文章不存在",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
// 模擬 FindOne 找不到文章
|
||||||
|
mockPostModel.EXPECT().FindOne(gomock.Any(), postID).Return(nil, model.ErrNotFound).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "插入評論失敗",
|
||||||
|
input: commentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
// 模擬 FindOne 成功找到文章
|
||||||
|
mockPostModel.EXPECT().FindOne(gomock.Any(), postID).Return(&model.Post{}, nil).Times(1)
|
||||||
|
// 模擬 Insert 失敗
|
||||||
|
mockCommentModel.EXPECT().Insert(gomock.Any(), gomock.Any()).Return(errors.New("insert failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行測試
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 設置測試環境
|
||||||
|
tt.prepare()
|
||||||
|
|
||||||
|
// 初始化 NewCommentLogic
|
||||||
|
logic := NewCommentLogic{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
ctx: context.TODO(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行 NewComment
|
||||||
|
resp, err := logic.NewComment(tt.input)
|
||||||
|
|
||||||
|
// 驗證結果
|
||||||
|
if tt.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, resp)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
assert.NotEmpty(t, resp.CommentId)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,8 +5,9 @@ import (
|
||||||
"app-cloudep-tweeting-service/internal/domain"
|
"app-cloudep-tweeting-service/internal/domain"
|
||||||
model "app-cloudep-tweeting-service/internal/model/mongo"
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
"app-cloudep-tweeting-service/internal/svc"
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
ers "code.30cm.net/digimon/library-go/errs"
|
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
ers "code.30cm.net/digimon/library-go/errs"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
package commentservicelogic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
|
mocklib "app-cloudep-tweeting-service/internal/mock/lib"
|
||||||
|
mockmodel "app-cloudep-tweeting-service/internal/mock/model"
|
||||||
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.uber.org/mock/gomock"
|
||||||
|
"google.golang.org/protobuf/proto"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUpdateComment(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
// 初始化 mock 依賴
|
||||||
|
mockCommentModel := mockmodel.NewMockCommentModel(ctrl)
|
||||||
|
mockValidate := mocklib.NewMockValidate(ctrl)
|
||||||
|
|
||||||
|
// 初始化服務上下文
|
||||||
|
svcCtx := &svc.ServiceContext{
|
||||||
|
CommentModel: mockCommentModel,
|
||||||
|
Validate: mockValidate,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據
|
||||||
|
commentID := primitive.NewObjectID().Hex()
|
||||||
|
updateCommentReq := &tweeting.UpdateCommentReq{
|
||||||
|
CommentId: commentID,
|
||||||
|
Content: "Updated content",
|
||||||
|
LikeCount: proto.Int64(5),
|
||||||
|
DislikeCount: proto.Int64(2),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 測試數據集
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input *tweeting.UpdateCommentReq
|
||||||
|
prepare func()
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "成功更新評論",
|
||||||
|
input: updateCommentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
// 模擬 UpdateOptional 成功
|
||||||
|
mockCommentModel.EXPECT().UpdateOptional(gomock.Any(), gomock.Any()).Return(&mongo.UpdateResult{
|
||||||
|
ModifiedCount: 1,
|
||||||
|
}, nil).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "驗證失敗",
|
||||||
|
input: updateCommentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 失敗
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("validation failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "更新評論失敗",
|
||||||
|
input: updateCommentReq,
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
// 模擬 UpdateOptional 失敗
|
||||||
|
mockCommentModel.EXPECT().UpdateOptional(gomock.Any(), gomock.Any()).Return(
|
||||||
|
&mongo.UpdateResult{
|
||||||
|
ModifiedCount: 0,
|
||||||
|
}, errors.New("update failed")).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "無效的評論ID",
|
||||||
|
input: &tweeting.UpdateCommentReq{
|
||||||
|
CommentId: "invalid_id",
|
||||||
|
Content: "Updated content",
|
||||||
|
},
|
||||||
|
prepare: func() {
|
||||||
|
// 模擬 Validate 成功
|
||||||
|
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
|
||||||
|
},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行測試
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// 設置測試環境
|
||||||
|
tt.prepare()
|
||||||
|
|
||||||
|
// 初始化 UpdateCommentLogic
|
||||||
|
logic := UpdateCommentLogic{
|
||||||
|
svcCtx: svcCtx,
|
||||||
|
ctx: context.TODO(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 執行 UpdateComment
|
||||||
|
resp, err := logic.UpdateComment(tt.input)
|
||||||
|
|
||||||
|
// 驗證結果
|
||||||
|
if tt.expectErr {
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, resp)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,152 @@
|
||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: ./internal/model/mongo/comment_model.go
|
||||||
|
//
|
||||||
|
// Generated by this command:
|
||||||
|
//
|
||||||
|
// mockgen -source=./internal/model/mongo/comment_model.go -destination=./internal/mock/model/comment_model.go -package=mock
|
||||||
|
//
|
||||||
|
|
||||||
|
// Package mock is a generated GoMock package.
|
||||||
|
package mock
|
||||||
|
|
||||||
|
import (
|
||||||
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
mongo "go.mongodb.org/mongo-driver/mongo"
|
||||||
|
gomock "go.uber.org/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockCommentModel is a mock of CommentModel interface.
|
||||||
|
type MockCommentModel struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockCommentModelMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockCommentModelMockRecorder is the mock recorder for MockCommentModel.
|
||||||
|
type MockCommentModelMockRecorder struct {
|
||||||
|
mock *MockCommentModel
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockCommentModel creates a new mock instance.
|
||||||
|
func NewMockCommentModel(ctrl *gomock.Controller) *MockCommentModel {
|
||||||
|
mock := &MockCommentModel{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockCommentModelMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockCommentModel) EXPECT() *MockCommentModelMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete mocks base method.
|
||||||
|
func (m *MockCommentModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Delete", ctx, id)
|
||||||
|
ret0, _ := ret[0].(int64)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete indicates an expected call of Delete.
|
||||||
|
func (mr *MockCommentModelMockRecorder) Delete(ctx, id any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockCommentModel)(nil).Delete), ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteMany mocks base method.
|
||||||
|
func (m *MockCommentModel) DeleteMany(ctx context.Context, id ...string) (int64, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
varargs := []any{ctx}
|
||||||
|
for _, a := range id {
|
||||||
|
varargs = append(varargs, a)
|
||||||
|
}
|
||||||
|
ret := m.ctrl.Call(m, "DeleteMany", varargs...)
|
||||||
|
ret0, _ := ret[0].(int64)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteMany indicates an expected call of DeleteMany.
|
||||||
|
func (mr *MockCommentModelMockRecorder) DeleteMany(ctx any, id ...any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
varargs := append([]any{ctx}, id...)
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteMany", reflect.TypeOf((*MockCommentModel)(nil).DeleteMany), varargs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find mocks base method.
|
||||||
|
func (m *MockCommentModel) Find(ctx context.Context, param *model.QueryCommentModelReq) ([]*model.Comment, int64, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Find", ctx, param)
|
||||||
|
ret0, _ := ret[0].([]*model.Comment)
|
||||||
|
ret1, _ := ret[1].(int64)
|
||||||
|
ret2, _ := ret[2].(error)
|
||||||
|
return ret0, ret1, ret2
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find indicates an expected call of Find.
|
||||||
|
func (mr *MockCommentModelMockRecorder) Find(ctx, param any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Find", reflect.TypeOf((*MockCommentModel)(nil).Find), ctx, param)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOne mocks base method.
|
||||||
|
func (m *MockCommentModel) FindOne(ctx context.Context, id string) (*model.Comment, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "FindOne", ctx, id)
|
||||||
|
ret0, _ := ret[0].(*model.Comment)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOne indicates an expected call of FindOne.
|
||||||
|
func (mr *MockCommentModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockCommentModel)(nil).FindOne), ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert mocks base method.
|
||||||
|
func (m *MockCommentModel) Insert(ctx context.Context, data *model.Comment) error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Insert", ctx, data)
|
||||||
|
ret0, _ := ret[0].(error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert indicates an expected call of Insert.
|
||||||
|
func (mr *MockCommentModelMockRecorder) Insert(ctx, data any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockCommentModel)(nil).Insert), ctx, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update mocks base method.
|
||||||
|
func (m *MockCommentModel) Update(ctx context.Context, data *model.Comment) (*mongo.UpdateResult, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Update", ctx, data)
|
||||||
|
ret0, _ := ret[0].(*mongo.UpdateResult)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update indicates an expected call of Update.
|
||||||
|
func (mr *MockCommentModelMockRecorder) Update(ctx, data any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockCommentModel)(nil).Update), ctx, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateOptional mocks base method.
|
||||||
|
func (m *MockCommentModel) UpdateOptional(ctx context.Context, data *model.Comment) (*mongo.UpdateResult, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "UpdateOptional", ctx, data)
|
||||||
|
ret0, _ := ret[0].(*mongo.UpdateResult)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateOptional indicates an expected call of UpdateOptional.
|
||||||
|
func (mr *MockCommentModelMockRecorder) UpdateOptional(ctx, data any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateOptional", reflect.TypeOf((*MockCommentModel)(nil).UpdateOptional), ctx, data)
|
||||||
|
}
|
|
@ -0,0 +1,101 @@
|
||||||
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
|
// Source: ./internal/model/mongo/comment_model_gen.go
|
||||||
|
//
|
||||||
|
// Generated by this command:
|
||||||
|
//
|
||||||
|
// mockgen -source=./internal/model/mongo/comment_model_gen.go -destination=./internal/mock/model/comment_model_gen.go -package=mock
|
||||||
|
//
|
||||||
|
|
||||||
|
// Package mock is a generated GoMock package.
|
||||||
|
package mock
|
||||||
|
|
||||||
|
import (
|
||||||
|
model "app-cloudep-tweeting-service/internal/model/mongo"
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
mongo "go.mongodb.org/mongo-driver/mongo"
|
||||||
|
gomock "go.uber.org/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockcommentModel is a mock of commentModel interface.
|
||||||
|
type MockcommentModel struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockcommentModelMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockcommentModelMockRecorder is the mock recorder for MockcommentModel.
|
||||||
|
type MockcommentModelMockRecorder struct {
|
||||||
|
mock *MockcommentModel
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockcommentModel creates a new mock instance.
|
||||||
|
func NewMockcommentModel(ctrl *gomock.Controller) *MockcommentModel {
|
||||||
|
mock := &MockcommentModel{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockcommentModelMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||||
|
func (m *MockcommentModel) EXPECT() *MockcommentModelMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete mocks base method.
|
||||||
|
func (m *MockcommentModel) Delete(ctx context.Context, id string) (int64, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Delete", ctx, id)
|
||||||
|
ret0, _ := ret[0].(int64)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete indicates an expected call of Delete.
|
||||||
|
func (mr *MockcommentModelMockRecorder) Delete(ctx, id any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockcommentModel)(nil).Delete), ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOne mocks base method.
|
||||||
|
func (m *MockcommentModel) FindOne(ctx context.Context, id string) (*model.Comment, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "FindOne", ctx, id)
|
||||||
|
ret0, _ := ret[0].(*model.Comment)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindOne indicates an expected call of FindOne.
|
||||||
|
func (mr *MockcommentModelMockRecorder) FindOne(ctx, id any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockcommentModel)(nil).FindOne), ctx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert mocks base method.
|
||||||
|
func (m *MockcommentModel) Insert(ctx context.Context, data *model.Comment) error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Insert", ctx, data)
|
||||||
|
ret0, _ := ret[0].(error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert indicates an expected call of Insert.
|
||||||
|
func (mr *MockcommentModelMockRecorder) Insert(ctx, data any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockcommentModel)(nil).Insert), ctx, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update mocks base method.
|
||||||
|
func (m *MockcommentModel) Update(ctx context.Context, data *model.Comment) (*mongo.UpdateResult, error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Update", ctx, data)
|
||||||
|
ret0, _ := ret[0].(*mongo.UpdateResult)
|
||||||
|
ret1, _ := ret[1].(error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update indicates an expected call of Update.
|
||||||
|
func (mr *MockcommentModelMockRecorder) Update(ctx, data any) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockcommentModel)(nil).Update), ctx, data)
|
||||||
|
}
|
|
@ -2,12 +2,13 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/stores/mon"
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ CommentModel = (*customCommentModel)(nil)
|
var _ CommentModel = (*customCommentModel)(nil)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
"app-cloudep-tweeting-service/internal/logic/commentservice"
|
commentservicelogic "app-cloudep-tweeting-service/internal/logic/commentservice"
|
||||||
"app-cloudep-tweeting-service/internal/svc"
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
||||||
"app-cloudep-tweeting-service/internal/logic/postservice"
|
postservicelogic "app-cloudep-tweeting-service/internal/logic/postservice"
|
||||||
"app-cloudep-tweeting-service/internal/svc"
|
"app-cloudep-tweeting-service/internal/svc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue