108 lines
3.4 KiB
Go
108 lines
3.4 KiB
Go
package repository_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
|
"github.com/stretchr/testify/assert"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestGormDeviceRepository(t *testing.T) {
|
|
db := setupTestDB(t)
|
|
repo := repository.NewGormDeviceRepository(db)
|
|
|
|
// --- 准备测试数据 ---
|
|
loraProps, _ := json.Marshal(models.LoraProperties{LoraAddress: "0xABCD"})
|
|
busProps, _ := json.Marshal(models.BusProperties{BusID: 1, BusAddress: 10})
|
|
|
|
areaController := &models.Device{
|
|
Name: "1号猪舍主控",
|
|
Type: models.DeviceTypeAreaController,
|
|
Location: "1号猪舍",
|
|
Properties: loraProps,
|
|
}
|
|
|
|
t.Run("创建 - 成功创建区域主控", func(t *testing.T) {
|
|
err := repo.Create(areaController)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, areaController.ID, "创建后应获得一个非零ID")
|
|
assert.Nil(t, areaController.ParentID, "区域主控的 ParentID 应为 nil")
|
|
})
|
|
|
|
var createdDevice *models.Device
|
|
t.Run("通过ID查找 - 成功找到已创建的设备", func(t *testing.T) {
|
|
var err error
|
|
createdDevice, err = repo.FindByID(areaController.ID)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, createdDevice)
|
|
assert.Equal(t, areaController.Name, createdDevice.Name)
|
|
})
|
|
|
|
t.Run("通过字符串ID查找 - 使用有效字符串ID找到设备", func(t *testing.T) {
|
|
foundDevice, err := repo.FindByIDString(strconv.FormatUint(uint64(areaController.ID), 10))
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, foundDevice)
|
|
assert.Equal(t, areaController.ID, foundDevice.ID)
|
|
})
|
|
|
|
t.Run("通过字符串ID查找 - 使用无效字符串ID", func(t *testing.T) {
|
|
_, err := repo.FindByIDString("invalid-id")
|
|
assert.Error(t, err, "使用无效ID字符串应返回错误")
|
|
})
|
|
|
|
// 创建一个子设备
|
|
childDevice := &models.Device{
|
|
Name: "1号猪舍温度传感器",
|
|
Type: models.DeviceTypeDevice,
|
|
SubType: models.SubTypeSensorTemp,
|
|
ParentID: &areaController.ID,
|
|
Location: "1号猪舍东侧",
|
|
Properties: busProps,
|
|
}
|
|
|
|
t.Run("创建 - 成功创建子设备", func(t *testing.T) {
|
|
err := repo.Create(childDevice)
|
|
assert.NoError(t, err)
|
|
assert.NotZero(t, childDevice.ID)
|
|
assert.NotNil(t, childDevice.ParentID)
|
|
assert.Equal(t, areaController.ID, *childDevice.ParentID)
|
|
})
|
|
|
|
t.Run("通过父ID列出 - 找到子设备", func(t *testing.T) {
|
|
children, err := repo.ListByParentID(&areaController.ID)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, children, 1, "应找到一个子设备")
|
|
assert.Equal(t, childDevice.ID, children[0].ID)
|
|
})
|
|
|
|
t.Run("通过父ID列出 - 找到顶层设备", func(t *testing.T) {
|
|
parents, err := repo.ListByParentID(nil)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, parents, 1, "应找到一个顶层设备")
|
|
assert.Equal(t, areaController.ID, parents[0].ID)
|
|
})
|
|
|
|
t.Run("更新 - 成功更新设备信息", func(t *testing.T) {
|
|
childDevice.Location = "1号猪舍西侧"
|
|
err := repo.Update(childDevice)
|
|
assert.NoError(t, err)
|
|
|
|
updatedDevice, _ := repo.FindByID(childDevice.ID)
|
|
assert.Equal(t, "1号猪舍西侧", updatedDevice.Location)
|
|
})
|
|
|
|
t.Run("删除 - 成功删除设备", func(t *testing.T) {
|
|
err := repo.Delete(childDevice.ID)
|
|
assert.NoError(t, err)
|
|
|
|
// 验证设备已被软删除
|
|
_, err = repo.FindByID(childDevice.ID)
|
|
assert.Error(t, err, "删除后应无法找到设备")
|
|
assert.ErrorIs(t, err, gorm.ErrRecordNotFound, "错误类型应为 RecordNotFound")
|
|
})
|
|
}
|