181 lines
5.4 KiB
Go
181 lines
5.4 KiB
Go
// archive_test.go S4·D3:分区边界纯函数+归档全链(sqlite+mock CH)+冷查条件。
|
|
package archive
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/repository/clickhouse"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func newArchiveDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Discard})
|
|
if err != nil {
|
|
t.Fatalf("打开内存库失败: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&entity.MessageDetail{}); err != nil {
|
|
t.Fatalf("AutoMigrate 失败: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
// mockRows 查询游标打桩(仅支撑 count() 单列场景)。
|
|
type mockRows struct {
|
|
vals []int64
|
|
idx int
|
|
}
|
|
|
|
func (r *mockRows) Next() bool { r.idx++; return r.idx-1 < len(r.vals) }
|
|
func (r *mockRows) Scan(dest ...any) error {
|
|
if r.idx-1 < len(r.vals) {
|
|
v := r.vals[r.idx-1]
|
|
switch p := dest[0].(type) {
|
|
case *int64:
|
|
*p = v
|
|
case *uint64: // CH count() UInt64 口径
|
|
*p = uint64(v)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (r *mockRows) Close() error { return nil }
|
|
|
|
// mockCH ClickHouse Conn 打桩:记录写入/DDL;count 应答按调用序注入。
|
|
type mockCH struct {
|
|
inserted [][]any
|
|
execs []string
|
|
countReplies []int64
|
|
}
|
|
|
|
func (m *mockCH) Ping(context.Context) error { return nil }
|
|
func (m *mockCH) Close() error { return nil }
|
|
func (m *mockCH) Exec(_ context.Context, q string, _ ...any) error {
|
|
m.execs = append(m.execs, q)
|
|
return nil
|
|
}
|
|
func (m *mockCH) BatchInsert(_ context.Context, _ string, _ []string, rows [][]any) error {
|
|
m.inserted = append(m.inserted, rows...)
|
|
return nil
|
|
}
|
|
func (m *mockCH) Query(_ context.Context, q string, _ ...any) (clickhouse.Rows, error) {
|
|
if strings.Contains(q, "system.parts") {
|
|
return &mockRows{vals: []int64{1}}, nil // dropCHPartition 探测:分区存在
|
|
}
|
|
v := int64(0)
|
|
if strings.HasPrefix(q, "SELECT count()") && len(m.countReplies) > 0 {
|
|
v = m.countReplies[0]
|
|
m.countReplies = m.countReplies[1:]
|
|
}
|
|
return &mockRows{vals: []int64{v}}, nil
|
|
}
|
|
|
|
var _ clickhouse.Conn = (*mockCH)(nil)
|
|
|
|
// TestDayFloorSnowflakeMonotonic 边界单调+PartName/HotFloor 口径。
|
|
func TestDayFloorSnowflakeMonotonic(t *testing.T) {
|
|
base := time.Date(2026, 11, 16, 15, 30, 0, 0, time.Local)
|
|
if PartName(base) != "p20261116" {
|
|
t.Fatalf("PartName=%s 期望 p20261116", PartName(base))
|
|
}
|
|
prev := DayFloorSnowflake(base)
|
|
for i := 1; i <= 3; i++ {
|
|
next := DayFloorSnowflake(base.AddDate(0, 0, i))
|
|
if next <= prev {
|
|
t.Fatalf("边界非单调: day+%d", i)
|
|
}
|
|
prev = next
|
|
}
|
|
if got := HotFloor(base); !got.Equal(time.Date(2026, 11, 12, 0, 0, 0, 0, time.Local)) {
|
|
t.Fatalf("HotFloor=%v 期望 2026-11-12 零点", got)
|
|
}
|
|
}
|
|
|
|
// TestArchiveOnceFullCycle 归档全链:7 天前 2 行迁出(CH 收到+MySQL 清退,
|
|
// sqlite 无分区=DELETE 兜底分支),今天行保留;再跑幂等零动作。
|
|
func TestArchiveOnceFullCycle(t *testing.T) {
|
|
db := newArchiveDB(t)
|
|
old := time.Now().AddDate(0, 0, -7)
|
|
mk := func(id int64, at time.Time, mobile string) {
|
|
d := entity.MessageDetail{TaskID: 1, AccountID: 5, ChannelID: 7,
|
|
Mobile: mobile, Status: 2, Fee: 1}
|
|
d.ID = id
|
|
d.CreatedAt = at
|
|
if err := db.Create(&d).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
mk(73021000001, old, "13800001111")
|
|
mk(73021000002, old.Add(time.Hour), "13800002222")
|
|
mk(73021000003, time.Now(), "13800003333") // 热窗内:不迁
|
|
|
|
ch := &mockCH{countReplies: []int64{2}}
|
|
s := NewService(db, ch)
|
|
if done := s.ArchiveOnce(context.Background()); done != 1 {
|
|
t.Fatalf("应归档 1 天, got %d", done)
|
|
}
|
|
if len(ch.inserted) != 2 {
|
|
t.Fatalf("CH 应收到 2 行, got %d", len(ch.inserted))
|
|
}
|
|
var remainOld, remainNew int64
|
|
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800001111").Count(&remainOld)
|
|
db.Model(&entity.MessageDetail{}).Where("mobile = ?", "13800003333").Count(&remainNew)
|
|
if remainOld != 0 || remainNew != 1 {
|
|
t.Fatalf("清退异常: old=%d new=%d 期望 0/1", remainOld, remainNew)
|
|
}
|
|
if done := s.ArchiveOnce(context.Background()); done != 0 {
|
|
t.Fatalf("再跑应 0 天(幂等), got %d", done)
|
|
}
|
|
}
|
|
|
|
// TestArchiveVerifyMismatchKeepsMySQL 校验不匹配:CH count≠MySQL count →
|
|
// MySQL 保留(零丢失方向),不计完成。
|
|
func TestArchiveVerifyMismatchKeepsMySQL(t *testing.T) {
|
|
db := newArchiveDB(t)
|
|
old := time.Now().AddDate(0, 0, -7)
|
|
d := entity.MessageDetail{TaskID: 1, AccountID: 5, Mobile: "13800009999", Status: 2}
|
|
d.ID = 73021000009
|
|
d.CreatedAt = old
|
|
if err := db.Create(&d).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ch := &mockCH{countReplies: []int64{0}}
|
|
s := NewService(db, ch)
|
|
if done := s.ArchiveOnce(context.Background()); done != 0 {
|
|
t.Fatalf("校验不匹配不应计完成, got %d", done)
|
|
}
|
|
var n int64
|
|
db.Model(&entity.MessageDetail{}).Count(&n)
|
|
if n != 1 {
|
|
t.Fatalf("MySQL 应保留(零丢失方向), got %d", n)
|
|
}
|
|
}
|
|
|
|
// TestQueryHistoryWhere 冷查条件构造(六筛选→where 子句与参数序)。
|
|
func TestQueryHistoryWhere(t *testing.T) {
|
|
st := int8(2)
|
|
lo := time.Date(2026, 11, 1, 0, 0, 0, 0, time.Local)
|
|
hi := lo.AddDate(0, 0, 5)
|
|
w, args := historyWhere(HistoryQuery{
|
|
AccountID: 5, Mobile: "138", Status: &st, Start: &lo, End: &hi, TaskID: 9,
|
|
})
|
|
for _, want := range []string{
|
|
"account_id = ?", "mobile = ?", "status = ?",
|
|
"created_at >= ?", "created_at < ?", "task_id = ?",
|
|
} {
|
|
if !strings.Contains(w, want) {
|
|
t.Fatalf("where 缺 %q: %s", want, w)
|
|
}
|
|
}
|
|
if len(args) != 6 {
|
|
t.Fatalf("参数数 %d 期望 6", len(args))
|
|
}
|
|
}
|