test: 后端测试从 SQLite 全面切换到 MySQL,与生产环境对齐
测试库不再使用 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 的测试运行说明
This commit is contained in:
@@ -4,8 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"hfb_sys/backend/internal/database"
|
||||
)
|
||||
|
||||
func TestFilterAdminListingsByAssetLevels(t *testing.T) {
|
||||
@@ -100,10 +99,7 @@ func TestFilterAdminListingsBySkinGroupOnly(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApplyAdminListFiltersSmartRouting(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{DryRun: true})
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试数据库失败: %v", err)
|
||||
}
|
||||
db := database.NewDryRunDB()
|
||||
repo := &Repository{}
|
||||
|
||||
tests := []struct {
|
||||
@@ -143,4 +139,3 @@ func TestApplyAdminListFiltersSmartRouting(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -128,34 +128,34 @@ type AdminListResult struct {
|
||||
}
|
||||
|
||||
type PublicListQuery struct {
|
||||
Page int
|
||||
PageSize int
|
||||
Keyword string
|
||||
Sort string
|
||||
Zone string
|
||||
Server []string
|
||||
Region []string
|
||||
LoginMethod []string
|
||||
Rank []string
|
||||
Insurance []string
|
||||
Stamina []string
|
||||
Load []string
|
||||
SkinGroup []string
|
||||
SkinName []string
|
||||
MinCoin *float64
|
||||
MaxCoin *float64
|
||||
MinRatio *float64
|
||||
MaxRatio *float64
|
||||
MinPrice *float64
|
||||
MaxPrice *float64
|
||||
MinDeposit *float64
|
||||
MaxDeposit *float64
|
||||
MinTotal *float64
|
||||
MaxTotal *float64
|
||||
MinFireLevel *float64
|
||||
MaxFireLevel *float64
|
||||
MinDailyLoss *float64
|
||||
MaxDailyLoss *float64
|
||||
Page int
|
||||
PageSize int
|
||||
Keyword string
|
||||
Sort string
|
||||
Zone string
|
||||
Server []string
|
||||
Region []string
|
||||
LoginMethod []string
|
||||
Rank []string
|
||||
Insurance []string
|
||||
Stamina []string
|
||||
Load []string
|
||||
SkinGroup []string
|
||||
SkinName []string
|
||||
MinCoin *float64
|
||||
MaxCoin *float64
|
||||
MinRatio *float64
|
||||
MaxRatio *float64
|
||||
MinPrice *float64
|
||||
MaxPrice *float64
|
||||
MinDeposit *float64
|
||||
MaxDeposit *float64
|
||||
MinTotal *float64
|
||||
MaxTotal *float64
|
||||
MinFireLevel *float64
|
||||
MaxFireLevel *float64
|
||||
MinDailyLoss *float64
|
||||
MaxDailyLoss *float64
|
||||
MinSecretKD *float64
|
||||
MaxSecretKD *float64
|
||||
ResourceRanges map[string]NumberRange
|
||||
|
||||
@@ -209,7 +209,7 @@ func newExternalAdjustmentFixture(
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("初始化商品测试表失败: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.ListingUpload{}, &model.AuditLog{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.ListingUpload{}, &model.AuditLog{}); err != nil {
|
||||
t.Fatalf("初始化外部商品测试表失败: %v", err)
|
||||
}
|
||||
user := model.User{Phone: "18800000009", Nickname: "测试号主", RealnameStatus: "verified", Status: "active"}
|
||||
|
||||
@@ -205,4 +205,3 @@ func (h *Handler) UpdateScreenshots(c *gin.Context) {
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,21 +15,21 @@ import (
|
||||
|
||||
type listingRow struct {
|
||||
model.RentalListing
|
||||
Title string
|
||||
OwnerPhone string
|
||||
OwnerNickname string
|
||||
Title string
|
||||
OwnerPhone string
|
||||
OwnerNickname string
|
||||
DisplaySourceChannel string `gorm:"column:display_source_channel"`
|
||||
IsExternalUpload bool `gorm:"column:is_external_upload"`
|
||||
DistributionSiteCode string `gorm:"column:distribution_site_code"`
|
||||
DistributionSiteName string `gorm:"column:distribution_site_name"`
|
||||
Description string
|
||||
GameName string
|
||||
ServerRegion string
|
||||
LoginPlatform string
|
||||
RankLevel string
|
||||
HafCoinAmount int64
|
||||
AssetSummary datatypes.JSON `gorm:"column:asset_summary"`
|
||||
ScreenshotURLS datatypes.JSON `gorm:"column:screenshot_urls"`
|
||||
Description string
|
||||
GameName string
|
||||
ServerRegion string
|
||||
LoginPlatform string
|
||||
RankLevel string
|
||||
HafCoinAmount int64
|
||||
AssetSummary datatypes.JSON `gorm:"column:asset_summary"`
|
||||
ScreenshotURLS datatypes.JSON `gorm:"column:screenshot_urls"`
|
||||
}
|
||||
|
||||
func rowsToDTO(rows []listingRow) []ListingDTO {
|
||||
@@ -165,32 +165,32 @@ func (row listingRow) toDTO() ListingDTO {
|
||||
screenshotURLS := cleanScreenshotURLs(decodeScreenshots(row.ScreenshotURLS))
|
||||
reviewStatus, reviewReason := normalizedReviewState(row.Status, row.ReviewStatus, row.ReviewReason)
|
||||
return ListingDTO{
|
||||
ID: row.ID,
|
||||
ListingNo: row.ListingNo,
|
||||
AccountID: row.AccountID,
|
||||
OwnerID: row.OwnerID,
|
||||
OwnerPhone: row.OwnerPhone,
|
||||
OwnerNickname: row.OwnerNickname,
|
||||
SourceChannel: row.DisplaySourceChannel,
|
||||
IsExternalUpload: row.IsExternalUpload,
|
||||
Title: row.Title,
|
||||
Description: row.Description,
|
||||
GameName: row.GameName,
|
||||
ServerRegion: row.ServerRegion,
|
||||
LoginPlatform: row.LoginPlatform,
|
||||
RankLevel: row.RankLevel,
|
||||
HafCoinAmount: row.HafCoinAmount,
|
||||
AssetSummary: assetSummary,
|
||||
ScreenshotURLS: screenshotURLS,
|
||||
CoverURL: publicCoverURL(row.ID, screenshotURLS, row.Status, row.ReviewStatus),
|
||||
PriceCent: row.PriceCent,
|
||||
DepositAmountCent: row.DepositAmountCent,
|
||||
IsAccelerated: isAcceleratedSale(assetSummary),
|
||||
InTransaction: row.InTransaction,
|
||||
Status: row.Status,
|
||||
ReviewStatus: reviewStatus,
|
||||
HandoffMode: row.HandoffMode,
|
||||
SettlementMode: row.SettlementMode,
|
||||
ID: row.ID,
|
||||
ListingNo: row.ListingNo,
|
||||
AccountID: row.AccountID,
|
||||
OwnerID: row.OwnerID,
|
||||
OwnerPhone: row.OwnerPhone,
|
||||
OwnerNickname: row.OwnerNickname,
|
||||
SourceChannel: row.DisplaySourceChannel,
|
||||
IsExternalUpload: row.IsExternalUpload,
|
||||
Title: row.Title,
|
||||
Description: row.Description,
|
||||
GameName: row.GameName,
|
||||
ServerRegion: row.ServerRegion,
|
||||
LoginPlatform: row.LoginPlatform,
|
||||
RankLevel: row.RankLevel,
|
||||
HafCoinAmount: row.HafCoinAmount,
|
||||
AssetSummary: assetSummary,
|
||||
ScreenshotURLS: screenshotURLS,
|
||||
CoverURL: publicCoverURL(row.ID, screenshotURLS, row.Status, row.ReviewStatus),
|
||||
PriceCent: row.PriceCent,
|
||||
DepositAmountCent: row.DepositAmountCent,
|
||||
IsAccelerated: isAcceleratedSale(assetSummary),
|
||||
InTransaction: row.InTransaction,
|
||||
Status: row.Status,
|
||||
ReviewStatus: reviewStatus,
|
||||
HandoffMode: row.HandoffMode,
|
||||
SettlementMode: row.SettlementMode,
|
||||
ManagedAdminID: row.ManagedAdminID,
|
||||
DistributionSiteID: row.DistributionSiteID,
|
||||
DistributionSiteCode: row.DistributionSiteCode,
|
||||
|
||||
@@ -299,4 +299,3 @@ func isAllDigits(s string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -434,4 +434,3 @@ func (r *Repository) UpdateScreenshots(ctx context.Context, adminID uint64, list
|
||||
})
|
||||
return dto, err
|
||||
}
|
||||
|
||||
|
||||
@@ -91,4 +91,3 @@ func (s *Service) UpdateScreenshots(ctx context.Context, adminID uint64, id uint
|
||||
}
|
||||
return s.repo.UpdateScreenshots(ctx, adminID, id, req, meta)
|
||||
}
|
||||
|
||||
|
||||
@@ -384,7 +384,7 @@ func TestImportExternalUploadStoresSourceChannel(t *testing.T) {
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("failed to migrate test db: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.AdminUser{}, &model.ListingUpload{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.AdminUser{}, &model.ListingUpload{}); err != nil {
|
||||
t.Fatalf("failed to migrate upload tables: %v", err)
|
||||
}
|
||||
admin := model.AdminUser{
|
||||
@@ -424,7 +424,7 @@ func TestListPendingReviewIncludesSourceChannel(t *testing.T) {
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("failed to migrate test db: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.ListingUpload{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.ListingUpload{}); err != nil {
|
||||
t.Fatalf("failed to migrate listing uploads: %v", err)
|
||||
}
|
||||
user := model.User{Phone: "18800000001", RealnameStatus: "verified", Status: "active"}
|
||||
@@ -682,7 +682,7 @@ func TestListPublicWithDistributionSiteCode(t *testing.T) {
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("migrate err: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.DistributionSite{}, &model.DistributionSiteAccount{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.DistributionSite{}, &model.DistributionSiteAccount{}); err != nil {
|
||||
t.Fatalf("migrate distribution err: %v", err)
|
||||
}
|
||||
|
||||
@@ -774,7 +774,7 @@ func TestUpdateScreenshots(t *testing.T) {
|
||||
if err := database.MigrateListingLifecycleTestSchema(db); err != nil {
|
||||
t.Fatalf("failed to migrate test db: %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.AuditLog{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.AuditLog{}); err != nil {
|
||||
t.Fatalf("failed to migrate audit log: %v", err)
|
||||
}
|
||||
repo := NewRepository(db, nil)
|
||||
|
||||
Reference in New Issue
Block a user