Files
go-sms-V2/internal/service/settle/consume_test.go
T

220 lines
7.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package settle
// consume_test.go S2-D2:消耗日汇总引擎单测(sqlite,glebarez)——聚合正确性/
// 幂等重跑/跨天边界/UNKNOWN 状态迁移重刷收敛(滚动重刷核心语义)/查询过滤。
// handler 端点(customer scope 收窄+手动回补)在 api 层 settle_handler_test.go。
import (
"context"
"math"
"testing"
"time"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"go-sms/internal/model/entity"
)
// newConsumeDB 内存库 + message_detail/billing_consume_log/stats_channel_daily。
func newConsumeDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if s, e := db.DB(); e == nil {
s.SetMaxOpenConns(1) // sqlite :memory: 多连接=多个空库
}
if err := db.AutoMigrate(&entity.MessageDetail{}, &entity.BillingConsumeLog{},
&entity.StatsChannelDaily{}); err != nil {
t.Fatal(err)
}
return db
}
// mkDetail 种一条明细(id 雪花由调用方递增保证唯一)。
func mkDetail(id, accountID, channelID int64, business string, status int8, fee float64, created time.Time) entity.MessageDetail {
return entity.MessageDetail{
SnowflakeModel: entity.SnowflakeModel{ID: id, CreatedAt: created},
TaskID: 800000 + id, AccountID: accountID, ChannelID: channelID,
Mobile: "13000001111", Content: "t", Status: status, Business: business, Fee: fee,
}
}
func feq(a, b float64) bool { return math.Abs(a-b) < 1e-9 }
func TestAggregateDay_Correctness(t *testing.T) {
db := newConsumeDB(t)
now := time.Now()
details := []entity.MessageDetail{
// 账号 1 · sms:2 成功(fee 1.5×2)+1 失败+1 未知+1 拦截 → submit5/succ2/fail1/unk1/fee 6.5
mkDetail(1, 1, 1, "sms", 2, 1.5, now), mkDetail(2, 1, 1, "sms", 2, 1.5, now),
mkDetail(3, 1, 1, "sms", 3, 1.0, now), mkDetail(4, 1, 1, "sms", 4, 1.0, now),
mkDetail(5, 1, 1, "sms", 5, 1.5, now),
// 账号 1 · video:1 成功 → submit1/succ1/fee 2.0
mkDetail(6, 1, 2, "video", 2, 2.0, now),
// 账号 100 · sms:1 成功 → submit1/succ1/fee 3.0
mkDetail(7, 100, 2, "sms", 2, 3.0, now),
}
if err := db.Create(&details).Error; err != nil {
t.Fatal(err)
}
svc := NewService(db)
a, ch, err := svc.AggregateDay(context.Background(), now)
if err != nil {
t.Fatal(err)
}
if a != 3 || ch != 2 {
t.Fatalf("落账行数 account=%d(期望3) channel=%d(期望2)", a, ch)
}
var logs []entity.BillingConsumeLog
if err := db.Order("account_id, business").Find(&logs).Error; err != nil {
t.Fatal(err)
}
if len(logs) != 3 {
t.Fatalf("billing_consume_log 行数=%d 期望 3", len(logs))
}
check := func(l entity.BillingConsumeLog, accountID int64, business string, submit, succ, fail, unknown int, fee float64) {
t.Helper()
if l.AccountID != accountID || l.Business != business {
t.Fatalf("行归属错: got account=%d business=%s", l.AccountID, l.Business)
}
if l.SubmitCount != submit || l.SuccessCount != succ || l.FailCount != fail || l.UnknownCount != unknown {
t.Fatalf("账号 %d %s 计数错: submit=%d succ=%d fail=%d unk=%d (期望 %d/%d/%d/%d)",
accountID, business, l.SubmitCount, l.SuccessCount, l.FailCount, l.UnknownCount, submit, succ, fail, unknown)
}
if !feq(l.FeeTotal, fee) {
t.Fatalf("账号 %d %s fee_total=%v 期望 %v", accountID, business, l.FeeTotal, fee)
}
}
check(logs[0], 1, "sms", 5, 2, 1, 1, 6.5)
check(logs[1], 1, "video", 1, 1, 0, 0, 2.0)
check(logs[2], 100, "sms", 1, 1, 0, 0, 3.0)
var chans []entity.StatsChannelDaily
if err := db.Order("channel_id").Find(&chans).Error; err != nil || len(chans) != 2 {
t.Fatalf("stats_channel_daily 行数=%d err=%v 期望 2", len(chans), err)
}
if chans[0].ChannelID != 1 || chans[0].SubmitCount != 5 || chans[0].SuccessCount != 2 ||
chans[0].FailCount != 1 || chans[0].UnknownCount != 1 {
t.Fatalf("通道 1 计数错: %+v", chans[0])
}
if chans[1].ChannelID != 2 || chans[1].SubmitCount != 2 || chans[1].SuccessCount != 2 {
t.Fatalf("通道 2 计数错: %+v", chans[1])
}
if chans[0].CostTotal != 0 {
t.Fatalf("cost_total 应保持 0(R1 出账计价),got %v", chans[0].CostTotal)
}
}
func TestAggregateDay_IdempotentAndBoundary(t *testing.T) {
db := newConsumeDB(t)
now := time.Now()
yesterday := now.AddDate(0, 0, -1)
details := []entity.MessageDetail{
mkDetail(1, 1, 1, "sms", 2, 2.0, now),
mkDetail(2, 1, 1, "sms", 3, 2.0, now),
mkDetail(3, 1, 1, "sms", 2, 2.0, yesterday), // 次日窗口外
}
if err := db.Create(&details).Error; err != nil {
t.Fatal(err)
}
svc := NewService(db)
for i := 0; i < 2; i++ { // 两遍幂等
a, ch, err := svc.AggregateDay(context.Background(), now)
if err != nil {
t.Fatal(err)
}
if a != 1 || ch != 1 {
t.Fatalf("第 %d 遍落账 account=%d channel=%d 期望 1/1", i+1, a, ch)
}
}
var n int64
db.Model(&entity.BillingConsumeLog{}).Count(&n)
if n != 1 {
t.Fatalf("幂等重跑后行数=%d 期望 1", n)
}
var l entity.BillingConsumeLog
db.First(&l)
if l.SubmitCount != 2 || l.SuccessCount != 1 || l.FailCount != 1 {
t.Fatalf("跨天边界泄漏(应只含今天): %+v", l)
}
if !feq(l.FeeTotal, 4.0) {
t.Fatalf("fee_total=%v 期望 4.0", l.FeeTotal)
}
}
func TestAggregateDay_StateMigration(t *testing.T) {
// 滚动重刷核心语义:72h 回执窗内 UNKNOWN→成功迁移后,重跑即覆盖收敛。
db := newConsumeDB(t)
now := time.Now()
if err := db.Create(&[]entity.MessageDetail{
mkDetail(1, 1, 1, "sms", 4, 1.0, now), // 先 UNKNOWN
}).Error; err != nil {
t.Fatal(err)
}
svc := NewService(db)
if _, _, err := svc.AggregateDay(context.Background(), now); err != nil {
t.Fatal(err)
}
var l entity.BillingConsumeLog
db.First(&l)
if l.UnknownCount != 1 || l.SuccessCount != 0 {
t.Fatalf("首跑应 unknown=1: %+v", l)
}
// 回执到达:状态迁移为成功,重刷收敛。
if err := db.Model(&entity.MessageDetail{}).Where("id = ?", int64(1)).
Update("status", 2).Error; err != nil {
t.Fatal(err)
}
if _, _, err := svc.AggregateDay(context.Background(), now); err != nil {
t.Fatal(err)
}
db.First(&l)
if l.UnknownCount != 0 || l.SuccessCount != 1 {
t.Fatalf("重刷后应 success=1/unknown=0: %+v", l)
}
}
func TestListConsume_Filters(t *testing.T) {
db := newConsumeDB(t)
now := time.Now()
if err := db.Create(&[]entity.BillingConsumeLog{
{AccountID: 1, Business: "sms", StatDate: now, SubmitCount: 5, SuccessCount: 5},
{AccountID: 1, Business: "video", StatDate: now, SubmitCount: 1, SuccessCount: 1},
{AccountID: 100, Business: "sms", StatDate: now.AddDate(0, 0, -3), SubmitCount: 2, SuccessCount: 2},
{AccountID: 100, Business: "sms", StatDate: now.AddDate(0, 0, -10), SubmitCount: 9, SuccessCount: 9},
}).Error; err != nil {
t.Fatal(err)
}
svc := NewService(db)
// 全量(默认排序 stat_date DESC)。
list, total, err := svc.ListConsume(context.Background(), ConsumeQuery{})
if err != nil || total != 4 || len(list) != 4 {
t.Fatalf("全量 total=%d len=%d err=%v", total, len(list), err)
}
// 账号过滤。
_, total, err = svc.ListConsume(context.Background(), ConsumeQuery{AccountID: 1})
if err != nil || total != 2 {
t.Fatalf("账号过滤 total=%d err=%v", total, err)
}
// 业务过滤。
_, total, err = svc.ListConsume(context.Background(), ConsumeQuery{Business: "video"})
if err != nil || total != 1 {
t.Fatalf("业务过滤 total=%d err=%v", total, err)
}
// 时间窗(近 7 天闭区间:今 2 行+3 天前 1 行;零点归一语义)。
_, total, err = svc.ListConsume(context.Background(), ConsumeQuery{
From: now.AddDate(0, 0, -6), To: now,
})
if err != nil || total != 3 {
t.Fatalf("时间窗 total=%d err=%v(期望 3)", total, err)
}
// 分页。
list, total, err = svc.ListConsume(context.Background(), ConsumeQuery{Page: 2, PageSize: 3})
if err != nil || total != 4 || len(list) != 1 {
t.Fatalf("分页 total=%d len=%d err=%v", total, len(list), err)
}
}