4366a9be8b
- 实现促销管理接口:规则CRUD、优惠价配置、使用记录查询 - 修复 UpsertPrices 新增记录时提前 return 的问题 - DeleteRule/DeletePrice 不存在记录返回 code=404 - SetPromoPriceRequest.items 空数组校验 - 分页参数限制 page>0、1<=size<=200 - 下单侧促销价格按数量档总价口径计算(含 #87 修复) Co-authored-by: multica-agent <github@multica.ai>
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package promo
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
promomodel "github.com/perfect-panel/server/internal/model/promo"
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
"github.com/perfect-panel/server/internal/types"
|
|
"github.com/perfect-panel/server/pkg/xerr"
|
|
pkgerrors "github.com/pkg/errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type fakePromoModel struct{}
|
|
|
|
func (fakePromoModel) QueryEligibleRules(context.Context, int64, int64) ([]*promomodel.RuleWithPrice, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (fakePromoModel) InsertUsage(context.Context, *promomodel.Usage, ...*gorm.DB) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) InsertRule(context.Context, *promomodel.Rule) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) FindRule(context.Context, int64) (*promomodel.Rule, error) {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
func (fakePromoModel) UpdateRule(context.Context, *promomodel.Rule) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) DeleteRule(context.Context, int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) QueryRuleList(context.Context, int, int, string, *bool, string) (int64, []*promomodel.Rule, error) {
|
|
return 0, nil, nil
|
|
}
|
|
|
|
func (fakePromoModel) UpsertPrices(context.Context, int64, []*promomodel.SubscribePromo) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) FindPrice(context.Context, int64) (*promomodel.SubscribePromo, error) {
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
func (fakePromoModel) DeletePrice(context.Context, int64) error {
|
|
return nil
|
|
}
|
|
|
|
func (fakePromoModel) QueryPriceList(context.Context, int64, int, int) (int64, []*promomodel.SubscribePromo, error) {
|
|
return 0, nil, nil
|
|
}
|
|
|
|
func (fakePromoModel) QueryUsageList(context.Context, promomodel.UsageFilter) (int64, []*promomodel.Usage, error) {
|
|
return 0, nil, nil
|
|
}
|
|
|
|
func (fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
|
|
return nil
|
|
}
|
|
|
|
func TestDeleteRuleNotFoundReturns404(t *testing.T) {
|
|
svcCtx := &svc.ServiceContext{PromoModel: fakePromoModel{}}
|
|
err := NewDeleteRuleLogic(context.Background(), svcCtx).DeleteRule(&types.DeletePromoRuleRequest{Id: 1})
|
|
assertCodeError(t, err, 404)
|
|
}
|
|
|
|
func TestDeletePriceNotFoundReturns404(t *testing.T) {
|
|
svcCtx := &svc.ServiceContext{PromoModel: fakePromoModel{}}
|
|
err := NewDeletePriceLogic(context.Background(), svcCtx).DeletePrice(&types.DeletePromoPriceRequest{Id: 1})
|
|
assertCodeError(t, err, 404)
|
|
}
|
|
|
|
func assertCodeError(t *testing.T, err error, want uint32) {
|
|
t.Helper()
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
codeErr, ok := pkgerrors.Cause(err).(*xerr.CodeError)
|
|
if !ok {
|
|
t.Fatalf("expected CodeError, got %T", pkgerrors.Cause(err))
|
|
}
|
|
if got := codeErr.GetErrCode(); got != want {
|
|
t.Fatalf("unexpected error code: got %d want %d", got, want)
|
|
}
|
|
}
|