remove test fmt

This commit is contained in:
daniel.w 2024-10-22 17:20:16 +08:00
parent 352f11a792
commit f5a043ab79
11 changed files with 196 additions and 19 deletions

View File

@ -7,10 +7,11 @@ import (
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/mock/gomock"
"testing"
mockmodel "app-cloudep-order-server/internal/mock/model"
)

View File

@ -5,7 +5,6 @@ import (
"app-cloudep-order-server/internal/domain"
model "app-cloudep-order-server/internal/model/mongo"
"context"
"fmt"
"time"
ers "code.30cm.net/digimon/library-go/errs"
@ -72,18 +71,15 @@ func (l *CreateOrderLogic) CreateOrder(in *order.CreateOrderReq) (*order.OKResp,
req, err := buildCreateOrderReq(in)
if err != nil {
// 錯誤代碼 06-011-00
fmt.Println("1", err)
return nil, ers.InvalidFormat(err.Error())
}
fmt.Println(req)
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(req); err != nil {
// 錯誤代碼 06-011-00
fmt.Println("2", err)
return nil, ers.InvalidFormat(err.Error())
}
fmt.Println(req)
now := time.Now().UTC().UnixNano()
// 插入資料庫
o := &model.Order{

View File

@ -7,11 +7,12 @@ import (
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"time"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"testing"
"time"
)
func TestDecimalPtrFromString(t *testing.T) {

View File

@ -7,9 +7,10 @@ import (
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"testing"
)
func TestDeleteOrder(t *testing.T) {

View File

@ -3,7 +3,6 @@ package orderservicelogic
import (
"app-cloudep-order-server/gen_result/pb/order"
"context"
"fmt"
ers "code.30cm.net/digimon/library-go/errs"
"github.com/shopspring/decimal"
@ -46,7 +45,6 @@ func (l *GetOrderLogic) GetOrder(in *order.GetOrderReq) (*order.GetOrderResp, er
if err != nil {
return nil, err
}
fmt.Println(o)
return &order.GetOrderResp{
UpdateTime: o.UpdateTime,
@ -102,6 +100,7 @@ func nilString(s *string) *string {
if s == nil {
return nil
}
return s
}
@ -112,3 +111,10 @@ func nilInt64(i *int64) *int64 {
return i
}
// Helper functions
//
//nolint:unused
func ptrString(s string) *string {
return &s
}

View File

@ -8,10 +8,11 @@ import (
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"testing"
)
func TestGetOrder(t *testing.T) {

View File

@ -160,8 +160,3 @@ func ConvertOrdersToGetOrderResp(orders []model.Order) []*order.GetOrderResp {
return res
}
// Helper functions
func ptrString(s string) *string {
return &s
}

View File

@ -8,10 +8,11 @@ import (
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"testing"
)
func TestConvertOrdersToGetOrderResp(t *testing.T) {

View File

@ -0,0 +1,102 @@
package orderservicelogic
import (
"app-cloudep-order-server/gen_result/pb/order"
mocksvc "app-cloudep-order-server/internal/mock/lib"
mockmodel "app-cloudep-order-server/internal/mock/model"
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func TestModifyOrderStatus(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// 初始化 mock 依賴
mockOrderModel := mockmodel.NewMockOrderModel(ctrl)
mockValidate := mocksvc.NewMockValidate(ctrl)
// 初始化服務上下文
svcCtx := &svc.ServiceContext{
OrderModel: mockOrderModel,
Validate: mockValidate,
}
// 測試數據集
tests := []struct {
name string
input *order.ModifyOrderStatusReq
prepare func()
expectErr bool
expectedRes *order.OKResp
}{
{
name: "成功修改訂單狀態",
input: &order.ModifyOrderStatusReq{
BusinessId: "B123",
Status: 3,
},
prepare: func() {
// 模擬驗證成功
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
// 模擬狀態更新成功
mockOrderModel.EXPECT().UpdateStatus(gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
},
expectErr: false,
expectedRes: &order.OKResp{},
},
{
name: "驗證失敗",
input: &order.ModifyOrderStatusReq{
BusinessId: "B123",
Status: 12, // 錯誤的狀態值
},
prepare: func() {
// 模擬驗證失敗
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(errors.New("invalid status")).Times(1)
},
expectErr: true,
},
{
name: "修改訂單狀態失敗",
input: &order.ModifyOrderStatusReq{
BusinessId: "B123",
Status: 3,
},
prepare: func() {
// 模擬驗證成功
mockValidate.EXPECT().ValidateAll(gomock.Any()).Return(nil).Times(1)
// 模擬狀態更新失敗
mockOrderModel.EXPECT().UpdateStatus(gomock.Any(), gomock.Any()).Return(nil, errors.New("update failed")).Times(1)
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 準備測試環境
tt.prepare()
// 初始化 ModifyOrderStatusLogic
logic := NewModifyOrderStatusLogic(context.TODO(), svcCtx)
// 執行 ModifyOrderStatus
resp, err := logic.ModifyOrderStatus(tt.input)
// 驗證結果
if tt.expectErr {
assert.Error(t, err)
assert.Nil(t, resp)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedRes, resp)
}
})
}
}

View File

@ -0,0 +1,74 @@
package orderservicelogic
import (
"app-cloudep-order-server/gen_result/pb/order"
mockmodel "app-cloudep-order-server/internal/mock/model"
"app-cloudep-order-server/internal/svc"
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func TestOrderStatusTimeout(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
// 初始化 mock 依賴
mockOrderModel := mockmodel.NewMockOrderModel(ctrl)
// 初始化服務上下文
svcCtx := &svc.ServiceContext{
OrderModel: mockOrderModel,
}
// 測試數據集
tests := []struct {
name string
prepare func()
expectErr bool
expectedRes *order.OKResp
}{
{
name: "成功更新訂單狀態",
prepare: func() {
// 模擬訂單狀態超時更新成功
mockOrderModel.EXPECT().UpdateTimeoutOrder(gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
},
expectErr: false,
expectedRes: &order.OKResp{},
},
{
name: "更新訂單狀態失敗",
prepare: func() {
// 模擬訂單狀態超時更新失敗
mockOrderModel.EXPECT().UpdateTimeoutOrder(gomock.Any(), gomock.Any()).Return(nil, errors.New("update failed")).Times(1)
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 準備測試環境
tt.prepare()
// 初始化 OrderStatusTimeoutLogic
logic := NewOrderStatusTimeoutLogic(context.TODO(), svcCtx)
// 執行 OrderStatusTimeout
resp, err := logic.OrderStatusTimeout(&order.OrderStatusTimeoutReq{})
// 驗證結果
if tt.expectErr {
assert.Error(t, err)
assert.Nil(t, resp)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedRes, resp)
}
})
}
}

View File

@ -34,7 +34,6 @@ func (s *OrderServiceServer) CancelOrder(ctx context.Context, in *order.CancelOr
return l.CancelOrder(in)
}
// ModifyOrderStatus 修改訂單狀態
func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *order.ModifyOrderStatusReq) (*order.OKResp, error) {
l := orderservicelogic.NewModifyOrderStatusLogic(ctx, s.svcCtx)