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:
@@ -279,7 +279,7 @@ func TestRepositoryCreateAppliesRenterGrowthDiscount(t *testing.T) {
|
||||
|
||||
func TestCreateOrderSnapshotsShelfMarkup(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.DistributionSite{}, &model.DistributionSiteAccount{}, &model.DistributionRuleVersion{}, &model.DistributionOrderFinancial{}, &model.DistributionOrderParty{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.DistributionSite{}, &model.DistributionSiteAccount{}, &model.DistributionRuleVersion{}, &model.DistributionOrderFinancial{}, &model.DistributionOrderParty{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -859,7 +859,7 @@ func TestCancelPendingHandoffKeepsAssetsUntilRefundApproved(t *testing.T) {
|
||||
t.Fatalf("create account failed: %v", err)
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
ListingNo: "LST-REFUND-REVIEW-001",
|
||||
ListingNo: "LST-REFUND-REV-001",
|
||||
OwnerID: owner.ID,
|
||||
AccountID: account.ID,
|
||||
Status: listingStatusRented,
|
||||
@@ -1319,7 +1319,7 @@ func TestAdminResetRenterConfirmTimeoutRefreshesStageTime(t *testing.T) {
|
||||
|
||||
func TestAdminForceHandoffStartsNormalOwnerOrder(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.Dispute{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Dispute{}); err != nil {
|
||||
t.Fatalf("migrate disputes failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -1387,7 +1387,7 @@ func TestAdminForceHandoffStartsNormalOwnerOrder(t *testing.T) {
|
||||
|
||||
func TestAdminForceHandoffAbnormalOwnerTimeout(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.Dispute{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Dispute{}); err != nil {
|
||||
t.Fatalf("migrate disputes failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -1460,26 +1460,26 @@ func TestOwnerConfirmCheckoutAfterTimeout(t *testing.T) {
|
||||
}
|
||||
|
||||
listing := model.RentalListing{
|
||||
OwnerID: owner.ID,
|
||||
AccountID: account.ID,
|
||||
Status: listingStatusRented,
|
||||
ReviewStatus: listingReviewStatusApproved,
|
||||
PriceCent: 1000,
|
||||
OwnerID: owner.ID,
|
||||
AccountID: account.ID,
|
||||
Status: listingStatusRented,
|
||||
ReviewStatus: listingReviewStatusApproved,
|
||||
PriceCent: 1000,
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatalf("create listing failed: %v", err)
|
||||
}
|
||||
|
||||
order := model.RentalOrder{
|
||||
OrderNo: "ORD-CHECKOUT-TIMEOUT-001",
|
||||
ListingID: listing.ID,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
RenterID: renter.ID,
|
||||
Status: orderStatusAbnormal,
|
||||
HandoffStatus: handoffStatusOwnerCheckoutConfirmTimeout,
|
||||
SettlementStatus: settlementStatusPending,
|
||||
RentAmountCent: 1000,
|
||||
OrderNo: "ORD-CHECKOUT-TIMEOUT-001",
|
||||
ListingID: listing.ID,
|
||||
AccountID: account.ID,
|
||||
OwnerID: owner.ID,
|
||||
RenterID: renter.ID,
|
||||
Status: orderStatusAbnormal,
|
||||
HandoffStatus: handoffStatusOwnerCheckoutConfirmTimeout,
|
||||
SettlementStatus: settlementStatusPending,
|
||||
RentAmountCent: 1000,
|
||||
DepositAmountCent: 0,
|
||||
}
|
||||
if err := db.Create(&order).Error; err != nil {
|
||||
@@ -1526,7 +1526,7 @@ func TestOwnerConfirmCheckoutAfterTimeout(t *testing.T) {
|
||||
|
||||
func TestAdminForceHandoffUsesExistingOwnerRecordAndRejectsPlatformOrderBeforeHandoff(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.Dispute{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Dispute{}); err != nil {
|
||||
t.Fatalf("migrate disputes failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -1607,7 +1607,7 @@ func TestAdminForceHandoffUsesExistingOwnerRecordAndRejectsPlatformOrderBeforeHa
|
||||
|
||||
func TestAdminForceHandoffStartsPlatformManagedOrderAfterPlatformHandoff(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.Dispute{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Dispute{}); err != nil {
|
||||
t.Fatalf("migrate disputes failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -2017,7 +2017,7 @@ func TestPlatformManagedCheckoutIncludesConsumableAndDepositCompensation(t *test
|
||||
|
||||
func TestAdminPlatformCheckoutDisputeCreatesArbitrationCase(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.Dispute{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.Dispute{}); err != nil {
|
||||
t.Fatalf("migrate dispute failed: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
@@ -2217,7 +2217,7 @@ func TestAdminPlatformCheckoutConfirmRequiresExplicitConfirmationForZeroOwnerInc
|
||||
|
||||
func TestListForUserAndFindForUserSiteIsolation(t *testing.T) {
|
||||
db := setupOrderTestDB(t)
|
||||
if err := db.AutoMigrate(&model.DistributionSite{}); err != nil {
|
||||
if err := database.AutoMigrateIfMissing(db, &model.DistributionSite{}); err != nil {
|
||||
t.Fatalf("migrate distribution_sites err: %v", err)
|
||||
}
|
||||
repo := NewRepository(db)
|
||||
|
||||
@@ -5,13 +5,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/rentergrowth"
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func pureCoinDiscountOrder() model.RentalOrder {
|
||||
@@ -504,10 +503,7 @@ func TestNewOrderNoUsesShanghaiTimeWhenLocalIsUTC(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestApplyAdminOrderFiltersSmartRouting(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{DryRun: true})
|
||||
if err != nil {
|
||||
t.Fatalf("打开测试数据库失败: %v", err)
|
||||
}
|
||||
db := database.NewDryRunDB()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user