diff --git a/internal/logic/orderservice/cancel_order_logic_test.go b/internal/logic/orderservice/cancel_order_logic_test.go index 03f53c0..ce457bd 100644 --- a/internal/logic/orderservice/cancel_order_logic_test.go +++ b/internal/logic/orderservice/cancel_order_logic_test.go @@ -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" ) diff --git a/internal/logic/orderservice/create_order_logic.go b/internal/logic/orderservice/create_order_logic.go index d415f7a..e23a0b6 100644 --- a/internal/logic/orderservice/create_order_logic.go +++ b/internal/logic/orderservice/create_order_logic.go @@ -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{ diff --git a/internal/logic/orderservice/create_order_logic_test.go b/internal/logic/orderservice/create_order_logic_test.go index 9f681fa..92d7558 100644 --- a/internal/logic/orderservice/create_order_logic_test.go +++ b/internal/logic/orderservice/create_order_logic_test.go @@ -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) { diff --git a/internal/logic/orderservice/delete_order_logic_test.go b/internal/logic/orderservice/delete_order_logic_test.go index 0d71124..bbc7c9d 100644 --- a/internal/logic/orderservice/delete_order_logic_test.go +++ b/internal/logic/orderservice/delete_order_logic_test.go @@ -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) { diff --git a/internal/logic/orderservice/get_order_logic.go b/internal/logic/orderservice/get_order_logic.go index 2cf4838..bdbeaaa 100644 --- a/internal/logic/orderservice/get_order_logic.go +++ b/internal/logic/orderservice/get_order_logic.go @@ -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 +} diff --git a/internal/logic/orderservice/get_order_logic_test.go b/internal/logic/orderservice/get_order_logic_test.go index eea74a0..5424e67 100644 --- a/internal/logic/orderservice/get_order_logic_test.go +++ b/internal/logic/orderservice/get_order_logic_test.go @@ -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) { diff --git a/internal/logic/orderservice/list_order_logic.go b/internal/logic/orderservice/list_order_logic.go index 7e2826a..ebe68e7 100644 --- a/internal/logic/orderservice/list_order_logic.go +++ b/internal/logic/orderservice/list_order_logic.go @@ -160,8 +160,3 @@ func ConvertOrdersToGetOrderResp(orders []model.Order) []*order.GetOrderResp { return res } - -// Helper functions -func ptrString(s string) *string { - return &s -} diff --git a/internal/logic/orderservice/list_order_logic_test.go b/internal/logic/orderservice/list_order_logic_test.go index fc59707..5f8a92c 100644 --- a/internal/logic/orderservice/list_order_logic_test.go +++ b/internal/logic/orderservice/list_order_logic_test.go @@ -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) { diff --git a/internal/logic/orderservice/modify_order_status_logic_test.go b/internal/logic/orderservice/modify_order_status_logic_test.go new file mode 100644 index 0000000..069cad1 --- /dev/null +++ b/internal/logic/orderservice/modify_order_status_logic_test.go @@ -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) + } + }) + } +} diff --git a/internal/logic/orderservice/order_status_timeout_logic_test.go b/internal/logic/orderservice/order_status_timeout_logic_test.go new file mode 100644 index 0000000..a1a94da --- /dev/null +++ b/internal/logic/orderservice/order_status_timeout_logic_test.go @@ -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) + } + }) + } +} diff --git a/internal/server/orderservice/order_service_server.go b/internal/server/orderservice/order_service_server.go index 830e1a8..971e351 100644 --- a/internal/server/orderservice/order_service_server.go +++ b/internal/server/orderservice/order_service_server.go @@ -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)