package comm import "testing" func TestCompareVersion(t *testing.T) { cases := []struct { a, b string want int }{ {"1.0.0", "1.0.0", 0}, {"1.2", "1.2.0", 0}, // 段数不同,短的补 0 {"1.10.0", "1.9.0", 1}, // 字符串比较会判反,必须按整数 {"1.9.0", "1.10.0", -1}, {"2.0.0", "1.99.99", 1}, {"1.0.1", "1.0.0", 1}, {"1.0.0-beta", "1.0.0", 0}, // 非数字后缀取前导数字 {"1.2.3rc1", "1.2.3", 0}, {"", "1.0.0", -1}, {"1.0.0", "", 1}, } for _, c := range cases { if got := CompareVersion(c.a, c.b); got != c.want { t.Fatalf("CompareVersion(%q,%q)=%d, 期望 %d", c.a, c.b, got, c.want) } } } func TestNeedForceUpdate(t *testing.T) { on := &ChannelApp{ForceUpdate: true, MinVersion: "2.0.0"} if !on.NeedForceUpdate("1.9.9") { t.Fatal("低于最低版本应强更") } if on.NeedForceUpdate("2.0.0") || on.NeedForceUpdate("2.1.0") { t.Fatal("达到或高于最低版本不该强更") } if on.NeedForceUpdate("") { t.Fatal("客户端未上报版本时不该强更,否则老客户端会被全挡在门外") } off := &ChannelApp{ForceUpdate: false, MinVersion: "2.0.0"} if off.NeedForceUpdate("1.0.0") { t.Fatal("强更开关关闭时不该强更") } noMin := &ChannelApp{ForceUpdate: true} if noMin.NeedForceUpdate("1.0.0") { t.Fatal("未配最低版本时不该强更") } var nilApp *ChannelApp if nilApp.NeedForceUpdate("1.0.0") { t.Fatal("nil 配置不该强更") } } func TestIsReviewing(t *testing.T) { c := &ChannelApp{ReviewVersion: "3.1.0"} if !c.IsReviewing("3.1.0") { t.Fatal("送审版本应识别为审核中") } if c.IsReviewing("3.0.0") || c.IsReviewing("3.2.0") { t.Fatal("非送审版本不该识别为审核中") } if (&ChannelApp{}).IsReviewing("3.1.0") { t.Fatal("未配送审版本时恒为 false") } } func TestPickChannelApp(t *testing.T) { rows := []*ChannelApp{ {AppName: "", Channel: ChannelGlobalApple, Version: "1.0.0"}, {AppName: "voitrans", Channel: ChannelGlobalApple, Version: "2.0.0"}, {AppName: "", Channel: ChannelGlobalGoogle, Version: "1.5.0"}, } if got := PickChannelApp(rows, ChannelGlobalApple); got == nil || got.Version != "2.0.0" { t.Fatalf("应用行应覆盖全局行,实际 %+v", got) } if got := PickChannelApp(rows, ChannelGlobalGoogle); got == nil || got.Version != "1.5.0" { t.Fatalf("只有全局行时应回退全局,实际 %+v", got) } if got := PickChannelApp(rows, ChannelChinaHuawei); got != nil { t.Fatalf("该渠道无配置时应返回 nil,实际 %+v", got) } } func TestSplitPayChannels(t *testing.T) { got := SplitPayChannels(" wxpay , ,apple ") if len(got) != 2 || got[0] != PayWechat || got[1] != PayApple { t.Fatalf("SplitPayChannels 结果异常: %#v", got) } }