59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package logic
|
||
|
||
import "testing"
|
||
|
||
// TestDedupBatchIds 去重 + 剔除非法 id,并保持首次出现顺序。
|
||
func TestDedupBatchIds(t *testing.T) {
|
||
got := dedupBatchIds([]int64{3, 1, 3, 0, -2, 1, 5})
|
||
want := []int64{3, 1, 5}
|
||
if len(got) != len(want) {
|
||
t.Fatalf("len=%d want %d (%v)", len(got), len(want), got)
|
||
}
|
||
for i := range want {
|
||
if got[i] != want[i] {
|
||
t.Fatalf("got[%d]=%d want %d (%v)", i, got[i], want[i], got)
|
||
}
|
||
}
|
||
if n := len(dedupBatchIds(nil)); n != 0 {
|
||
t.Fatalf("nil 应返回空,got len=%d", n)
|
||
}
|
||
}
|
||
|
||
// TestPartitionBatchIds 按存在性拆分,且保持入参顺序。
|
||
func TestPartitionBatchIds(t *testing.T) {
|
||
ok, missing := partitionBatchIds([]int64{5, 2, 7, 3}, map[int64]bool{5: true, 7: true})
|
||
if len(ok) != 2 || ok[0] != 5 || ok[1] != 7 {
|
||
t.Fatalf("ok=%v want [5 7]", ok)
|
||
}
|
||
if len(missing) != 2 || missing[0] != 2 || missing[1] != 3 {
|
||
t.Fatalf("missing=%v want [2 3]", missing)
|
||
}
|
||
}
|
||
|
||
// TestBatchFailures 失败明细映射(id + 原因)。
|
||
func TestBatchFailures(t *testing.T) {
|
||
out := batchFailures([]int64{9, 8}, "用户不存在")
|
||
if len(out) != 2 {
|
||
t.Fatalf("len=%d want 2", len(out))
|
||
}
|
||
if out[0].Id != 9 || out[0].Reason != "用户不存在" {
|
||
t.Fatalf("out[0]=%+v", out[0])
|
||
}
|
||
if out[1].Id != 8 {
|
||
t.Fatalf("out[1].Id=%d want 8", out[1].Id)
|
||
}
|
||
if n := len(batchFailures(nil, "x")); n != 0 {
|
||
t.Fatalf("nil 应返回空,got len=%d", n)
|
||
}
|
||
}
|
||
|
||
// TestNormalizeToggle 归一化为 0/1(幂等写值)。
|
||
func TestNormalizeToggle(t *testing.T) {
|
||
cases := map[int]int{0: 0, 1: 1, 2: 0, -3: 0, 99: 0}
|
||
for in, want := range cases {
|
||
if got := normalizeToggle(in); got != want {
|
||
t.Fatalf("normalizeToggle(%d)=%d want %d", in, got, want)
|
||
}
|
||
}
|
||
}
|