package model import ( "testing" "time" ) func TestConsumableLowStockAlert(t *testing.T) { c := Consumable{Quantity: 5, MinQuantity: 10} if !c.LowStockAlert() { t.Error("库存低于安全阈值应预警") } c2 := Consumable{Quantity: 10, MinQuantity: 10} if c2.LowStockAlert() { t.Error("库存等于安全阈值不应预警") } } func TestConsumableExpiringAlert(t *testing.T) { soon := time.Now().Add(10 * 24 * time.Hour) if !(Consumable{ExpiryDate: &soon}).ExpiringAlert(30) { t.Error("30 天内到期应预警") } past := time.Now().Add(-24 * time.Hour) if !(Consumable{ExpiryDate: &past}).ExpiringAlert(30) { t.Error("已过期应预警") } far := time.Now().Add(60 * 24 * time.Hour) if (Consumable{ExpiryDate: &far}).ExpiringAlert(30) { t.Error("60 天后到期不应预警(30 天窗口)") } if (Consumable{}).ExpiringAlert(30) { t.Error("无效期不应预警") } } func TestPurchaseSuggestion(t *testing.T) { if s := (Consumable{Quantity: 3, MinQuantity: 10}).PurchaseSuggestion(); s != 7 { t.Errorf("建议采购量 = %v, want 7", s) } if s := (Consumable{Quantity: 10, MinQuantity: 10}).PurchaseSuggestion(); s != 0 { t.Errorf("不缺货建议采购量 = %v, want 0", s) } }