76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/freefire/jiang13-bbs/model"
|
|
)
|
|
|
|
func TestComplementaryLedgerRef(t *testing.T) {
|
|
if got := complementaryLedgerRef(model.PointReasonUnlockPost, "post"); got != "post_earn" {
|
|
t.Fatalf("unlock debit: got %q", got)
|
|
}
|
|
if got := complementaryLedgerRef(model.PointReasonUnlockPost, "post_earn"); got != "post" {
|
|
t.Fatalf("unlock credit: got %q", got)
|
|
}
|
|
if got := complementaryLedgerRef(model.PointReasonDownloadFile, "attachment"); got != "attachment_earn" {
|
|
t.Fatalf("download debit: got %q", got)
|
|
}
|
|
if got := complementaryLedgerRef(model.PointReasonCheckin, "checkin"); got != "" {
|
|
t.Fatalf("checkin should not pair, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPairLedgerIndicesMergesUnlock(t *testing.T) {
|
|
rows := []model.PointLedger{
|
|
{ID: 4, UserID: 2, Delta: 10, Reason: model.PointReasonUnlockPost, RefType: "post_earn", RefID: 9},
|
|
{ID: 3, UserID: 1, Delta: -10, Reason: model.PointReasonUnlockPost, RefType: "post", RefID: 9},
|
|
{ID: 2, UserID: 3, Delta: 5, Reason: model.PointReasonCheckin, RefType: "checkin", RefID: 1},
|
|
}
|
|
pairs := pairLedgerIndices(rows)
|
|
if len(pairs) != 2 {
|
|
t.Fatalf("pairs=%d want 2: %#v", len(pairs), pairs)
|
|
}
|
|
if pairs[0] != [2]int{0, 1} {
|
|
t.Fatalf("first pair=%v want [0 1]", pairs[0])
|
|
}
|
|
if pairs[1] != [2]int{2, -1} {
|
|
t.Fatalf("checkin pair=%v want [2 -1]", pairs[1])
|
|
}
|
|
}
|
|
|
|
func TestPairLedgerIndicesNearestSamePost(t *testing.T) {
|
|
rows := []model.PointLedger{
|
|
{ID: 10, UserID: 2, Delta: 8, Reason: model.PointReasonUnlockPost, RefType: "post_earn", RefID: 1},
|
|
{ID: 9, UserID: 1, Delta: -8, Reason: model.PointReasonUnlockPost, RefType: "post", RefID: 1},
|
|
{ID: 8, UserID: 4, Delta: 8, Reason: model.PointReasonUnlockPost, RefType: "post_earn", RefID: 1},
|
|
{ID: 7, UserID: 3, Delta: -8, Reason: model.PointReasonUnlockPost, RefType: "post", RefID: 1},
|
|
}
|
|
pairs := pairLedgerIndices(rows)
|
|
if len(pairs) != 2 {
|
|
t.Fatalf("pairs=%d want 2: %#v", len(pairs), pairs)
|
|
}
|
|
if pairs[0] != [2]int{0, 1} || pairs[1] != [2]int{2, 3} {
|
|
t.Fatalf("pairs=%v", pairs)
|
|
}
|
|
from, to := transferEnds(rows[0], rows[1])
|
|
if from != 1 || to != 2 {
|
|
t.Fatalf("flow=%d→%d want 1→2", from, to)
|
|
}
|
|
}
|
|
|
|
func TestMergePairedLedgerKeepsNewer(t *testing.T) {
|
|
a := model.PointLedger{ID: 4, UserID: 2, Delta: 10, Note: "正文解锁收益", Reason: model.PointReasonUnlockPost}
|
|
b := model.PointLedger{ID: 3, UserID: 1, Delta: -10, Note: "解锁帖子:长标题", Reason: model.PointReasonUnlockPost}
|
|
got := mergePairedLedger(a, b)
|
|
if got.ID != 4 {
|
|
t.Fatalf("id=%d want 4", got.ID)
|
|
}
|
|
if got.Note != "解锁帖子:长标题" {
|
|
t.Fatalf("note=%q", got.Note)
|
|
}
|
|
if got.Delta != 10 {
|
|
t.Fatalf("delta=%d want 10", got.Delta)
|
|
}
|
|
}
|