Files
iotlcm/internal/service/renewchange/renew_balance_test.go
T
MiFaZhan 2bec257289 chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。

- Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件)
- 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、
  库名 xiujie_admin → iotlcm、哨兵名占位
- 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异,
  不破坏「校验 MSISDN 格式」等测试断言)
- 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像
  digest 记录
- Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23),
  移除写死的 git 凭据
- README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0
  LICENSE,LF 行尾 + .gitattributes

已知:3 个测试包在源仓同一提交上同样失败(client/upstream、
service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
2026-10-22 16:37:56 +08:00

109 lines
2.9 KiB
Go

package renewchange
import (
"context"
"path/filepath"
"strings"
"testing"
"time"
_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
rc "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/repository/renewchange"
)
func TestDeductRuntimeRenewBalanceRejectsInsufficientBalanceAtomically(t *testing.T) {
ctx := context.Background()
if err := gdb.SetConfig(gdb.Config{
"default": gdb.ConfigGroup{{Type: "sqlite", Name: filepath.Join(t.TempDir(), "renew-balance.db")}},
}); err != nil {
t.Fatalf("configure sqlite: %v", err)
}
defer g.DB().Close(ctx)
createRenewBalanceTables(t, ctx)
if _, err := g.DB().Model(rc.TableAgentBalance).Ctx(ctx).Data(g.Map{
"dept_id": 117,
"tenant_id": "000000",
"balance": 100,
"status": renewFinanceAccountStatusNormal,
}).Insert(); err != nil {
t.Fatalf("seed balance: %v", err)
}
main := &rc.BatchMainRow{
BatchNo: "REN20260806110000TEST",
TenantId: "000000",
CreatedBy: 60,
}
err := g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
_, _, _, err := deductRuntimeRenewBalance(ctx, tx, main, 117, 576, time.Date(2026, 8, 6, 11, 0, 0, 0, time.Local))
return err
})
if err == nil || !strings.Contains(err.Error(), "账户余额不足") {
t.Fatalf("deduct error = %v, want insufficient balance", err)
}
balance, err := g.DB().Model(rc.TableAgentBalance).Ctx(ctx).
Where("dept_id", 117).
Where("tenant_id", "000000").
Value("balance")
if err != nil {
t.Fatalf("query balance: %v", err)
}
if got := balance.Float64(); got != 100 {
t.Fatalf("balance = %.2f, want unchanged 100.00", got)
}
count, err := g.DB().Model(rc.TableBalanceDetail).Ctx(ctx).
Where("serial_no", "RENPAY"+main.BatchNo).
Count()
if err != nil {
t.Fatalf("count balance detail: %v", err)
}
if count != 0 {
t.Fatalf("balance detail count = %d, want 0", count)
}
}
func createRenewBalanceTables(t *testing.T, ctx context.Context) {
t.Helper()
statements := []string{
`CREATE TABLE t_agent_balance (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_id INTEGER NOT NULL,
tenant_id TEXT NOT NULL,
balance REAL NOT NULL,
status INTEGER NOT NULL DEFAULT 0,
updated_by INTEGER,
updated_at DATETIME
)`,
`CREATE TABLE t_balance_detail (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_id INTEGER NOT NULL,
serial_no TEXT NOT NULL,
transaction_time DATETIME,
income_expense_type INTEGER,
transaction_type INTEGER,
transaction_channel INTEGER,
transaction_status INTEGER,
amount REAL,
before_balance REAL,
balance_after REAL,
order_sn TEXT,
remark TEXT,
tenant_id TEXT NOT NULL,
created_dept INTEGER,
created_by INTEGER,
created_at DATETIME
)`,
}
for _, statement := range statements {
if _, err := g.DB().Exec(ctx, statement); err != nil {
t.Fatalf("create renew balance table: %v", err)
}
}
}