- 新增 CreateOrderReq 结构体用于统一订单创建参数- 修改 CreateOrder 方法签名,使用结构体传参替代多个参数 - 更新 jd_cookie 相关枚举值,增加 JdCookieStatusUnknown 状态 - 调整 OrderInfo 和 JdOrderInfo 模型字段,增强数据一致性 -优化订单与京东订单关联逻辑,移除冗余的 CurrentOrderId 字段 - 移除 ShouldExtractCard 方法,改为内部私有方法 shouldExtractCard- 精简 Callback 方法参数,移除不必要的 userOrderId 和 amount 参数 - 修复订单历史记录中订单号关联问题,直接使用 orderId 字段查询 - 更新控制器层参数传递方式,适配新的服务层接口定义 - 调整卡密提取逻辑,去除对用户订单实体的依赖 - 完善订单状态检查机制,提高卡密提取安全性 - 优化数据库查询逻辑,减少不必要的关联查询操作
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package jd_cookie
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
|
||
"kami/internal/consts"
|
||
"kami/internal/model/entity"
|
||
|
||
"github.com/gogf/gf/v2/os/gtime"
|
||
"github.com/gogf/gf/v2/test/gtest"
|
||
)
|
||
|
||
// TestShouldExtractCard 测试是否需要提取卡密的判断逻辑
|
||
func TestShouldExtractCard(t *testing.T) {
|
||
gtest.C(t, func(t *gtest.T) {
|
||
s := &sJdCookie{}
|
||
ctx := context.Background()
|
||
|
||
// 测试空订单
|
||
result := s.shouldExtractCard(ctx, nil)
|
||
t.Assert(result, false)
|
||
|
||
// 测试未支付订单
|
||
jdOrder := &entity.V1JdCookieJdOrder{
|
||
Status: int(consts.JdOrderStatusPending),
|
||
}
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, true) // 待支付状态可以提取卡密
|
||
|
||
// 测试已支付订单
|
||
jdOrder.Status = int(consts.JdOrderStatusPaid)
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, true) // 已支付状态可以提取卡密
|
||
|
||
// 测试已支付且有支付时间的订单
|
||
now := gtime.Now()
|
||
jdOrder.PaidAt = now
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, true)
|
||
|
||
// 测试已经提取过卡密的订单
|
||
jdOrder.CardNo = "1234567890"
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, false)
|
||
|
||
// 测试卡密不完整的订单(只有卡密没有卡号)
|
||
jdOrder.CardNo = ""
|
||
jdOrder.CardPassword = "password123"
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, false)
|
||
|
||
// 测试其他状态(已发货)
|
||
jdOrder.CardPassword = ""
|
||
jdOrder.Status = int(consts.JdOrderStatusSent)
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, false)
|
||
|
||
// 测试其他状态(已过期)
|
||
jdOrder.Status = int(consts.JdOrderStatusExpired)
|
||
result = s.shouldExtractCard(ctx, jdOrder)
|
||
t.Assert(result, false)
|
||
})
|
||
}
|
||
|
||
// TestBatchCheckPaymentStatusConfig 测试批量检查支付状态的配置常量
|
||
func TestBatchCheckPaymentStatusConfig(t *testing.T) {
|
||
gtest.C(t, func(t *gtest.T) {
|
||
// 验证批量处理的配置常量是否合理
|
||
const (
|
||
BatchSize = 20 // 单次处理订单数量
|
||
CheckInterval = 5 // 检查间隔时间(分钟)
|
||
)
|
||
|
||
t.Assert(BatchSize > 0, true)
|
||
t.Assert(BatchSize <= 50, true) // 确保批量大小合理
|
||
t.Assert(CheckInterval >= 1, true) // 确保检查间隔至少1分钟
|
||
})
|
||
}
|
||
|
||
// TestCacheKeyGeneration 测试缓存键生成
|
||
func TestCacheKeyGeneration(t *testing.T) {
|
||
gtest.C(t, func(t *gtest.T) {
|
||
// 这里我们测试缓存键的格式是否正确
|
||
// 由于我们无法直接访问cache包的PrefixEnum,我们测试格式逻辑
|
||
orderId := "JD123456789"
|
||
|
||
// 模拟缓存键格式
|
||
paymentCheckKey := "jd_payment_check:" + orderId
|
||
cardExtractKey := "jd_card_extract:" + orderId
|
||
|
||
t.Assert(len(paymentCheckKey) > len("jd_payment_check:"), true)
|
||
t.Assert(len(cardExtractKey) > len("jd_card_extract:"), true)
|
||
|
||
// 确保不同的键前缀产生不同的键
|
||
t.AssertNE(paymentCheckKey, cardExtractKey)
|
||
})
|
||
}
|