feat: 添加独立其他设置菜单及有效端参数保存
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package business
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"kra/internal/biz/business"
|
||||
)
|
||||
|
||||
func TestOtherSettingsReuseParametersAndSaveAtomically(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
if err := db.AutoMigrate(&otherSettingsPO{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&otherSettingsPO{Name: "other", Key: "unrelated", Value: "keep"}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewOtherSettingsRepo(testData{db})
|
||||
ctx := context.Background()
|
||||
if value, err := r.Find(ctx); err != nil || value != nil {
|
||||
t.Fatal("缺省记录应由用例提供默认值")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for i := int64(1); i <= 8; i++ {
|
||||
wg.Add(1)
|
||||
go func(value int64) {
|
||||
defer wg.Done()
|
||||
if _, err := r.Save(ctx, business.OtherSettings{Amount: value * 100, Days: value}); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
value, err := NewOtherSettingsRepo(testData{db}).Find(ctx)
|
||||
if err != nil || value.Amount != value.Days*100 || value.UpdatedAt.IsZero() {
|
||||
t.Fatalf("重建仓储后应回显完整持久化参数: %+v %v", value, err)
|
||||
}
|
||||
var count int64
|
||||
if err := db.Model(&otherSettingsPO{}).Where(map[string]any{"key": otherSettingsKey}).Count(&count).Error; err != nil || count != 1 {
|
||||
t.Fatal("反复保存不能新增多份参数")
|
||||
}
|
||||
var other otherSettingsPO
|
||||
if err := db.Where(map[string]any{"key": "unrelated"}).First(&other).Error; err != nil || other.Value != "keep" {
|
||||
t.Fatal("保存不得覆盖其他参数")
|
||||
}
|
||||
if _, err := r.Save(ctx, business.OtherSettings{Amount: 0, Days: 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, _ = r.Find(ctx)
|
||||
if value.Amount != 0 || value.Days != 1 {
|
||||
t.Fatal("零金额必须持久化")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOtherSettingsMalformedStoredValueCanBeRepaired(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
if err := db.AutoMigrate(&otherSettingsPO{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewOtherSettingsRepo(testData{db})
|
||||
ctx := context.Background()
|
||||
if _, err := r.Save(ctx, business.DefaultOtherSettings()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, raw := range []string{`{"amount":3000}`, `{"amount":3000,"days":0}`, `{"amount":1.5,"days":30}`, `invalid`} {
|
||||
if err := db.Model(&otherSettingsPO{}).Where(map[string]any{"key": otherSettingsKey}).Update("value", raw).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.Find(ctx); err != business.ErrOtherSettingsStored {
|
||||
t.Fatalf("错误参数不能静默当成默认值: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := r.Save(ctx, business.DefaultOtherSettings()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := r.Find(ctx)
|
||||
if err != nil || value.Amount != 3000 || value.Days != 30 {
|
||||
t.Fatal("恢复默认保存应修复格式错误的参数")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user