|
|
|
@@ -2,9 +2,13 @@ package common
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"regexp"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
|
|
|
|
|
|
"github.com/perfect-panel/server/internal/model/promo"
|
|
|
|
|
"github.com/perfect-panel/server/internal/svc"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
@@ -86,6 +90,42 @@ func TestEvaluatePromoAllowsAnonymousCampaign(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEvaluatePromoRejectsInactiveRuleWhenUserHasNoSubscription(t *testing.T) {
|
|
|
|
|
db, mock, cleanup := newCommonPromoTestDB(t)
|
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
|
|
model := &fakePromoModel{rules: []*promo.RuleWithPrice{
|
|
|
|
|
{
|
|
|
|
|
Rule: promo.Rule{
|
|
|
|
|
Id: 9,
|
|
|
|
|
Name: "inactive",
|
|
|
|
|
Type: promo.RuleTypeInactiveUser,
|
|
|
|
|
Params: `{"inactive_months":3}`,
|
|
|
|
|
Enabled: true,
|
|
|
|
|
},
|
|
|
|
|
PromoPrice: 100,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `user_subscribe` WHERE user_id = ? ORDER BY CASE WHEN expire_time = ? THEN 0 ELSE 1 END, expire_time DESC LIMIT ?")).
|
|
|
|
|
WithArgs(int64(51640), time.UnixMilli(0), 1).
|
|
|
|
|
WillReturnError(gorm.ErrRecordNotFound)
|
|
|
|
|
|
|
|
|
|
got, err := EvaluatePromo(context.Background(), &svc.ServiceContext{DB: db, PromoModel: model}, 51640, 1, 30)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("EvaluatePromo returned error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got.Eligible {
|
|
|
|
|
t.Fatal("new user without subscription history should not be eligible for inactive promo")
|
|
|
|
|
}
|
|
|
|
|
if got.RuleID != 0 || got.PromoPrice != 0 {
|
|
|
|
|
t.Fatalf("promo fields = (%d, %d), want zero values", got.RuleID, got.PromoPrice)
|
|
|
|
|
}
|
|
|
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
|
|
|
t.Fatalf("unmet db expectations: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type fakePromoModel struct {
|
|
|
|
|
rules []*promo.RuleWithPrice
|
|
|
|
|
lastSubscribeID int64
|
|
|
|
@@ -145,3 +185,22 @@ func (m *fakePromoModel) QueryUsageList(context.Context, promo.UsageFilter) (int
|
|
|
|
|
func (m *fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCommonPromoTestDB(t *testing.T) (*gorm.DB, sqlmock.Sqlmock, func()) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
sqlDB, mock, err := sqlmock.New()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("create sqlmock: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB, SkipInitializeWithVersion: true}), &gorm.Config{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = sqlDB.Close()
|
|
|
|
|
t.Fatalf("open gorm db: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db, mock, func() {
|
|
|
|
|
_ = sqlDB.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|