Files

78 lines
2.1 KiB
Go
Raw Permalink 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 service
import "testing"
func TestRainDays(t *testing.T) {
daily := []DailyForecast{
{Date: "2026-08-12", TextDay: "小雨", TextNight: "阴"},
{Date: "2026-08-13", TextDay: "多云", TextNight: "晴"},
{Date: "2026-08-14", TextDay: "雷阵雨", TextNight: "小雨"},
}
if n := RainDays(daily); n != 2 {
t.Errorf("RainDays = %d, want 2", n)
}
if n := RainDays(nil); n != 0 {
t.Errorf("空预报 RainDays = %d, want 0", n)
}
}
func TestWeatherTextRainy(t *testing.T) {
if !WeatherTextRainy("小雨") {
t.Error("小雨应判为降雨")
}
if !WeatherTextRainy("雷阵雨") {
t.Error("雷阵雨应判为降雨")
}
if WeatherTextRainy("晴") {
t.Error("晴不应判为降雨")
}
}
func TestFungalRisk(t *testing.T) {
// 湿度 85 + 未来 2 天降雨 → 白僵病高风险
now := WeatherNow{Temp: 27, Humidity: 85, Text: "小雨"}
daily := []DailyForecast{
{TextDay: "小雨"}, {TextDay: "中雨"}, {TextDay: "多云"},
}
level, reason := FungalRisk(now, daily)
if level != "orange" {
t.Errorf("FungalRisk level = %s, want orange(原因 %s", level, reason)
}
// 湿度正常 → 无
now2 := WeatherNow{Temp: 27, Humidity: 60, Text: "晴"}
if l, _ := FungalRisk(now2, daily); l != "" {
t.Errorf("湿度 60 不应触发白僵病: %s", l)
}
}
func TestTempSwingRisk(t *testing.T) {
if l, _ := TempSwingRisk(32); l != "yellow" {
t.Errorf("32℃ 应为 yellow,实际 %s", l)
}
if l, _ := TempSwingRisk(18); l != "yellow" {
t.Errorf("18℃ 应为 yellow,实际 %s", l)
}
if l, _ := TempSwingRisk(25); l != "" {
t.Errorf("25℃ 不应触发,实际 %s", l)
}
}
func TestEvaluateWeatherRules(t *testing.T) {
now := WeatherNow{Temp: 32, Humidity: 85, Text: "小雨"}
daily := []DailyForecast{{TextDay: "小雨"}, {TextDay: "中雨"}, {TextDay: "多云"}}
alerts := EvaluateWeatherRules(now, daily, "late5")
if len(alerts) == 0 {
t.Fatal("高温高湿+阴雨应产生预警")
}
found := map[string]bool{}
for _, a := range alerts {
found[a.Disease] = true
if a.Reason == "" {
t.Errorf("预警缺少原因: %+v", a)
}
}
if !found["白僵病"] {
t.Error("缺少白僵病预警")
}
}