51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package codes_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/wangjia/pangolin/server/internal/codes"
|
|
)
|
|
|
|
func TestListBatchesAndVoidBatchOnLibTables(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := openMigratedSQLite(t)
|
|
store := codes.NewStore(db)
|
|
svc := codes.NewService(store, nil, 5, time.Hour)
|
|
|
|
res, err := svc.CreateBatch(ctx, codes.BatchRequest{
|
|
PlanCode: codes.PlanPro, DurationDays: 30, Count: 3,
|
|
Channel: codes.ChannelManual, Note: "n", CreatedBy: "admin:1",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("mint: %v", err)
|
|
}
|
|
|
|
infos, total, err := store.ListBatches(ctx, 50, 0)
|
|
if err != nil {
|
|
t.Fatalf("ListBatches: %v", err)
|
|
}
|
|
if total != 1 || len(infos) != 1 {
|
|
t.Fatalf("total=%d len=%d", total, len(infos))
|
|
}
|
|
bi := infos[0]
|
|
if bi.ID != res.BatchID || bi.Channel != codes.ChannelManual || bi.CreatedBy != "admin:1" ||
|
|
bi.Note != "n" || bi.Total != 3 || bi.Unused != 3 || bi.Redeemed != 0 || bi.Void != 0 {
|
|
t.Fatalf("BatchInfo = %+v", bi)
|
|
}
|
|
|
|
n, err := store.VoidBatch(ctx, res.BatchID)
|
|
if err != nil || n != 3 {
|
|
t.Fatalf("VoidBatch = %d, %v", n, err)
|
|
}
|
|
infos, _, err = store.ListBatches(ctx, 50, 0)
|
|
if err != nil || infos[0].Void != 3 || infos[0].Unused != 0 {
|
|
t.Fatalf("after void: %+v (err=%v)", infos[0], err)
|
|
}
|
|
// 二次 void:0 行(只动 unused)。
|
|
if n, err := store.VoidBatch(ctx, res.BatchID); err != nil || n != 0 {
|
|
t.Fatalf("re-void = %d, %v", n, err)
|
|
}
|
|
}
|