Files
pig-farm-controller/internal/infra/repository/plan_repository_test.go

137 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package repository_test
import (
"errors"
"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"
)
// createTestPlan 是一个辅助函数,用于创建测试计划。
func createTestPlan(name, description string, execType models.PlanExecutionType, contentType models.PlanContentType) models.Plan {
return models.Plan{
Name: name,
Description: description,
ExecutionType: execType,
ContentType: contentType,
}
}
// TestListBasicPlans 测试 ListBasicPlans 方法,确保它能正确返回所有计划的基本信息。
func TestListBasicPlans(t *testing.T) {
tests := []struct {
name string
setupPlans []models.Plan
expectedCount int
expectedError error
}{
{
name: "数据库中没有计划",
setupPlans: []models.Plan{},
expectedCount: 0,
expectedError: nil,
},
{
name: "数据库中有多个计划",
setupPlans: []models.Plan{
createTestPlan("计划 A", "描述 A", models.PlanExecutionTypeAutomatic, models.PlanContentTypeTasks),
createTestPlan("计划 B", "描述 B", models.PlanExecutionTypeManual, models.PlanContentTypeSubPlans),
},
expectedCount: 2,
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := setupTestDB(t)
repo := repository.NewGormPlanRepository(db)
for i := range tt.setupPlans {
err := db.Create(&tt.setupPlans[i]).Error
assert.NoError(t, err, "插入设置计划失败")
}
plans, err := repo.ListBasicPlans()
if tt.expectedError != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.expectedError))
} else {
assert.NoError(t, err)
assert.Len(t, plans, tt.expectedCount)
if tt.expectedCount > 0 {
// 验证返回的计划是否包含预期的ID
var actualIDs []uint
for _, p := range plans {
actualIDs = append(actualIDs, p.ID)
assert.Empty(t, p.SubPlans, "ListBasicPlans 不应加载子计划")
assert.Empty(t, p.Tasks, "ListBasicPlans 不应加载任务")
}
for _, setupPlan := range tt.setupPlans {
assert.Contains(t, actualIDs, setupPlan.ID, "返回的计划应包含设置计划的ID")
}
}
}
})
}
}
// TestGetBasicPlanByID 测试 GetBasicPlanByID 方法确保它能根据ID正确返回计划的基本信息。
func TestGetBasicPlanByID(t *testing.T) {
tests := []struct {
name string
setupPlan models.Plan
idToFetch uint
expectFound bool
expectedError error
}{
{
name: "通过ID找到计划",
setupPlan: createTestPlan("计划 C", "描述 C", models.PlanExecutionTypeAutomatic, models.PlanContentTypeTasks),
idToFetch: 0, // 创建后设置
expectFound: true,
},
{
name: "通过ID未找到计划",
setupPlan: models.Plan{}, // 此情况下无需设置计划
idToFetch: 999,
expectFound: false,
expectedError: gorm.ErrRecordNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := setupTestDB(t)
repo := repository.NewGormPlanRepository(db)
if tt.setupPlan.Name != "" { // 仅在有效设置时创建计划
err := db.Create(&tt.setupPlan).Error
assert.NoError(t, err, "插入设置计划失败")
tt.idToFetch = tt.setupPlan.ID // 使用数据库生成的ID
}
fetchedPlan, err := repo.GetBasicPlanByID(tt.idToFetch)
if tt.expectedError != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.expectedError), "预期错误类型不匹配")
assert.Nil(t, fetchedPlan)
} else {
assert.NoError(t, err)
assert.NotNil(t, fetchedPlan)
assert.Equal(t, tt.setupPlan.Name, fetchedPlan.Name)
assert.Equal(t, tt.setupPlan.Description, fetchedPlan.Description)
assert.Equal(t, tt.setupPlan.ExecutionType, fetchedPlan.ExecutionType)
assert.Equal(t, tt.setupPlan.ContentType, fetchedPlan.ContentType)
assert.Empty(t, fetchedPlan.SubPlans, "GetBasicPlanByID 不应加载子计划")
assert.Empty(t, fetchedPlan.Tasks, "GetBasicPlanByID 不应加载任务")
}
})
}
}