修复套餐快照集成测试与自动购包幂等

This commit is contained in:
2026-07-23 12:23:52 +09:00
parent 2df44d8e3c
commit 992757c056
8 changed files with 33 additions and 23 deletions

View File

@@ -1,8 +1,5 @@
package order
// testIDMask 用于将纳秒时间戳截断为合法的 uint 主键范围,仅限测试用。
const testIDMask = testIDMask
import (
"context"
"strconv"
@@ -17,6 +14,9 @@ import (
"gorm.io/gorm"
)
// testIDMask 用于将纳秒时间戳截断为合法的 uint 主键范围,仅限测试用。
const testIDMask = 0x7fffffff
// TestSynchronousPurchasePersistsImmutableTermsSnapshots 验证同步主套餐和加油包固化购买时计时条款。
func TestSynchronousPurchasePersistsImmutableTermsSnapshots(t *testing.T) {
tx := testutil.NewPostgresTransaction(t)
@@ -99,13 +99,13 @@ func TestCEndPurchasePersistsSnapshotAndActivatesImmediately(t *testing.T) {
service := &Service{shopPackageAllocationStore: postgres.NewShopPackageAllocationStore(tx), logger: zap.NewNop()}
now := time.Now()
order := &model.Order{
Model: gorm.Model{ID: uint(time.Now().UnixNano() & testIDMask)},
OrderNo: "UR55-CEND-" + strconv.FormatInt(time.Now().UnixNano(), 10),
OrderType: model.OrderTypeSingleCard,
BuyerType: model.BuyerTypePersonal,
IotCardID: &card.ID,
Model: gorm.Model{ID: uint(time.Now().UnixNano() & testIDMask)},
OrderNo: "UR55-CEND-" + strconv.FormatInt(time.Now().UnixNano(), 10),
OrderType: model.OrderTypeSingleCard,
BuyerType: model.BuyerTypePersonal,
IotCardID: &card.ID,
TotalAmount: 100,
Generation: 1,
Generation: 1,
}
if err := service.activateMainPackage(context.Background(), tx, order, pkg, constants.AssetWalletResourceTypeIotCard, card.ID, now); err != nil {
t.Fatalf("C 端购买创建主套餐失败:%v", err)

View File

@@ -1,8 +1,5 @@
package packagepkg
// testIDMask 用于将纳秒时间戳截断为合法的 uint 主键范围,仅限测试用。
const testIDMask = testIDMask
import (
"context"
"strconv"
@@ -16,6 +13,9 @@ import (
"gorm.io/gorm"
)
// testIDMask 用于将纳秒时间戳截断为合法的 uint 主键范围,仅限测试用。
const testIDMask = 0x7fffffff
// TestActivateByRealnameUsesPurchasedSnapshot 验证实名激活只使用购买快照计算起止时间。
func TestActivateByRealnameUsesPurchasedSnapshot(t *testing.T) {
tx := testutil.NewPostgresTransaction(t)
@@ -134,6 +134,10 @@ func TestHistoricalFallbackFiresWarningAndIncrementsCounter(t *testing.T) {
redisClient := testutil.NewRedisClient(t)
card := createActivationTermsCard(t, tx, constants.RealNameStatusVerified)
pkg := createActivationTermsPackage(t, tx, constants.PackageExpiryBaseFromActivation, 21)
// 仅在会回滚的测试事务内禁用触发器,以构造 UR#55 上线前的历史空快照记录。
if err := tx.Exec("ALTER TABLE tb_package_usage DISABLE TRIGGER trg_validate_package_usage_terms_snapshot").Error; err != nil {
t.Fatalf("禁用快照校验触发器失败:%v", err)
}
// 历史记录:四个快照字段全为空值/零值
unique := uint(time.Now().UnixNano() & testIDMask)
usage := &model.PackageUsage{

View File

@@ -20,11 +20,11 @@ type Service struct {
packageAllocationStore *postgres.ShopPackageAllocationStore
seriesAllocationStore *postgres.ShopSeriesAllocationStore
shopStore *postgres.ShopStore
auditService AuditService
auditService AuditLogger
}
// AuditService 敏感配置变更审计能力。
type AuditService interface {
// AuditLogger 敏感配置变更审计能力。
type AuditLogger interface {
LogOperation(ctx context.Context, log *model.AccountOperationLog)
}
@@ -34,7 +34,7 @@ func New(
packageAllocationStore *postgres.ShopPackageAllocationStore,
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
shopStore *postgres.ShopStore,
auditService AuditService,
auditService AuditLogger,
) *Service {
return &Service{
db: db,