提取修改猪群数量逻辑
This commit is contained in:
@@ -26,7 +26,7 @@ type MockDeviceRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Create 模拟 DeviceRepository 的 Create 方法
|
||||
// CreateTx 模拟 DeviceRepository 的 CreateTx 方法
|
||||
func (m *MockDeviceRepository) Create(device *models.Device) error {
|
||||
args := m.Called(device)
|
||||
return args.Error(0)
|
||||
@@ -169,7 +169,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
Properties: controller.Properties(`{"lora_address":"0x1234"}`),
|
||||
},
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {
|
||||
m.On("Create", mock.MatchedBy(func(dev *models.Device) bool {
|
||||
m.On("CreateTx", mock.MatchedBy(func(dev *models.Device) bool {
|
||||
// 检查 Name 字段
|
||||
nameMatch := dev.Name == "主控A"
|
||||
// 检查 Type 字段
|
||||
@@ -215,7 +215,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
Properties: controller.Properties(`{"bus_id":1,"bus_address":10}`),
|
||||
},
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {
|
||||
m.On("Create", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
|
||||
m.On("CreateTx", mock.Anything).Return(nil).Run(func(args mock.Arguments) {
|
||||
arg := args.Get(0).(*models.Device)
|
||||
arg.ID = 2
|
||||
arg.CreatedAt = time.Now()
|
||||
@@ -259,7 +259,7 @@ func TestCreateDevice(t *testing.T) {
|
||||
Type: models.DeviceTypeDevice,
|
||||
},
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {
|
||||
m.On("Create", mock.Anything).Return(errors.New("db error")).Once()
|
||||
m.On("CreateTx", mock.Anything).Return(errors.New("db error")).Once()
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedCode: controller.CodeInternalError,
|
||||
@@ -276,9 +276,9 @@ func TestCreateDevice(t *testing.T) {
|
||||
Properties: controller.Properties(`{invalid json}`),
|
||||
},
|
||||
mockRepoSetup: func(m *MockDeviceRepository) {
|
||||
// 期望 Create 方法被调用,并返回一个模拟的数据库错误
|
||||
// 期望 CreateTx 方法被调用,并返回一个模拟的数据库错误
|
||||
// 这个错误模拟的是数据库层因为 Properties 字段的 JSON 格式无效而拒绝保存
|
||||
m.On("Create", mock.Anything).Return(errors.New("database error: invalid json format")).Run(func(args mock.Arguments) {
|
||||
m.On("CreateTx", mock.Anything).Return(errors.New("database error: invalid json format")).Run(func(args mock.Arguments) {
|
||||
dev := args.Get(0).(*models.Device)
|
||||
assert.Equal(t, "无效JSON设备", dev.Name)
|
||||
assert.Equal(t, models.DeviceTypeDevice, dev.Type)
|
||||
|
||||
@@ -25,7 +25,7 @@ type MockUserRepository struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Create 模拟 UserRepository 的 Create 方法
|
||||
// CreateTx 模拟 UserRepository 的 CreateTx 方法
|
||||
func (m *MockUserRepository) Create(user *models.User) error {
|
||||
args := m.Called(user)
|
||||
return args.Error(0)
|
||||
@@ -90,8 +90,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 成功
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(nil).Run(func(args mock.Arguments) {
|
||||
// 模拟 CreateTx 成功
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(nil).Run(func(args mock.Arguments) {
|
||||
// 模拟数据库自动填充 ID
|
||||
userArg := args.Get(0).(*models.User)
|
||||
userArg.ID = 1 // 设置一个非零的 ID
|
||||
@@ -114,7 +114,7 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "123", // 密码少于6位
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
// 不会调用 CreateTx 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
@@ -128,7 +128,7 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 不会调用 Create 或 FindByUsername
|
||||
// 不会调用 CreateTx 或 FindByUsername
|
||||
},
|
||||
expectedResponse: map[string]interface{}{
|
||||
"code": float64(controller.CodeBadRequest),
|
||||
@@ -143,8 +143,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 失败,因为用户名已存在
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(errors.New("duplicate entry")).Once()
|
||||
// 模拟 CreateTx 失败,因为用户名已存在
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(errors.New("duplicate entry")).Once()
|
||||
// 模拟 FindByUsername 找到用户,确认是用户名重复
|
||||
m.On("FindByUsername", "existinguser").Return(&models.User{Username: "existinguser"}, nil).Once()
|
||||
},
|
||||
@@ -161,8 +161,8 @@ func TestCreateUser(t *testing.T) {
|
||||
Password: "password123",
|
||||
},
|
||||
mockRepoSetup: func(m *MockUserRepository) {
|
||||
// 模拟 Create 失败,通用数据库错误
|
||||
m.On("Create", mock.AnythingOfType("*models.User")).Return(errors.New("database error")).Once()
|
||||
// 模拟 CreateTx 失败,通用数据库错误
|
||||
m.On("CreateTx", mock.AnythingOfType("*models.User")).Return(errors.New("database error")).Once()
|
||||
// 模拟 FindByUsername 找不到用户,确认不是用户名重复
|
||||
m.On("FindByUsername", "db_error_user").Return(nil, gorm.ErrRecordNotFound).Once()
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user