This commit is contained in:
yml
2026-06-10 02:12:02 +08:00
parent 992d4ed234
commit c517349db6
6 changed files with 388 additions and 69 deletions
@@ -1,6 +1,7 @@
package wallet
import (
"context"
"testing"
"hfb_sys/backend/internal/model"
@@ -41,9 +42,10 @@ func TestRepositoryAccountCreatesAccountIfNotExists(t *testing.T) {
defer cleanupTestDB(t, db)
repo := NewRepository(db)
ctx := context.Background()
userID := uint64(1001)
account, err := repo.Account(userID)
account, err := repo.Account(ctx, userID)
if err != nil {
t.Fatalf("Account() error = %v", err)
}
@@ -68,10 +70,11 @@ func TestRepositoryRechargeIncreasesAvailableBalance(t *testing.T) {
defer cleanupTestDB(t, db)
repo := NewRepository(db)
ctx := context.Background()
userID := uint64(1002)
// 第一次充值
account, err := repo.Recharge(userID, 10000)
account, err := repo.Recharge(ctx, userID, 10000)
if err != nil {
t.Fatalf("Recharge() error = %v", err)
}
@@ -80,7 +83,7 @@ func TestRepositoryRechargeIncreasesAvailableBalance(t *testing.T) {
}
// 第二次充值
account, err = repo.Recharge(userID, 5000)
account, err = repo.Recharge(ctx, userID, 5000)
if err != nil {
t.Fatalf("Recharge() error = %v", err)
}
@@ -89,7 +92,7 @@ func TestRepositoryRechargeIncreasesAvailableBalance(t *testing.T) {
}
// 验证账本记录
ledger, err := repo.Ledger(userID, 1, 10)
ledger, err := repo.Ledger(ctx, userID, 1, 10)
if err != nil {
t.Fatalf("Ledger() error = %v", err)
}
@@ -104,34 +107,35 @@ func TestRepositoryConfirmRechargeFromChannelIsIdempotent(t *testing.T) {
defer cleanupTestDB(t, db)
repo := NewRepository(db)
ctx := context.Background()
userID := uint64(1003)
bizNo := "PAY123456"
amount := int64(10000)
// 第一次确认充值
err := repo.ConfirmRechargeFromChannel(userID, bizNo, amount)
err := repo.ConfirmRechargeFromChannel(ctx, userID, bizNo, amount)
if err != nil {
t.Fatalf("第一次 ConfirmRechargeFromChannel() error = %v", err)
}
account, _ := repo.Account(userID)
account, _ := repo.Account(ctx, userID)
if account.AvailableBalanceCent != amount {
t.Fatalf("第一次充值后余额 = %d, want %d", account.AvailableBalanceCent, amount)
}
// 第二次确认充值(相同 bizNo)应该幂等,不重复入账
err = repo.ConfirmRechargeFromChannel(userID, bizNo, amount)
err = repo.ConfirmRechargeFromChannel(ctx, userID, bizNo, amount)
if err != nil {
t.Fatalf("第二次 ConfirmRechargeFromChannel() error = %v", err)
}
account, _ = repo.Account(userID)
account, _ = repo.Account(ctx, userID)
if account.AvailableBalanceCent != amount {
t.Fatalf("第二次充值后余额 = %d, want %d(应保持不变)", account.AvailableBalanceCent, amount)
}
// 验证只有一条账本记录
ledger, _ := repo.Ledger(userID, 1, 10)
ledger, _ := repo.Ledger(ctx, userID, 1, 10)
if ledger.Total != 1 {
t.Fatalf("账本记录数 = %d, want 1(幂等)", ledger.Total)
}
@@ -159,7 +163,7 @@ func TestRepositoryConfirmRechargeFromChannelRejectsInvalidParams(t *testing.T)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := repo.ConfirmRechargeFromChannel(tc.userID, tc.bizNo, tc.amount)
err := repo.ConfirmRechargeFromChannel(context.Background(), tc.userID, tc.bizNo, tc.amount)
if err != tc.wantError {
t.Fatalf("error = %v, want %v", err, tc.wantError)
}
@@ -311,6 +315,7 @@ func TestRepositoryLedgerPagination(t *testing.T) {
defer cleanupTestDB(t, db)
repo := NewRepository(db)
ctx := context.Background()
userID := uint64(1008)
// 创建 25 条记录
@@ -330,7 +335,7 @@ func TestRepositoryLedgerPagination(t *testing.T) {
})
// 测试第一页
page1, err := repo.Ledger(userID, 1, 10)
page1, err := repo.Ledger(ctx, userID, 1, 10)
if err != nil {
t.Fatalf("Ledger() page 1 error = %v", err)
}
@@ -348,7 +353,7 @@ func TestRepositoryLedgerPagination(t *testing.T) {
}
// 测试第三页
page3, err := repo.Ledger(userID, 3, 10)
page3, err := repo.Ledger(ctx, userID, 3, 10)
if err != nil {
t.Fatalf("Ledger() page 3 error = %v", err)
}