测试库不再使用 SQLite 内存库,改为每个测试包独立的 MySQL 库, 表结构由 backend/migrations 的真实迁移建立。 基础设施 - 新增 internal/database/testdb.go:NewTestDB() 保持原签名(28 处调用零改动), 内部连接 MySQL 测试库,首次调用重建库并执行全部真实迁移,之后每次调用清空全表 - 库名 = 测试库基础名 + 包目录名,同包共享、跨包隔离,使 go test ./... 可并发执行 (共用一库时各包会互相 DROP/CREATE 导致 database exists) - 新增 NewDryRunDB():MySQL 方言 + DryRun,供断言 SQL 形态的 5 处测试使用 - 新增 AutoMigrateIfMissing():只对不存在的表 AutoMigrate, 避免改动迁移定义的索引与约束 - 新增 scripts/prepare-test-db.sh:建库与通配授权,可重复执行 - 33 处 sqlite.Open 全部替换,26 个测试文件改用 MySQL - 删除 46 处手写 SQLite 方言 DDL(AUTOINCREMENT / INTEGER PRIMARY KEY / TEXT), 表统一由真实迁移建立 - go.mod 移除 gorm.io/driver/sqlite 与 mattn/go-sqlite3 - 删除生产代码里 distribution/repository.go 的 sqlite 方言分支(strftime) 对齐环境后暴露并修复的真实问题 - DATE(col) 在 MySQL 返回 DATE 类型,使 Date string 字段变成 RFC3339 时间串, 大盘日期会带出时间部分;28 处改为 DATE_FORMAT(col, '%Y-%m-%d') - 系统自动创建分销结算批次时 created_by 写入 0,违反指向 admin_users 的外键, 生产会直接插入失败;改为写 NULL(新增 adminRef helper) - MySQL DATETIME 仅到秒,静默截断 Go 侧写入的时间, 导致租用时长计算与结算时点回读比对偏差; rental_orders 的 9 个业务时间列提到 DATETIME(6) - 测试数据补齐严格模式要求的 NOT NULL 列、修正超出列长的取值、补齐外键父记录 迁移 - 000065 的 ALTER TABLE rental_orders 一并完成业务时间列精度调整, feature 侧不再单开迁移文件 脚本与文档 - check.sh / check-backend-coverage.sh 去掉 -p 1 串行与 SQLite 相关注释 - 更新 docs/Repository层测试补充总结.md 的测试运行说明
130 lines
6.2 KiB
Go
130 lines
6.2 KiB
Go
package adminfinance
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func TestDisbursementStatisticsUseActualPaymentTimeAndAmount(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
|
|
loc := timeutil.ShanghaiLocation()
|
|
inRangeOffline := time.Date(2026, 7, 1, 10, 0, 0, 0, loc)
|
|
inRangeWithdrawal := time.Date(2026, 7, 2, 11, 0, 0, 0, loc)
|
|
inRangeManual := time.Date(2026, 7, 3, 12, 0, 0, 0, loc)
|
|
outOfRange := time.Date(2026, 6, 30, 23, 59, 0, 0, loc)
|
|
|
|
for _, args := range [][]any{
|
|
{1, "ORD-1", 1, 1, 1, 1, "platform_managed", "settled", 12000, inRangeOffline},
|
|
{2, "ORD-2", 2, 2, 2, 2, "platform_managed", "settled", 3000, outOfRange},
|
|
{3, "ORD-3", 3, 3, 3, 3, "platform_managed", "pending", 8000, nil},
|
|
{4, "ORD-4", 4, 4, 4, 4, "platform_managed", "pending", 0, nil},
|
|
{5, "ORD-5", 5, 5, 5, 5, "owner_wallet", "pending", 9000, nil},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO rental_orders
|
|
(id, order_no, listing_id, account_id, owner_id, renter_id,
|
|
settlement_mode, offline_settlement_status, offline_settlement_amount_cent, offline_settled_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, args...).Error; err != nil {
|
|
t.Fatalf("写入订单失败: %v", err)
|
|
}
|
|
}
|
|
for _, args := range [][]any{
|
|
{1, "PKR-1", 1, 1, 1, 1, "platform_managed", "settled", 4000, inRangeOffline, inRangeOffline},
|
|
{2, "PKR-2", 2, 2, 2, 2, "platform_managed", "pending", 2500, nil, inRangeOffline},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO admin_pickups
|
|
(id, pickup_no, listing_id, account_id, owner_id, admin_id, settlement_mode,
|
|
offline_settlement_status, settle_amount_cent, offline_settled_at, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, args...).Error; err != nil {
|
|
t.Fatalf("写入提号失败: %v", err)
|
|
}
|
|
}
|
|
for _, args := range [][]any{
|
|
{1, 1, "platform_managed", "settled", "补差价", 1, 500, inRangeManual, inRangeManual},
|
|
{2, 2, "platform_managed", "pending", "补差价", 1, 300, nil, inRangeManual},
|
|
{3, 3, "platform_managed", "recovery_pending", "补差价", 1, -200, nil, inRangeManual},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO admin_pickup_financial_adjustments
|
|
(id, pickup_id, settlement_mode, status, reason, created_by, settle_delta_cent, settled_at, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, args...).Error; err != nil {
|
|
t.Fatalf("写入提号调整失败: %v", err)
|
|
}
|
|
}
|
|
for _, args := range [][]any{
|
|
{1, "MD-1", "推广投放", "测试收款方", "测试备注", 1, "paid", 2500, inRangeManual},
|
|
{2, "MD-2", "推广投放", "测试收款方", "测试备注", 1, "paid", 1000, outOfRange},
|
|
{3, "MD-3", "推广投放", "测试收款方", "测试备注", 1, "voided", 6000, inRangeManual},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO manual_disbursements
|
|
(id, disbursement_no, category, payee_name, remark, created_by, status, amount_cent, paid_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, args...).Error; err != nil {
|
|
t.Fatalf("写入其他线下出款失败: %v", err)
|
|
}
|
|
}
|
|
for _, args := range [][]any{
|
|
{1, "WD-1", 1, "alipay", "测试户名", "test@example.com", "completed", 10000, 100, 9900, inRangeWithdrawal},
|
|
{2, "WD-2", 1, "alipay", "测试户名", "test@example.com", "completed", 5000, 0, 5000, outOfRange},
|
|
{3, "WD-3", 1, "alipay", "测试户名", "test@example.com", "processing", 5000, 200, 4800, nil},
|
|
{4, "WD-4", 1, "alipay", "测试户名", "test@example.com", "pending", 2100, 100, 2000, nil},
|
|
{5, "WD-5", 1, "alipay", "测试户名", "test@example.com", "rejected", 6000, 100, 5900, nil},
|
|
} {
|
|
if err := db.Exec(`INSERT INTO withdrawal_requests
|
|
(id, withdraw_no, user_id, account_type, account_name, account_no,
|
|
status, amount_cent, fee_cent, actual_amount_cent, paid_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, args...).Error; err != nil {
|
|
t.Fatalf("写入提现单失败: %v", err)
|
|
}
|
|
}
|
|
|
|
query := DashboardQuery{
|
|
StartDate: time.Date(2026, 7, 1, 0, 0, 0, 0, loc),
|
|
EndDate: time.Date(2026, 7, 3, 23, 59, 59, 0, loc),
|
|
}
|
|
repo := NewRepository(db)
|
|
summary, err := repo.disbursementSummary(t.Context(), query)
|
|
if err != nil {
|
|
t.Fatalf("disbursementSummary() error = %v", err)
|
|
}
|
|
if summary.PaidAmountCent != 28900 || summary.PaidCount != 5 {
|
|
t.Fatalf("实际出款 = %d/%d, want 28900/5", summary.PaidAmountCent, summary.PaidCount)
|
|
}
|
|
if summary.OfflineSettlementPaidAmountCent != 16500 || summary.OfflineSettlementPaidCount != 3 {
|
|
t.Fatalf("代管打款 = %d/%d, want 16500/3", summary.OfflineSettlementPaidAmountCent, summary.OfflineSettlementPaidCount)
|
|
}
|
|
if summary.WithdrawalAmountCent != 10000 || summary.WithdrawalFeeAmountCent != 100 || summary.WithdrawalPaidAmountCent != 9900 {
|
|
t.Fatalf("提现申请/手续费/实付 = %d/%d/%d, want 10000/100/9900", summary.WithdrawalAmountCent, summary.WithdrawalFeeAmountCent, summary.WithdrawalPaidAmountCent)
|
|
}
|
|
if summary.ManualPaidAmountCent != 2500 || summary.ManualPaidCount != 1 {
|
|
t.Fatalf("其他线下出款 = %d/%d, want 2500/1", summary.ManualPaidAmountCent, summary.ManualPaidCount)
|
|
}
|
|
if summary.PendingPaymentAmountCent != 15600 || summary.PendingPaymentCount != 4 {
|
|
t.Fatalf("待出款 = %d/%d, want 15600/4", summary.PendingPaymentAmountCent, summary.PendingPaymentCount)
|
|
}
|
|
if summary.OfflineSettlementPendingAmountCent != 10800 || summary.OfflineSettlementPendingCount != 3 {
|
|
t.Fatalf("线下结算待出款 = %d/%d, want 10800/3", summary.OfflineSettlementPendingAmountCent, summary.OfflineSettlementPendingCount)
|
|
}
|
|
if summary.WithdrawalReviewAmountCent != 2000 || summary.WithdrawalReviewCount != 1 {
|
|
t.Fatalf("待审核提现 = %d/%d, want 2000/1", summary.WithdrawalReviewAmountCent, summary.WithdrawalReviewCount)
|
|
}
|
|
|
|
daily, err := repo.dailyDisbursements(t.Context(), query)
|
|
if err != nil {
|
|
t.Fatalf("dailyDisbursements() error = %v", err)
|
|
}
|
|
if len(daily) != 3 {
|
|
t.Fatalf("每日出款行数 = %d, want 3", len(daily))
|
|
}
|
|
if daily[0].Date != "2026-07-01" || daily[0].OfflineSettlementPaidAmountCent != 16000 {
|
|
t.Fatalf("第一日代管出款 = %+v", daily[0])
|
|
}
|
|
if daily[1].Date != "2026-07-02" || daily[1].WithdrawalPaidAmountCent != 9900 {
|
|
t.Fatalf("第二日提现出款 = %+v", daily[1])
|
|
}
|
|
if daily[2].Date != "2026-07-03" || daily[2].ManualPaidAmountCent != 2500 || daily[2].OfflineSettlementPaidAmountCent != 500 {
|
|
t.Fatalf("第三日其他线下出款 = %+v", daily[2])
|
|
}
|
|
}
|