测试库不再使用 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 的测试运行说明
109 lines
4.5 KiB
Go
109 lines
4.5 KiB
Go
package admindashboard
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"hfb_sys/backend/internal/database"
|
|
"hfb_sys/backend/internal/listingstatus"
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/timeutil"
|
|
)
|
|
|
|
func TestListingDailyOverviewGroupsBySourceChannel(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db, &model.ListingStatusEvent{}, &model.ListingUpload{}); err != nil {
|
|
t.Fatalf("AutoMigrate() error = %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
|
|
listingID1 := uint64(1)
|
|
listingID2 := uint64(2)
|
|
listingID4 := uint64(4)
|
|
|
|
uploads := []model.ListingUpload{
|
|
{ListingID: &listingID1, UploaderName: "客服1", SourceChannel: "淘宝"},
|
|
{ListingID: &listingID2, UploaderName: "客服2", SourceChannel: "微信"},
|
|
{ListingID: &listingID4, UploaderName: "客服3"},
|
|
}
|
|
if err := db.Create(&uploads).Error; err != nil {
|
|
t.Fatalf("create uploads error = %v", err)
|
|
}
|
|
|
|
events := []model.ListingStatusEvent{
|
|
{ListingID: listingID1, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now},
|
|
{ListingID: listingID1, EventType: listingstatus.EventOffline, Source: listingstatus.SourceAdmin, CreatedAt: now.Add(time.Minute)},
|
|
{ListingID: listingID2, EventType: listingstatus.EventRented, Source: listingstatus.SourceOrder, CreatedAt: now.Add(2 * time.Minute)},
|
|
{ListingID: 3, EventType: listingstatus.EventPublished, Source: listingstatus.SourceSeller, CreatedAt: now.Add(3 * time.Minute)},
|
|
{ListingID: listingID4, EventType: listingstatus.EventPublished, Source: listingstatus.SourceReview, CreatedAt: now.Add(4 * time.Minute)},
|
|
}
|
|
if err := db.Create(&events).Error; err != nil {
|
|
t.Fatalf("create events error = %v", err)
|
|
}
|
|
|
|
overview, err := NewRepository(db).listingDailyOverview(t.Context(), now, 1)
|
|
if err != nil {
|
|
t.Fatalf("listingDailyOverview() error = %v", err)
|
|
}
|
|
if overview.Today.PublishedCount != 3 || overview.Today.ActiveOfflineCount != 1 || overview.Today.TradeLeaveCount != 1 {
|
|
t.Fatalf("today stats = %#v", overview.Today)
|
|
}
|
|
|
|
statsByChannel := make(map[string]ListingChannelDayStatsDTO)
|
|
for _, item := range overview.TodayChannels {
|
|
statsByChannel[item.SourceChannel] = item
|
|
}
|
|
assertChannelStats(t, statsByChannel["淘宝"], 1, 1, 0)
|
|
assertChannelStats(t, statsByChannel["微信"], 0, 0, 1)
|
|
assertChannelStats(t, statsByChannel[listingChannelWebsite], 1, 0, 0)
|
|
assertChannelStats(t, statsByChannel[listingChannelExternalUnknown], 1, 0, 0)
|
|
}
|
|
|
|
func assertChannelStats(t *testing.T, item ListingChannelDayStatsDTO, published, offline, tradeLeave int64) {
|
|
t.Helper()
|
|
if item.PublishedCount != published || item.ActiveOfflineCount != offline || item.TradeLeaveCount != tradeLeave {
|
|
t.Fatalf("channel %q stats = %#v, want published=%d offline=%d tradeLeave=%d", item.SourceChannel, item, published, offline, tradeLeave)
|
|
}
|
|
}
|
|
|
|
func TestListingDailyOverviewGroupsExternalUploadsByUploader(t *testing.T) {
|
|
db := database.NewTestDB()
|
|
if err := database.AutoMigrateIfMissing(db, &model.ListingStatusEvent{}, &model.ListingUpload{}); err != nil {
|
|
t.Fatalf("AutoMigrate() error = %v", err)
|
|
}
|
|
loc := timeutil.ShanghaiLocation()
|
|
now := time.Date(2026, 7, 25, 12, 0, 0, 0, loc)
|
|
uploaderX := uint64(11)
|
|
uploaderY := uint64(12)
|
|
listingOne := uint64(101)
|
|
listingTwo := uint64(102)
|
|
listingThree := uint64(103)
|
|
uploads := []model.ListingUpload{
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingOne, CreatedAt: now},
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingTwo, CreatedAt: now.Add(time.Hour)},
|
|
{UploaderName: "小美", MatchedAdminID: &uploaderY, ListingID: &listingThree, CreatedAt: now.Add(2 * time.Hour)},
|
|
{UploaderName: "小晴", MatchedAdminID: &uploaderX, ListingID: &listingOne, CreatedAt: now.AddDate(0, 0, -1)},
|
|
}
|
|
if err := db.Create(&uploads).Error; err != nil {
|
|
t.Fatalf("create uploads error = %v", err)
|
|
}
|
|
|
|
overview, err := NewRepository(db).listingDailyOverview(t.Context(), now, 2)
|
|
if err != nil {
|
|
t.Fatalf("listingDailyOverview() error = %v", err)
|
|
}
|
|
if len(overview.TodayUploaders) != 2 {
|
|
t.Fatalf("today uploaders = %#v, want 2 rows", overview.TodayUploaders)
|
|
}
|
|
counts := make(map[string]int64)
|
|
for _, item := range overview.TodayUploaders {
|
|
counts[item.UploaderName] = item.UploadCount
|
|
}
|
|
if counts["小晴"] != 2 || counts["小美"] != 1 {
|
|
t.Fatalf("today uploader counts = %#v, want 小晴=2, 小美=1", counts)
|
|
}
|
|
if len(overview.UploaderTrend) != 3 {
|
|
t.Fatalf("uploader trend = %#v, want 3 rows", overview.UploaderTrend)
|
|
}
|
|
}
|