将状态码改成自定义码
This commit is contained in:
@@ -203,7 +203,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
}).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusCreated,
|
||||
expectedCode: controller.CodeCreated,
|
||||
expectedMessage: "设备创建成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataMap, ok := data.(map[string]interface{})
|
||||
@@ -236,7 +236,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
}).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusCreated,
|
||||
expectedCode: controller.CodeCreated,
|
||||
expectedMessage: "设备创建成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataMap, ok := data.(map[string]interface{})
|
||||
@@ -260,7 +260,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
},
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusBadRequest,
|
||||
expectedCode: controller.CodeBadRequest,
|
||||
expectedMessage: "Key: 'CreateDeviceRequest.Name' Error:Field validation for 'Name' failed on the 'required' tag",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -275,7 +275,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
m.On("Create", mock.Anything).Return(errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
expectedMessage: "创建设备失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -299,9 +299,9 @@ func TestCreateDevice(t *testing.T) {
|
||||
assert.True(t, bytes.Equal(dev.Properties, expectedProperties), "Properties should match")
|
||||
}).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK, // HTTP status is 200 OK for business errors
|
||||
expectedCode: http.StatusInternalServerError, // Business code for internal server error
|
||||
expectedMessage: "创建设备失败", // The message returned by the controller
|
||||
expectedStatus: http.StatusOK, // HTTP status is 200 OK for business errors
|
||||
expectedCode: controller.CodeInternalError, // Business code for internal server error
|
||||
expectedMessage: "创建设备失败", // The message returned by the controller
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
}
|
||||
@@ -338,7 +338,7 @@ func TestGetDevice(t *testing.T) {
|
||||
}, nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "获取设备信息成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataMap, ok := data.(map[string]interface{})
|
||||
@@ -359,7 +359,7 @@ func TestGetDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "999").Return(nil, gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusNotFound,
|
||||
expectedCode: controller.CodeNotFound,
|
||||
expectedMessage: "设备未找到",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -372,7 +372,7 @@ func TestGetDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "abc").Return(nil, errors.New("无效的设备ID格式")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusBadRequest,
|
||||
expectedCode: controller.CodeBadRequest,
|
||||
expectedMessage: "无效的设备ID格式",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -385,7 +385,7 @@ func TestGetDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "1").Return(nil, errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
expectedMessage: "获取设备信息失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -413,7 +413,7 @@ func TestListDevices(t *testing.T) {
|
||||
m.On("ListAll").Return([]*models.Device{}, nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "获取设备列表成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
s, ok := data.([]interface{})
|
||||
@@ -450,7 +450,7 @@ func TestListDevices(t *testing.T) {
|
||||
}, nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "获取设备列表成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataList, ok := data.([]interface{})
|
||||
@@ -483,7 +483,7 @@ func TestListDevices(t *testing.T) {
|
||||
m.On("ListAll").Return(nil, errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
expectedMessage: "获取设备列表失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -529,7 +529,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
m.On("Update", mock.AnythingOfType("*models.Device")).Return(nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "设备更新成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataMap, ok := data.(map[string]interface{})
|
||||
@@ -555,7 +555,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "1").Return(&models.Device{Model: gorm.Model{ID: 1}}, nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusBadRequest,
|
||||
expectedCode: controller.CodeBadRequest,
|
||||
expectedMessage: "Key: 'UpdateDeviceRequest.Name' Error:Field validation for 'Name' failed on the 'required' tag",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -570,7 +570,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "999").Return(nil, gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusNotFound,
|
||||
expectedCode: controller.CodeNotFound,
|
||||
expectedMessage: "设备未找到",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -585,7 +585,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
m.On("FindByIDString", "abc").Return(nil, errors.New("无效的设备ID格式")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusBadRequest,
|
||||
expectedCode: controller.CodeBadRequest,
|
||||
expectedMessage: "无效的设备ID格式",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -601,7 +601,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
m.On("Update", mock.AnythingOfType("*models.Device")).Return(errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
expectedMessage: "更新设备失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -628,8 +628,8 @@ func TestUpdateDevice(t *testing.T) {
|
||||
}).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError, // Expected to be internal server error due to DB error
|
||||
expectedMessage: "更新设备失败", // The message returned by the controller
|
||||
expectedCode: controller.CodeInternalError, // Expected to be internal server error due to DB error
|
||||
expectedMessage: "更新设备失败", // The message returned by the controller
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
// 新增:成功更新设备的ParentID
|
||||
@@ -664,7 +664,7 @@ func TestUpdateDevice(t *testing.T) {
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "设备更新成功",
|
||||
expectedDataFunc: func(data interface{}) bool {
|
||||
dataMap, ok := data.(map[string]interface{})
|
||||
@@ -700,7 +700,7 @@ func TestDeleteDevice(t *testing.T) {
|
||||
m.On("Delete", uint(1)).Return(nil).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusOK,
|
||||
expectedCode: controller.CodeSuccess,
|
||||
expectedMessage: "设备删除成功",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -711,7 +711,7 @@ func TestDeleteDevice(t *testing.T) {
|
||||
paramID: "abc",
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusBadRequest,
|
||||
expectedCode: controller.CodeBadRequest,
|
||||
expectedMessage: "无效的设备ID格式",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -724,7 +724,7 @@ func TestDeleteDevice(t *testing.T) {
|
||||
m.On("Delete", uint(1)).Return(errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
expectedMessage: "删除设备失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
@@ -738,7 +738,7 @@ func TestDeleteDevice(t *testing.T) {
|
||||
m.On("Delete", uint(999)).Return(gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: http.StatusInternalServerError, // 当前控制器逻辑会将 ErrRecordNotFound 视为内部错误
|
||||
expectedCode: controller.CodeInternalError, // 当前控制器逻辑会将 ErrRecordNotFound 视为内部错误
|
||||
expectedMessage: "删除设备失败",
|
||||
expectedDataFunc: func(data interface{}) bool { return data == nil },
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user