修复 code review 问题:提取重复函数、补全缺失测试用例
- 提取 ResolveTermsFromTx 到 service/package,消除 order 和 auto_purchase 中的重复实现 - 补全 domain 测试:新增 from_activation 覆盖用例,现覆盖两种覆盖值 - 补全激活集成测试:新增 from_purchase 立即激活场景、历史记录兼容回退计数器验证 - 补全自动购包集成测试:新增购买后修改套餐配置不影响已有快照的验证 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -98,6 +98,69 @@ func TestRefundFollowUpActivatesNextPackageFromSnapshot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFromPurchaseActivatesImmediatelyAtPurchaseTime 验证 from_purchase 快照在购买时直接激活,不等实名。
|
||||
func TestFromPurchaseActivatesImmediatelyAtPurchaseTime(t *testing.T) {
|
||||
tx := testutil.NewPostgresTransaction(t)
|
||||
redisClient := testutil.NewRedisClient(t)
|
||||
card := createActivationTermsCard(t, tx, constants.RealNameStatusNotVerified)
|
||||
pkg := createActivationTermsPackage(t, tx, constants.PackageExpiryBaseFromPurchase, 7)
|
||||
usage := createActivationTermsUsage(t, tx, pkg.ID, card.ID, 1, constants.PackageExpiryBaseFromPurchase, 7, false, time.Now())
|
||||
service := NewActivationService(tx, redisClient, nil, nil, nil, zap.NewNop())
|
||||
if err := service.ActivateSpecificPackage(context.Background(), usage.ID); err != nil {
|
||||
t.Fatalf("from_purchase 直接激活失败:%v", err)
|
||||
}
|
||||
var refreshed model.PackageUsage
|
||||
if err := tx.First(&refreshed, usage.ID).Error; err != nil {
|
||||
t.Fatalf("查询激活结果失败:%v", err)
|
||||
}
|
||||
if refreshed.Status != constants.PackageUsageStatusActive || refreshed.ActivatedAt == nil || refreshed.ExpiresAt == nil {
|
||||
t.Fatalf("from_purchase 套餐应已激活:status=%d activatedAt=%v expiresAt=%v", refreshed.Status, refreshed.ActivatedAt, refreshed.ExpiresAt)
|
||||
}
|
||||
if refreshed.PendingRealnameActivation {
|
||||
t.Fatal("from_purchase 不应标记待实名")
|
||||
}
|
||||
expectedExpiry := CalculateExpiryTime(constants.PackageCalendarTypeByDay, *refreshed.ActivatedAt, 0, 7)
|
||||
if !refreshed.ExpiresAt.Equal(expectedExpiry) {
|
||||
t.Fatalf("to期时间不符:want=%v got=%v", expectedExpiry, refreshed.ExpiresAt)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHistoricalFallbackFiresWarningAndIncrementsCounter 验证历史记录缺少快照时回退套餐当前配置并递增计数器。
|
||||
func TestHistoricalFallbackFiresWarningAndIncrementsCounter(t *testing.T) {
|
||||
tx := testutil.NewPostgresTransaction(t)
|
||||
redisClient := testutil.NewRedisClient(t)
|
||||
card := createActivationTermsCard(t, tx, constants.RealNameStatusVerified)
|
||||
pkg := createActivationTermsPackage(t, tx, constants.PackageExpiryBaseFromActivation, 21)
|
||||
// 历史记录:四个快照字段全为空值/零值
|
||||
unique := uint(time.Now().UnixNano() & 0x7fffffff)
|
||||
usage := &model.PackageUsage{
|
||||
OrderID: unique, OrderNo: "UR55-HIST-" + strconv.FormatUint(uint64(unique), 10),
|
||||
PackageID: pkg.ID, PackageName: "UR55历史测试套餐",
|
||||
UsageType: constants.AssetWalletResourceTypeIotCard, IotCardID: card.ID,
|
||||
DataLimitMB: 1, Status: constants.PackageUsageStatusPending, Priority: 1, Generation: 1,
|
||||
// 快照字段均留空,模拟 UR#55 上线前的旧数据
|
||||
}
|
||||
if err := tx.Omit("status").Create(usage).Error; err != nil {
|
||||
t.Fatalf("创建历史测试使用记录失败:%v", err)
|
||||
}
|
||||
if err := tx.Model(usage).Update("status", constants.PackageUsageStatusPending).Error; err != nil {
|
||||
t.Fatalf("设置历史测试状态失败:%v", err)
|
||||
}
|
||||
before := HistoricalTermsFallbackCount()
|
||||
service := NewActivationService(tx, redisClient, nil, nil, nil, zap.NewNop())
|
||||
if err := service.ActivateSpecificPackage(context.Background(), usage.ID); err != nil {
|
||||
t.Fatalf("历史记录兼容激活失败:%v", err)
|
||||
}
|
||||
after := HistoricalTermsFallbackCount()
|
||||
if after <= before {
|
||||
t.Fatalf("历史回退计数器未递增:before=%d after=%d", before, after)
|
||||
}
|
||||
var refreshed model.PackageUsage
|
||||
if err := tx.First(&refreshed, usage.ID).Error; err != nil || refreshed.Status != constants.PackageUsageStatusActive {
|
||||
t.Fatalf("历史记录应兼容激活成功:status=%d err=%v", refreshed.Status, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestActivateSpecificPackageRejectsPartialSnapshot 验证新记录非法快照不会静默回退当前套餐配置。
|
||||
func TestActivateSpecificPackageRejectsPartialSnapshot(t *testing.T) {
|
||||
tx := testutil.NewPostgresTransaction(t)
|
||||
|
||||
Reference in New Issue
Block a user