feature/order_base #1
			
				
			
		
		
		
	|  | @ -7,10 +7,11 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/svc" | 	"app-cloudep-order-server/internal/svc" | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.mongodb.org/mongo-driver/mongo" | 	"go.mongodb.org/mongo-driver/mongo" | ||||||
| 	"go.uber.org/mock/gomock" | 	"go.uber.org/mock/gomock" | ||||||
| 	"testing" |  | ||||||
| 
 | 
 | ||||||
| 	mockmodel "app-cloudep-order-server/internal/mock/model" | 	mockmodel "app-cloudep-order-server/internal/mock/model" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -5,7 +5,6 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/domain" | 	"app-cloudep-order-server/internal/domain" | ||||||
| 	model "app-cloudep-order-server/internal/model/mongo" | 	model "app-cloudep-order-server/internal/model/mongo" | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" |  | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	ers "code.30cm.net/digimon/library-go/errs" | 	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) | 	req, err := buildCreateOrderReq(in) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		// 錯誤代碼 06-011-00
 | 		// 錯誤代碼 06-011-00
 | ||||||
| 		fmt.Println("1", err) |  | ||||||
| 		return nil, ers.InvalidFormat(err.Error()) | 		return nil, ers.InvalidFormat(err.Error()) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	fmt.Println(req) |  | ||||||
| 	// 驗證資料
 | 	// 驗證資料
 | ||||||
| 	if err := l.svcCtx.Validate.ValidateAll(req); err != nil { | 	if err := l.svcCtx.Validate.ValidateAll(req); err != nil { | ||||||
| 		// 錯誤代碼 06-011-00
 | 		// 錯誤代碼 06-011-00
 | ||||||
| 		fmt.Println("2", err) |  | ||||||
| 		return nil, ers.InvalidFormat(err.Error()) | 		return nil, ers.InvalidFormat(err.Error()) | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(req) | 
 | ||||||
| 	now := time.Now().UTC().UnixNano() | 	now := time.Now().UTC().UnixNano() | ||||||
| 	// 插入資料庫
 | 	// 插入資料庫
 | ||||||
| 	o := &model.Order{ | 	o := &model.Order{ | ||||||
|  |  | ||||||
|  | @ -7,11 +7,12 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/svc" | 	"app-cloudep-order-server/internal/svc" | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
| 	"github.com/shopspring/decimal" | 	"github.com/shopspring/decimal" | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/mock/gomock" | 	"go.uber.org/mock/gomock" | ||||||
| 	"testing" |  | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestDecimalPtrFromString(t *testing.T) { | func TestDecimalPtrFromString(t *testing.T) { | ||||||
|  |  | ||||||
|  | @ -7,9 +7,10 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/svc" | 	"app-cloudep-order-server/internal/svc" | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/mock/gomock" | 	"go.uber.org/mock/gomock" | ||||||
| 	"testing" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestDeleteOrder(t *testing.T) { | func TestDeleteOrder(t *testing.T) { | ||||||
|  |  | ||||||
|  | @ -3,7 +3,6 @@ package orderservicelogic | ||||||
| import ( | import ( | ||||||
| 	"app-cloudep-order-server/gen_result/pb/order" | 	"app-cloudep-order-server/gen_result/pb/order" | ||||||
| 	"context" | 	"context" | ||||||
| 	"fmt" |  | ||||||
| 
 | 
 | ||||||
| 	ers "code.30cm.net/digimon/library-go/errs" | 	ers "code.30cm.net/digimon/library-go/errs" | ||||||
| 	"github.com/shopspring/decimal" | 	"github.com/shopspring/decimal" | ||||||
|  | @ -46,7 +45,6 @@ func (l *GetOrderLogic) GetOrder(in *order.GetOrderReq) (*order.GetOrderResp, er | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	fmt.Println(o) |  | ||||||
| 
 | 
 | ||||||
| 	return &order.GetOrderResp{ | 	return &order.GetOrderResp{ | ||||||
| 		UpdateTime:  o.UpdateTime, | 		UpdateTime:  o.UpdateTime, | ||||||
|  | @ -102,6 +100,7 @@ func nilString(s *string) *string { | ||||||
| 	if s == nil { | 	if s == nil { | ||||||
| 		return nil | 		return nil | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
| 	return s | 	return s | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -112,3 +111,10 @@ func nilInt64(i *int64) *int64 { | ||||||
| 
 | 
 | ||||||
| 	return i | 	return i | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // Helper functions
 | ||||||
|  | //
 | ||||||
|  | //nolint:unused
 | ||||||
|  | func ptrString(s string) *string { | ||||||
|  | 	return &s | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -8,10 +8,11 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/svc" | 	"app-cloudep-order-server/internal/svc" | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
| 	"github.com/shopspring/decimal" | 	"github.com/shopspring/decimal" | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/mock/gomock" | 	"go.uber.org/mock/gomock" | ||||||
| 	"testing" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestGetOrder(t *testing.T) { | func TestGetOrder(t *testing.T) { | ||||||
|  |  | ||||||
|  | @ -160,8 +160,3 @@ func ConvertOrdersToGetOrderResp(orders []model.Order) []*order.GetOrderResp { | ||||||
| 
 | 
 | ||||||
| 	return res | 	return res | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // Helper functions
 |  | ||||||
| func ptrString(s string) *string { |  | ||||||
| 	return &s |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -8,10 +8,11 @@ import ( | ||||||
| 	"app-cloudep-order-server/internal/svc" | 	"app-cloudep-order-server/internal/svc" | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
| 	"github.com/shopspring/decimal" | 	"github.com/shopspring/decimal" | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
| 	"go.uber.org/mock/gomock" | 	"go.uber.org/mock/gomock" | ||||||
| 	"testing" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestConvertOrdersToGetOrderResp(t *testing.T) { | func TestConvertOrdersToGetOrderResp(t *testing.T) { | ||||||
|  |  | ||||||
|  | @ -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) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -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) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | @ -34,7 +34,6 @@ func (s *OrderServiceServer) CancelOrder(ctx context.Context, in *order.CancelOr | ||||||
| 	return l.CancelOrder(in) | 	return l.CancelOrder(in) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| // ModifyOrderStatus 修改訂單狀態
 | // ModifyOrderStatus 修改訂單狀態
 | ||||||
| func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *order.ModifyOrderStatusReq) (*order.OKResp, error) { | func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *order.ModifyOrderStatusReq) (*order.OKResp, error) { | ||||||
| 	l := orderservicelogic.NewModifyOrderStatusLogic(ctx, s.svcCtx) | 	l := orderservicelogic.NewModifyOrderStatusLogic(ctx, s.svcCtx) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue