修复(#75): 适配促销数量与永久订阅判定

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-27 01:21:25 -07:00
parent b9192db042
commit 0fcd062804
8 changed files with 144 additions and 44 deletions
@@ -11,31 +11,34 @@ import (
)
type fakePromoModel struct {
rules []*promo.RuleWithPrice
rules []*promo.RuleWithPrice
gotQuantity int64
}
func (m fakePromoModel) QueryEligibleRules(context.Context, int64) ([]*promo.RuleWithPrice, error) {
func (m *fakePromoModel) QueryEligibleRules(_ context.Context, _ int64, quantity int64) ([]*promo.RuleWithPrice, error) {
m.gotQuantity = quantity
return m.rules, nil
}
func (m fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
func (m *fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
return nil
}
func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 9,
Name: "campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 600,
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 9,
Name: "campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
}},
PromoPrice: 600,
},
}}
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: model,
}
result, err := calculatePurchasePrice(
@@ -68,22 +71,26 @@ func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
if result.PromoDiscount != 1200 {
t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount)
}
if model.gotQuantity != 3 {
t.Fatalf("promo query quantity = %d, want 3", model.gotQuantity)
}
}
func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 10,
Name: "invalid campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 1000,
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 10,
Name: "invalid campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
}},
PromoPrice: 1000,
},
}}
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: model,
}
result, err := calculatePurchasePrice(