代码拉取完成,页面将自动刷新
package oncall_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/target/goalert/oncall"
)
func TestUserCalculator(t *testing.T) {
type result struct {
Time time.Time
Value []string
}
var (
start = time.Date(2000, 1, 2, 3, 4, 0, 0, time.UTC)
end = time.Date(2000, 1, 2, 3, 8, 0, 0, time.UTC)
)
check := func(desc string, expected []result, setup func(*oncall.UserCalculator)) {
t.Run(desc, func(t *testing.T) {
iter := oncall.NewTimeIterator(
start,
end,
time.Minute,
).NewUserCalculator()
if setup != nil {
setup(iter)
}
iter.Init()
var count int
for iter.Next() {
i := count
count++
if count > len(expected) {
t.Errorf("unexpected result: Value=%v, Time=%d", iter.ActiveUsers(), iter.Unix())
continue
}
if len(expected[i].Value) > 0 || len(iter.ActiveUsers()) > 0 {
assert.EqualValuesf(t, expected[i].Value, iter.ActiveUsers(), "result[%d].Value", i)
}
assert.Equalf(t, expected[i].Time.String(), time.Unix(iter.Unix(), 0).UTC().String(), "result[%d].Time", i)
}
if count != len(expected) {
t.Errorf("got %d results; want %d", count, len(expected))
}
})
}
check("empty",
[]result{
{Time: start},
{Time: end},
},
nil,
)
check("simple",
[]result{
{Time: start},
{Time: time.Date(2000, 1, 2, 3, 5, 0, 0, time.UTC), Value: []string{"foo"}},
{Time: time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC)},
{Time: end},
},
func(iter *oncall.UserCalculator) {
iter.SetSpan(time.Date(2000, 1, 2, 3, 5, 0, 0, time.UTC), time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC), "foo")
},
)
check("at-start",
[]result{
{Time: time.Date(2000, 1, 2, 3, 3, 0, 0, time.UTC), Value: []string{"foo"}},
{Time: time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC)},
{Time: end},
},
func(iter *oncall.UserCalculator) {
iter.SetSpan(time.Date(2000, 1, 2, 3, 3, 0, 0, time.UTC), time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC), "foo")
},
)
check("multiple",
[]result{
{Time: start},
{Time: time.Date(2000, 1, 2, 3, 5, 0, 0, time.UTC), Value: []string{"foo"}},
{Time: time.Date(2000, 1, 2, 3, 6, 0, 0, time.UTC), Value: []string{"foo", "bar"}},
{Time: time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC)},
{Time: end},
},
func(iter *oncall.UserCalculator) {
iter.SetSpan(time.Date(2000, 1, 2, 3, 5, 0, 0, time.UTC), time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC), "foo")
iter.SetSpan(time.Date(2000, 1, 2, 3, 6, 0, 0, time.UTC), time.Date(2000, 1, 2, 3, 7, 0, 0, time.UTC), "bar")
},
)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。