MockDeviceRepo
This commit is contained in:
164
internal/tests/mocks/mock_repository.go
Normal file
164
internal/tests/mocks/mock_repository.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package mocks
|
||||
|
||||
// Package mocks 模拟测试包
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockDeviceRepo 模拟设备仓库,实现DeviceRepo接口
|
||||
type MockDeviceRepo struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Create 模拟创建设备方法
|
||||
func (m *MockDeviceRepo) Create(device *model.Device) error {
|
||||
args := m.Called(device)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
// FindByID 模拟根据ID查找设备方法
|
||||
func (m *MockDeviceRepo) FindByID(id uint) (*model.Device, error) {
|
||||
args := m.Called(id)
|
||||
// 返回第一个参数作为设备,第二个参数作为错误
|
||||
device, ok := args.Get(0).(*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return device, args.Error(1)
|
||||
}
|
||||
|
||||
// FindByIDString 模拟根据ID字符串查找设备方法
|
||||
func (m *MockDeviceRepo) FindByIDString(id string) (*model.Device, error) {
|
||||
args := m.Called(id)
|
||||
// 返回第一个参数作为设备,第二个参数作为错误
|
||||
device, ok := args.Get(0).(*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return device, args.Error(1)
|
||||
}
|
||||
|
||||
// FindByParentID 模拟根据上级设备ID查找设备方法
|
||||
func (m *MockDeviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
|
||||
args := m.Called(parentID)
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindByType 模拟根据设备类型查找设备方法
|
||||
func (m *MockDeviceRepo) FindByType(deviceType model.DeviceType) ([]*model.Device, error) {
|
||||
args := m.Called(deviceType)
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// Update 模拟更新设备信息方法
|
||||
func (m *MockDeviceRepo) Update(device *model.Device) error {
|
||||
args := m.Called(device)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
// Delete 模拟删除设备方法
|
||||
func (m *MockDeviceRepo) Delete(id uint) error {
|
||||
args := m.Called(id)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
// ListAll 模拟获取所有设备列表方法
|
||||
func (m *MockDeviceRepo) ListAll() ([]model.Device, error) {
|
||||
args := m.Called()
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindRelayDevices 模拟获取所有中继设备方法
|
||||
func (m *MockDeviceRepo) FindRelayDevices() ([]*model.Device, error) {
|
||||
args := m.Called()
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindByDeviceID 模拟根据设备ID查找设备方法(额外方法)
|
||||
func (m *MockDeviceRepo) FindByDeviceID(deviceID string) (*model.Device, error) {
|
||||
args := m.Called(deviceID)
|
||||
// 返回第一个参数作为设备,第二个参数作为错误
|
||||
device, ok := args.Get(0).(*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return device, args.Error(1)
|
||||
}
|
||||
|
||||
// FindControllers 模拟查找控制器方法(额外方法)
|
||||
func (m *MockDeviceRepo) FindControllers() ([]*model.Device, error) {
|
||||
args := m.Called()
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindRelays 模拟查找中继设备方法(额外方法)
|
||||
func (m *MockDeviceRepo) FindRelays() ([]*model.Device, error) {
|
||||
args := m.Called()
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindDevicesByType 模拟根据类型查找设备方法(额外方法)
|
||||
func (m *MockDeviceRepo) FindDevicesByType(deviceType string) ([]*model.Device, error) {
|
||||
args := m.Called(deviceType)
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// FindRelayDevices 模拟根据中继ID查找设备方法(额外方法)
|
||||
func (m *MockDeviceRepo) FindRelayDevicesByID(relayID uint) ([]*model.Device, error) {
|
||||
args := m.Called(relayID)
|
||||
// 返回第一个参数作为设备列表,第二个参数作为错误
|
||||
devices, ok := args.Get(0).([]*model.Device)
|
||||
if !ok {
|
||||
return nil, args.Error(1)
|
||||
}
|
||||
return devices, args.Error(1)
|
||||
}
|
||||
|
||||
// UpdateDeviceStatus 模拟更新设备状态方法(额外方法)
|
||||
func (m *MockDeviceRepo) UpdateDeviceStatus(id uint, active bool) error {
|
||||
args := m.Called(id, active)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
// GetDeviceStatus 模拟获取设备状态方法(额外方法)
|
||||
func (m *MockDeviceRepo) GetDeviceStatus(id uint) (bool, error) {
|
||||
args := m.Called(id)
|
||||
return args.Bool(0), args.Error(1)
|
||||
}
|
||||
Reference in New Issue
Block a user