Compare commits
	
		
			3 Commits
		
	
	
		
			04242ab3d8
			...
			f11bd35b74
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f11bd35b74 | |||
| 164c7a4923 | |||
| a5a4f6cbe2 | 
@@ -185,10 +185,27 @@ func (c *Controller) GetPlan(ctx *gin.Context) {
 | 
				
			|||||||
// @Success      200 {object} controller.Response{data=plan.ListPlansResponse} "业务码为200代表成功获取列表"
 | 
					// @Success      200 {object} controller.Response{data=plan.ListPlansResponse} "业务码为200代表成功获取列表"
 | 
				
			||||||
// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)"
 | 
					// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 500)"
 | 
				
			||||||
// @Router       /plans [get]
 | 
					// @Router       /plans [get]
 | 
				
			||||||
func (pc *Controller) ListPlans(c *gin.Context) {
 | 
					func (c *Controller) ListPlans(ctx *gin.Context) {
 | 
				
			||||||
	// 占位符:此处应调用服务层或仓库层来获取计划列表
 | 
						// 1. 调用仓库层获取所有计划
 | 
				
			||||||
	pc.logger.Infof("收到获取计划列表请求 (占位符)")
 | 
						plans, err := c.planRepo.ListBasicPlans()
 | 
				
			||||||
	controller.SendResponse(c, controller.CodeSuccess, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
 | 
						if err != nil {
 | 
				
			||||||
 | 
							c.logger.Errorf("获取计划列表失败: %v", err)
 | 
				
			||||||
 | 
							controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划列表时发生内部错误")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 2. 将模型转换为响应 DTO
 | 
				
			||||||
 | 
						planResponses := make([]PlanResponse, 0, len(plans))
 | 
				
			||||||
 | 
						for _, p := range plans {
 | 
				
			||||||
 | 
							planResponses = append(planResponses, *PlanToResponse(&p))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 3. 构造并发送成功响应
 | 
				
			||||||
 | 
						resp := ListPlansResponse{
 | 
				
			||||||
 | 
							Plans: planResponses,
 | 
				
			||||||
 | 
							Total: len(planResponses),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						controller.SendResponse(ctx, controller.CodeSuccess, "获取计划列表成功", resp)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// UpdatePlan godoc
 | 
					// UpdatePlan godoc
 | 
				
			||||||
@@ -202,10 +219,10 @@ func (pc *Controller) ListPlans(c *gin.Context) {
 | 
				
			|||||||
// @Success      200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表更新成功"
 | 
					// @Success      200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表更新成功"
 | 
				
			||||||
// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
					// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
				
			||||||
// @Router       /plans/{id} [put]
 | 
					// @Router       /plans/{id} [put]
 | 
				
			||||||
func (pc *Controller) UpdatePlan(c *gin.Context) {
 | 
					func (c *Controller) UpdatePlan(ctx *gin.Context) {
 | 
				
			||||||
	// 占位符:此处应调用服务层或仓库层来更新计划
 | 
						// 占位符:此处应调用服务层或仓库层来更新计划
 | 
				
			||||||
	pc.logger.Infof("收到更新计划请求 (占位符)")
 | 
						c.logger.Infof("收到更新计划请求 (占位符)")
 | 
				
			||||||
	controller.SendResponse(c, controller.CodeSuccess, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
 | 
						controller.SendResponse(ctx, controller.CodeSuccess, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// DeletePlan godoc
 | 
					// DeletePlan godoc
 | 
				
			||||||
@@ -217,10 +234,24 @@ func (pc *Controller) UpdatePlan(c *gin.Context) {
 | 
				
			|||||||
// @Success      200 {object} controller.Response "业务码为200代表删除成功"
 | 
					// @Success      200 {object} controller.Response "业务码为200代表删除成功"
 | 
				
			||||||
// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
					// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
				
			||||||
// @Router       /plans/{id} [delete]
 | 
					// @Router       /plans/{id} [delete]
 | 
				
			||||||
func (pc *Controller) DeletePlan(c *gin.Context) {
 | 
					func (c *Controller) DeletePlan(ctx *gin.Context) {
 | 
				
			||||||
	// 占位符:此处应调用服务层或仓库层来删除计划
 | 
						// 1. 从 URL 路径中获取 ID
 | 
				
			||||||
	pc.logger.Infof("收到删除计划请求 (占位符)")
 | 
						idStr := ctx.Param("id")
 | 
				
			||||||
	controller.SendResponse(c, controller.CodeSuccess, "删除计划接口占位符", nil)
 | 
						id, err := strconv.ParseUint(idStr, 10, 32)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的计划ID格式")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 2. 调用仓库层删除计划
 | 
				
			||||||
 | 
						if err := c.planRepo.DeletePlan(uint(id)); err != nil {
 | 
				
			||||||
 | 
							c.logger.Errorf("删除计划失败: %v", err)
 | 
				
			||||||
 | 
							controller.SendErrorResponse(ctx, controller.CodeInternalError, "删除计划时发生内部错误")
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 3. 发送成功响应
 | 
				
			||||||
 | 
						controller.SendResponse(ctx, controller.CodeSuccess, "计划删除成功", nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// StartPlan godoc
 | 
					// StartPlan godoc
 | 
				
			||||||
@@ -232,10 +263,10 @@ func (pc *Controller) DeletePlan(c *gin.Context) {
 | 
				
			|||||||
// @Success      200 {object} controller.Response "业务码为200代表成功启动计划"
 | 
					// @Success      200 {object} controller.Response "业务码为200代表成功启动计划"
 | 
				
			||||||
// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
					// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
				
			||||||
// @Router       /plans/{id}/start [post]
 | 
					// @Router       /plans/{id}/start [post]
 | 
				
			||||||
func (pc *Controller) StartPlan(c *gin.Context) {
 | 
					func (c *Controller) StartPlan(ctx *gin.Context) {
 | 
				
			||||||
	// 占位符:此处应调用服务层或仓库层来启动计划
 | 
						// 占位符:此处应调用服务层或仓库层来启动计划
 | 
				
			||||||
	pc.logger.Infof("收到启动计划请求 (占位符)")
 | 
						c.logger.Infof("收到启动计划请求 (占位符)")
 | 
				
			||||||
	controller.SendResponse(c, controller.CodeSuccess, "启动计划接口占位符", nil)
 | 
						controller.SendResponse(ctx, controller.CodeSuccess, "启动计划接口占位符", nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// StopPlan godoc
 | 
					// StopPlan godoc
 | 
				
			||||||
@@ -247,8 +278,8 @@ func (pc *Controller) StartPlan(c *gin.Context) {
 | 
				
			|||||||
// @Success      200 {object} controller.Response "业务码为200代表成功停止计划"
 | 
					// @Success      200 {object} controller.Response "业务码为200代表成功停止计划"
 | 
				
			||||||
// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
					// @Failure      200 {object} controller.Response "业务失败,具体错误码和信息见响应体(例如400, 404, 500)"
 | 
				
			||||||
// @Router       /plans/{id}/stop [post]
 | 
					// @Router       /plans/{id}/stop [post]
 | 
				
			||||||
func (pc *Controller) StopPlan(c *gin.Context) {
 | 
					func (c *Controller) StopPlan(ctx *gin.Context) {
 | 
				
			||||||
	// 占位符:此处应调用服务层或仓库层来停止计划
 | 
						// 占位符:此处应调用服务层或仓库层来停止计划
 | 
				
			||||||
	pc.logger.Infof("收到停止计划请求 (占位符)")
 | 
						c.logger.Infof("收到停止计划请求 (占位符)")
 | 
				
			||||||
	controller.SendResponse(c, controller.CodeSuccess, "停止计划接口占位符", nil)
 | 
						controller.SendResponse(ctx, controller.CodeSuccess, "停止计划接口占位符", nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,15 +18,15 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// MockPlanRepository 是 repository.PlanRepository 的一个模拟实现,用于测试
 | 
					// MockPlanRepository 是 repository.PlanRepository 的一个模拟实现,用于测试
 | 
				
			||||||
// [保持原样,不做任何修改]
 | 
					 | 
				
			||||||
type MockPlanRepository struct {
 | 
					type MockPlanRepository struct {
 | 
				
			||||||
	CreatePlanFunc     func(plan *models.Plan) error
 | 
						CreatePlanFunc     func(plan *models.Plan) error
 | 
				
			||||||
	GetPlanByIDFunc    func(id uint) (*models.Plan, error)
 | 
						GetPlanByIDFunc    func(id uint) (*models.Plan, error)
 | 
				
			||||||
	// ... 可以根据需要模拟其他接口方法
 | 
						ListBasicPlansFunc func() ([]models.Plan, error)
 | 
				
			||||||
 | 
						DeletePlanFunc     func(id uint) error
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *MockPlanRepository) ListBasicPlans() ([]models.Plan, error) {
 | 
					func (m *MockPlanRepository) ListBasicPlans() ([]models.Plan, error) {
 | 
				
			||||||
	panic("implement me")
 | 
						return m.ListBasicPlansFunc()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *MockPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
 | 
					func (m *MockPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
 | 
				
			||||||
@@ -49,7 +49,7 @@ func (m *MockPlanRepository) UpdatePlan(plan *models.Plan) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *MockPlanRepository) DeletePlan(id uint) error {
 | 
					func (m *MockPlanRepository) DeletePlan(id uint) error {
 | 
				
			||||||
	panic("implement me")
 | 
						return m.DeletePlanFunc(id)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// setupTestRouter 创建一个用于测试的 gin 引擎和控制器实例
 | 
					// setupTestRouter 创建一个用于测试的 gin 引擎和控制器实例
 | 
				
			||||||
@@ -60,6 +60,8 @@ func setupTestRouter(repo repository.PlanRepository) *gin.Engine {
 | 
				
			|||||||
	planController := NewController(logger, repo)
 | 
						planController := NewController(logger, repo)
 | 
				
			||||||
	router.POST("/plans", planController.CreatePlan)
 | 
						router.POST("/plans", planController.CreatePlan)
 | 
				
			||||||
	router.GET("/plans/:id", planController.GetPlan)
 | 
						router.GET("/plans/:id", planController.GetPlan)
 | 
				
			||||||
 | 
						router.GET("/plans", planController.ListPlans)
 | 
				
			||||||
 | 
						router.DELETE("/plans/:id", planController.DeletePlan)
 | 
				
			||||||
	return router
 | 
						return router
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -257,3 +259,207 @@ func TestController_GetPlan(t *testing.T) {
 | 
				
			|||||||
		assert.Equal(t, "获取计划详情时发生内部错误", resp.Message)
 | 
							assert.Equal(t, "获取计划详情时发生内部错误", resp.Message)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TestController_ListPlans tests the ListPlans method
 | 
				
			||||||
 | 
					func TestController_ListPlans(t *testing.T) {
 | 
				
			||||||
 | 
						t.Run("成功-获取计划列表", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							mockPlans := []models.Plan{
 | 
				
			||||||
 | 
								{Model: gorm.Model{ID: 1}, Name: "Plan 1", ContentType: models.PlanContentTypeTasks},
 | 
				
			||||||
 | 
								{Model: gorm.Model{ID: 2}, Name: "Plan 2", ContentType: models.PlanContentTypeTasks},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								ListBasicPlansFunc: func() ([]models.Plan, error) {
 | 
				
			||||||
 | 
									return mockPlans, nil
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodGet, "/plans", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeSuccess, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "获取计划列表成功", resp.Message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataBytes, err := json.Marshal(resp.Data)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
							var listResp ListPlansResponse
 | 
				
			||||||
 | 
							err = json.Unmarshal(dataBytes, &listResp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, 2, listResp.Total)
 | 
				
			||||||
 | 
							assert.Len(t, listResp.Plans, 2)
 | 
				
			||||||
 | 
							assert.Equal(t, uint(1), listResp.Plans[0].ID)
 | 
				
			||||||
 | 
							assert.Equal(t, "Plan 1", listResp.Plans[0].Name)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("成功-返回空列表", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								ListBasicPlansFunc: func() ([]models.Plan, error) {
 | 
				
			||||||
 | 
									return []models.Plan{}, nil
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodGet, "/plans", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeSuccess, resp.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							dataBytes, err := json.Marshal(resp.Data)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
							var listResp ListPlansResponse
 | 
				
			||||||
 | 
							err = json.Unmarshal(dataBytes, &listResp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, 0, listResp.Total)
 | 
				
			||||||
 | 
							assert.Len(t, listResp.Plans, 0)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("失败-仓库层返回错误", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							dbErr := errors.New("db error")
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								ListBasicPlansFunc: func() ([]models.Plan, error) {
 | 
				
			||||||
 | 
									return nil, dbErr
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodGet, "/plans", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeInternalError, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "获取计划列表时发生内部错误", resp.Message)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TestController_DeletePlan 是 DeletePlan 的单元测试
 | 
				
			||||||
 | 
					func TestController_DeletePlan(t *testing.T) {
 | 
				
			||||||
 | 
						t.Run("成功-删除计划", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								DeletePlanFunc: func(id uint) error {
 | 
				
			||||||
 | 
									assert.Equal(t, uint(1), id)
 | 
				
			||||||
 | 
									return nil // Simulate successful deletion
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodDelete, "/plans/1", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeSuccess, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "计划删除成功", resp.Message)
 | 
				
			||||||
 | 
							assert.Nil(t, resp.Data)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("失败-计划不存在", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								DeletePlanFunc: func(id uint) error {
 | 
				
			||||||
 | 
									return gorm.ErrRecordNotFound // Simulate not found
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodDelete, "/plans/999", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeInternalError, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "删除计划时发生内部错误", resp.Message)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("失败-无效的ID格式", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{} // No repo call expected
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodDelete, "/plans/abc", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeBadRequest, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "无效的计划ID格式", resp.Message)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t.Run("失败-仓库层内部错误", func(t *testing.T) {
 | 
				
			||||||
 | 
							// Arrange
 | 
				
			||||||
 | 
							internalErr := errors.New("something went wrong")
 | 
				
			||||||
 | 
							mockRepo := &MockPlanRepository{
 | 
				
			||||||
 | 
								DeletePlanFunc: func(id uint) error {
 | 
				
			||||||
 | 
									return internalErr // Simulate internal error
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							router := setupTestRouter(mockRepo)
 | 
				
			||||||
 | 
							w := httptest.NewRecorder()
 | 
				
			||||||
 | 
							req, _ := http.NewRequest(http.MethodDelete, "/plans/1", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Act
 | 
				
			||||||
 | 
							router.ServeHTTP(w, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Assert
 | 
				
			||||||
 | 
							assert.Equal(t, http.StatusOK, w.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							var resp controller.Response
 | 
				
			||||||
 | 
							err := json.Unmarshal(w.Body.Bytes(), &resp)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							assert.Equal(t, controller.CodeInternalError, resp.Code)
 | 
				
			||||||
 | 
							assert.Equal(t, "删除计划时发生内部错误", resp.Message)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user