Refactor user tests and add content and order tests
- Updated user_test.go to remove FocusConvey and clean up commented-out code. - Introduced content_test.go with comprehensive tests for content creation, updating, pricing, asset attachment, and access checks. - Added order_test.go to implement tests for order management, including admin top-ups, order details, refunds, and content purchases with various scenarios.
This commit is contained in:
244
backend/app/services/content_test.go
Normal file
244
backend/app/services/content_test.go
Normal file
@@ -0,0 +1,244 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/gen/types"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type ContentTestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *sql.DB
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type ContentTestSuite struct {
|
||||
suite.Suite
|
||||
ContentTestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_Content(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p ContentTestSuiteInjectParams) {
|
||||
suite.Run(t, &ContentTestSuite{ContentTestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_Create() {
|
||||
Convey("Content.Create", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameContent)
|
||||
|
||||
Convey("成功创建草稿内容并应用默认策略", func() {
|
||||
form := &dto.ContentCreateForm{
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
}
|
||||
m, err := Content.Create(ctx, tenantID, userID, form)
|
||||
So(err, ShouldBeNil)
|
||||
So(m, ShouldNotBeNil)
|
||||
So(m.TenantID, ShouldEqual, tenantID)
|
||||
So(m.UserID, ShouldEqual, userID)
|
||||
So(m.Status, ShouldEqual, consts.ContentStatusDraft)
|
||||
So(m.Visibility, ShouldEqual, consts.ContentVisibilityTenantOnly)
|
||||
So(m.PreviewSeconds, ShouldEqual, consts.DefaultContentPreviewSeconds)
|
||||
So(m.PreviewDownloadable, ShouldBeFalse)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_Update() {
|
||||
Convey("Content.Update", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameContent)
|
||||
|
||||
m := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusDraft,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
}
|
||||
So(m.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("发布内容应写入 published_at", func() {
|
||||
status := consts.ContentStatusPublished
|
||||
form := &dto.ContentUpdateForm{Status: &status}
|
||||
|
||||
updated, err := Content.Update(ctx, tenantID, userID, m.ID, form)
|
||||
So(err, ShouldBeNil)
|
||||
So(updated, ShouldNotBeNil)
|
||||
So(updated.Status, ShouldEqual, consts.ContentStatusPublished)
|
||||
So(updated.PublishedAt.IsZero(), ShouldBeFalse)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_UpsertPrice() {
|
||||
Convey("Content.UpsertPrice", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameContentPrice, models.TableNameContent)
|
||||
|
||||
content := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusDraft,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
}
|
||||
So(content.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("首次 upsert 应创建价格记录", func() {
|
||||
form := &dto.ContentPriceUpsertForm{
|
||||
PriceAmount: 100,
|
||||
}
|
||||
price, err := Content.UpsertPrice(ctx, tenantID, userID, content.ID, form)
|
||||
So(err, ShouldBeNil)
|
||||
So(price, ShouldNotBeNil)
|
||||
So(price.PriceAmount, ShouldEqual, 100)
|
||||
So(price.Currency, ShouldEqual, consts.CurrencyCNY)
|
||||
So(price.DiscountType, ShouldEqual, consts.DiscountTypeNone)
|
||||
})
|
||||
|
||||
Convey("再次 upsert 应更新价格记录", func() {
|
||||
form1 := &dto.ContentPriceUpsertForm{PriceAmount: 100}
|
||||
_, err := Content.UpsertPrice(ctx, tenantID, userID, content.ID, form1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
form2 := &dto.ContentPriceUpsertForm{
|
||||
PriceAmount: 200,
|
||||
DiscountType: consts.DiscountTypePercent,
|
||||
DiscountValue: 10,
|
||||
}
|
||||
price2, err := Content.UpsertPrice(ctx, tenantID, userID, content.ID, form2)
|
||||
So(err, ShouldBeNil)
|
||||
So(price2.PriceAmount, ShouldEqual, 200)
|
||||
So(price2.DiscountType, ShouldEqual, consts.DiscountTypePercent)
|
||||
So(price2.DiscountValue, ShouldEqual, 10)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_AttachAsset() {
|
||||
Convey("Content.AttachAsset", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameContentAsset, models.TableNameMediaAsset, models.TableNameContent)
|
||||
|
||||
content := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusDraft,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
}
|
||||
So(content.Create(ctx), ShouldBeNil)
|
||||
|
||||
asset := &models.MediaAsset{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetTypeVideo,
|
||||
Status: consts.MediaAssetStatusReady,
|
||||
Provider: "test",
|
||||
Bucket: "bucket",
|
||||
ObjectKey: "obj",
|
||||
Meta: types.JSON([]byte("{}")),
|
||||
}
|
||||
So(asset.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("成功绑定资源到内容", func() {
|
||||
m, err := Content.AttachAsset(ctx, tenantID, userID, content.ID, asset.ID, consts.ContentAssetRoleMain, 1, now)
|
||||
So(err, ShouldBeNil)
|
||||
So(m, ShouldNotBeNil)
|
||||
So(m.ContentID, ShouldEqual, content.ID)
|
||||
So(m.AssetID, ShouldEqual, asset.ID)
|
||||
So(m.Role, ShouldEqual, consts.ContentAssetRoleMain)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_HasAccess() {
|
||||
Convey("Content.HasAccess", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB, models.TableNameContentAccess, models.TableNameContent)
|
||||
|
||||
content := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
}
|
||||
So(content.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("未授予权益应返回 false", func() {
|
||||
ok, err := Content.HasAccess(ctx, tenantID, userID, content.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(ok, ShouldBeFalse)
|
||||
})
|
||||
|
||||
Convey("权益 active 应返回 true", func() {
|
||||
access := &models.ContentAccess{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
ContentID: content.ID,
|
||||
OrderID: 0,
|
||||
Status: consts.ContentAccessStatusActive,
|
||||
RevokedAt: time.Time{},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(access.Create(ctx), ShouldBeNil)
|
||||
|
||||
ok, err := Content.HasAccess(ctx, tenantID, userID, content.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(ok, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -66,6 +66,15 @@ func (s *LedgerTestSuite) Test_Freeze() {
|
||||
|
||||
s.seedTenantUser(ctx, tenantID, userID, 1000, 0)
|
||||
|
||||
Convey("金额非法应返回参数错误", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 0, "k_freeze_invalid_amount", "freeze", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
var appErr *errorx.AppError
|
||||
So(errors.As(err, &appErr), ShouldBeTrue)
|
||||
So(appErr.Code, ShouldEqual, errorx.CodeInvalidParameter)
|
||||
})
|
||||
|
||||
Convey("成功冻结", func() {
|
||||
res, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_1", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
@@ -109,7 +118,7 @@ func (s *LedgerTestSuite) Test_Freeze() {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LedgerTestSuite) Test_Unfreeze_InsufficientFrozen() {
|
||||
func (s *LedgerTestSuite) Test_Unfreeze() {
|
||||
Convey("Ledger.Unfreeze", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
@@ -118,6 +127,11 @@ func (s *LedgerTestSuite) Test_Unfreeze_InsufficientFrozen() {
|
||||
|
||||
s.seedTenantUser(ctx, tenantID, userID, 1000, 0)
|
||||
|
||||
Convey("金额非法应返回参数错误", func() {
|
||||
_, err := Ledger.Unfreeze(ctx, tenantID, userID, 0, 0, "k_unfreeze_invalid_amount", "unfreeze", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("冻结余额不足应返回前置条件失败", func() {
|
||||
_, err := Ledger.Unfreeze(ctx, tenantID, userID, 0, 999999, "k_unfreeze_over", "unfreeze", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
@@ -138,53 +152,123 @@ func (s *LedgerTestSuite) Test_Unfreeze_InsufficientFrozen() {
|
||||
So(res.TenantUser.Balance, ShouldEqual, 1000)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("幂等键重复调用不应重复入账", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_unfreeze_idem", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, err = Ledger.Unfreeze(ctx, tenantID, userID, 0, 300, "k_unfreeze_idem", "unfreeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res2, err := Ledger.Unfreeze(ctx, tenantID, userID, 0, 300, "k_unfreeze_idem", "unfreeze", now.Add(time.Second))
|
||||
So(err, ShouldBeNil)
|
||||
So(res2, ShouldNotBeNil)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, userID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 1000)
|
||||
So(tu2.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LedgerTestSuite) Test_DebitPurchaseTx() {
|
||||
Convey("Ledger.DebitPurchaseTx 应减少冻结余额并保留可用余额不变", s.T(), func() {
|
||||
Convey("Ledger.DebitPurchaseTx", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
now := time.Now().UTC()
|
||||
|
||||
s.seedTenantUser(ctx, tenantID, userID, 1000, 0)
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_debit", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_1", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeDebitPurchase)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 700)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
Convey("金额非法应返回参数错误", func() {
|
||||
_, err := Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 0, "k_debit_invalid_amount", "debit", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("冻结余额不足应返回前置条件失败", func() {
|
||||
_, err := Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_no_frozen", "debit", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("成功扣款应减少冻结余额并保持可用余额不变", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_debit", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_1", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeDebitPurchase)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 700)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("幂等键重复调用不应重复扣减冻结余额", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_debit_idem", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, err = Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_idem", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, err = Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_idem", "debit", now.Add(time.Second))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, userID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 700)
|
||||
So(tu2.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LedgerTestSuite) Test_CreditRefundTx() {
|
||||
Convey("Ledger.CreditRefundTx 应回滚可用余额并保持冻结余额不变", s.T(), func() {
|
||||
Convey("Ledger.CreditRefundTx", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
now := time.Now().UTC()
|
||||
|
||||
s.seedTenantUser(ctx, tenantID, userID, 1000, 0)
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_refund", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
_, err = Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_for_refund", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := Ledger.CreditRefundTx(ctx, _db, tenantID, userID, 123, 300, "k_refund_1", "refund", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeCreditRefund)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 1000)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
Convey("金额非法应返回参数错误", func() {
|
||||
_, err := Ledger.CreditRefundTx(ctx, _db, tenantID, userID, 123, 0, "k_refund_invalid_amount", "refund", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("成功退款应增加可用余额", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_refund", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
_, err = Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_for_refund", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
res, err := Ledger.CreditRefundTx(ctx, _db, tenantID, userID, 123, 300, "k_refund_1", "refund", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeCreditRefund)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 1000)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("幂等键重复调用不应重复退款入账", func() {
|
||||
_, err := Ledger.Freeze(ctx, tenantID, userID, 0, 300, "k_freeze_for_refund_idem", "freeze", now)
|
||||
So(err, ShouldBeNil)
|
||||
_, err = Ledger.DebitPurchaseTx(ctx, _db, tenantID, userID, 123, 300, "k_debit_for_refund_idem", "debit", now)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, err = Ledger.CreditRefundTx(ctx, _db, tenantID, userID, 123, 300, "k_refund_idem", "refund", now)
|
||||
So(err, ShouldBeNil)
|
||||
_, err = Ledger.CreditRefundTx(ctx, _db, tenantID, userID, 123, 300, "k_refund_idem", "refund", now.Add(time.Second))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, userID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 1000)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *LedgerTestSuite) Test_CreditTopupTx() {
|
||||
Convey("Ledger.CreditTopupTx 应增加可用余额并写入 credit_topup", s.T(), func() {
|
||||
Convey("Ledger.CreditTopupTx", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
@@ -192,11 +276,29 @@ func (s *LedgerTestSuite) Test_CreditTopupTx() {
|
||||
|
||||
s.seedTenantUser(ctx, tenantID, userID, 1000, 0)
|
||||
|
||||
res, err := Ledger.CreditTopupTx(ctx, _db, tenantID, userID, 456, 200, "k_topup_1", "topup", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeCreditTopup)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 1200)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
Convey("金额非法应返回参数错误", func() {
|
||||
_, err := Ledger.CreditTopupTx(ctx, _db, tenantID, userID, 456, 0, "k_topup_invalid_amount", "topup", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("成功充值应增加可用余额并写入账本", func() {
|
||||
res, err := Ledger.CreditTopupTx(ctx, _db, tenantID, userID, 456, 200, "k_topup_1", "topup", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeNil)
|
||||
So(res.Ledger.Type, ShouldEqual, consts.TenantLedgerTypeCreditTopup)
|
||||
So(res.TenantUser.Balance, ShouldEqual, 1200)
|
||||
So(res.TenantUser.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("幂等键重复调用不应重复充值入账", func() {
|
||||
_, err := Ledger.CreditTopupTx(ctx, _db, tenantID, userID, 456, 200, "k_topup_idem", "topup", now)
|
||||
So(err, ShouldBeNil)
|
||||
_, err = Ledger.CreditTopupTx(ctx, _db, tenantID, userID, 456, 200, "k_topup_idem", "topup", now.Add(time.Second))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, userID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 1200)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
586
backend/app/services/order_test.go
Normal file
586
backend/app/services/order_test.go
Normal file
@@ -0,0 +1,586 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/gen/types"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type OrderTestSuiteInjectParams struct {
|
||||
dig.In
|
||||
|
||||
DB *sql.DB
|
||||
Initials []contracts.Initial `group:"initials"` // nolint:structcheck
|
||||
}
|
||||
|
||||
type OrderTestSuite struct {
|
||||
suite.Suite
|
||||
OrderTestSuiteInjectParams
|
||||
}
|
||||
|
||||
func Test_Order(t *testing.T) {
|
||||
providers := testx.Default().With(Provide)
|
||||
|
||||
testx.Serve(providers, t, func(p OrderTestSuiteInjectParams) {
|
||||
suite.Run(t, &OrderTestSuite{OrderTestSuiteInjectParams: p})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) truncate(ctx context.Context, tableNames ...string) {
|
||||
database.Truncate(ctx, s.DB, tableNames...)
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) seedTenantUser(ctx context.Context, tenantID, userID, balance, frozen int64) {
|
||||
tu := &models.TenantUser{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
Role: types.NewArray([]consts.TenantUserRole{consts.TenantUserRoleMember}),
|
||||
Balance: balance,
|
||||
BalanceFrozen: frozen,
|
||||
Status: consts.UserStatusVerified,
|
||||
}
|
||||
So(tu.Create(ctx), ShouldBeNil)
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) seedPublishedContent(ctx context.Context, tenantID, ownerUserID int64) *models.Content {
|
||||
m := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerUserID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: time.Now().UTC(),
|
||||
}
|
||||
So(m.Create(ctx), ShouldBeNil)
|
||||
return m
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) seedContentPrice(ctx context.Context, tenantID, contentID, priceAmount int64) {
|
||||
p := &models.ContentPrice{
|
||||
TenantID: tenantID,
|
||||
UserID: 1,
|
||||
ContentID: contentID,
|
||||
Currency: consts.CurrencyCNY,
|
||||
PriceAmount: priceAmount,
|
||||
DiscountType: consts.DiscountTypeNone,
|
||||
DiscountValue: 0,
|
||||
DiscountStartAt: time.Time{},
|
||||
DiscountEndAt: time.Time{},
|
||||
}
|
||||
So(p.Create(ctx), ShouldBeNil)
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_AdminTopupUser() {
|
||||
Convey("Order.AdminTopupUser", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
operatorUserID := int64(10)
|
||||
targetUserID := int64(20)
|
||||
|
||||
s.truncate(
|
||||
ctx,
|
||||
models.TableNameTenantLedger,
|
||||
models.TableNameOrderItem,
|
||||
models.TableNameOrder,
|
||||
models.TableNameTenantUser,
|
||||
)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.AdminTopupUser(ctx, 0, operatorUserID, targetUserID, 100, "", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
_, err = Order.AdminTopupUser(ctx, tenantID, 0, targetUserID, 100, "", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
_, err = Order.AdminTopupUser(ctx, tenantID, operatorUserID, 0, 100, "", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
_, err = Order.AdminTopupUser(ctx, tenantID, operatorUserID, targetUserID, 0, "", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("目标用户不属于该租户应返回前置条件失败", func() {
|
||||
_, err := Order.AdminTopupUser(ctx, tenantID, operatorUserID, targetUserID, 100, "idem_not_member", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
var appErr *errorx.AppError
|
||||
So(errors.As(err, &appErr), ShouldBeTrue)
|
||||
So(appErr.Code, ShouldEqual, errorx.CodePreconditionFailed)
|
||||
})
|
||||
|
||||
Convey("成功充值并写入账本", func() {
|
||||
s.seedTenantUser(ctx, tenantID, targetUserID, 0, 0)
|
||||
|
||||
orderModel, err := Order.AdminTopupUser(ctx, tenantID, operatorUserID, targetUserID, 300, "idem_topup_1", "充值原因", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(orderModel, ShouldNotBeNil)
|
||||
So(orderModel.ID, ShouldBeGreaterThan, 0)
|
||||
So(orderModel.Type, ShouldEqual, consts.OrderTypeTopup)
|
||||
So(orderModel.Status, ShouldEqual, consts.OrderStatusPaid)
|
||||
So(orderModel.AmountPaid, ShouldEqual, 300)
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, targetUserID).First(&tu).Error, ShouldBeNil)
|
||||
So(tu.Balance, ShouldEqual, 300)
|
||||
|
||||
var ledgers []*models.TenantLedger
|
||||
So(_db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND user_id = ? AND type = ?", tenantID, targetUserID, consts.TenantLedgerTypeCreditTopup).
|
||||
Order("id ASC").
|
||||
Find(&ledgers).Error, ShouldBeNil)
|
||||
So(len(ledgers), ShouldEqual, 1)
|
||||
So(ledgers[0].OrderID, ShouldEqual, orderModel.ID)
|
||||
So(ledgers[0].Amount, ShouldEqual, 300)
|
||||
})
|
||||
|
||||
Convey("幂等键重复调用不应重复入账", func() {
|
||||
s.seedTenantUser(ctx, tenantID, targetUserID, 0, 0)
|
||||
|
||||
o1, err := Order.AdminTopupUser(ctx, tenantID, operatorUserID, targetUserID, 300, "idem_topup_2", "充值原因", now)
|
||||
So(err, ShouldBeNil)
|
||||
So(o1, ShouldNotBeNil)
|
||||
|
||||
o2, err := Order.AdminTopupUser(ctx, tenantID, operatorUserID, targetUserID, 999, "idem_topup_2", "不同金额也不应重复处理", now.Add(time.Second))
|
||||
So(err, ShouldBeNil)
|
||||
So(o2, ShouldNotBeNil)
|
||||
So(o2.ID, ShouldEqual, o1.ID)
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, targetUserID).First(&tu).Error, ShouldBeNil)
|
||||
So(tu.Balance, ShouldEqual, 300)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_MyOrderPage() {
|
||||
Convey("Order.MyOrderPage", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
s.truncate(ctx, models.TableNameOrder, models.TableNameOrderItem)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.MyOrderPage(ctx, 0, userID, &dto.MyOrderListFilter{})
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("空数据应返回 total=0", func() {
|
||||
pager, err := Order.MyOrderPage(ctx, tenantID, userID, &dto.MyOrderListFilter{})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_MyOrderDetail() {
|
||||
Convey("Order.MyOrderDetail", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
userID := int64(2)
|
||||
|
||||
s.truncate(ctx, models.TableNameOrder, models.TableNameOrderItem)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.MyOrderDetail(ctx, 0, userID, 1)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("订单不存在应返回错误", func() {
|
||||
_, err := Order.MyOrderDetail(ctx, tenantID, userID, 999)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_AdminOrderPage() {
|
||||
Convey("Order.AdminOrderPage", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
|
||||
s.truncate(ctx, models.TableNameOrder, models.TableNameOrderItem)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.AdminOrderPage(ctx, 0, &dto.AdminOrderListFilter{})
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("空数据应返回 total=0", func() {
|
||||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_AdminOrderDetail() {
|
||||
Convey("Order.AdminOrderDetail", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
tenantID := int64(1)
|
||||
|
||||
s.truncate(ctx, models.TableNameOrder, models.TableNameOrderItem)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.AdminOrderDetail(ctx, 0, 1)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("订单不存在应返回错误", func() {
|
||||
_, err := Order.AdminOrderDetail(ctx, tenantID, 999)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_AdminRefundOrder() {
|
||||
Convey("Order.AdminRefundOrder", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
operatorUserID := int64(10)
|
||||
buyerUserID := int64(20)
|
||||
|
||||
s.truncate(
|
||||
ctx,
|
||||
models.TableNameTenantLedger,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameOrderItem,
|
||||
models.TableNameOrder,
|
||||
models.TableNameTenantUser,
|
||||
)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.AdminRefundOrder(ctx, 0, operatorUserID, 1, false, "", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("订单非已支付状态应返回状态冲突", func() {
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 0, 0)
|
||||
|
||||
orderModel := &models.Order{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
Type: consts.OrderTypeContentPurchase,
|
||||
Status: consts.OrderStatusCreated,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountOriginal: 100,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(orderModel.Create(ctx), ShouldBeNil)
|
||||
|
||||
_, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
var appErr *errorx.AppError
|
||||
So(errors.As(err, &appErr), ShouldBeTrue)
|
||||
So(appErr.Code, ShouldEqual, errorx.CodeStatusConflict)
|
||||
})
|
||||
|
||||
Convey("已超过默认退款时间窗且非强制应失败", func() {
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 0, 0)
|
||||
|
||||
orderModel := &models.Order{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
Type: consts.OrderTypeContentPurchase,
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountOriginal: 100,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 100,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
PaidAt: now.Add(-consts.DefaultOrderRefundWindow).Add(-time.Second),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(orderModel.Create(ctx), ShouldBeNil)
|
||||
|
||||
_, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因", "", now)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("成功退款应回收权益并入账", func() {
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 0, 0)
|
||||
|
||||
contentID := int64(123)
|
||||
orderModel := &models.Order{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
Type: consts.OrderTypeContentPurchase,
|
||||
Status: consts.OrderStatusPaid,
|
||||
Currency: consts.CurrencyCNY,
|
||||
AmountOriginal: 300,
|
||||
AmountDiscount: 0,
|
||||
AmountPaid: 300,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
PaidAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(orderModel.Create(ctx), ShouldBeNil)
|
||||
|
||||
item := &models.OrderItem{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
OrderID: orderModel.ID,
|
||||
ContentID: contentID,
|
||||
ContentUserID: 999,
|
||||
AmountPaid: 300,
|
||||
Snapshot: types.JSON([]byte("{}")),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(item.Create(ctx), ShouldBeNil)
|
||||
|
||||
access := &models.ContentAccess{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: contentID,
|
||||
OrderID: orderModel.ID,
|
||||
Status: consts.ContentAccessStatusActive,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
RevokedAt: time.Time{},
|
||||
}
|
||||
So(access.Create(ctx), ShouldBeNil)
|
||||
|
||||
refunded, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因", "", now.Add(time.Minute))
|
||||
So(err, ShouldBeNil)
|
||||
So(refunded, ShouldNotBeNil)
|
||||
So(refunded.Status, ShouldEqual, consts.OrderStatusRefunded)
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, buyerUserID).First(&tu).Error, ShouldBeNil)
|
||||
So(tu.Balance, ShouldEqual, 300)
|
||||
|
||||
var access2 models.ContentAccess
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ? AND content_id = ?", tenantID, buyerUserID, contentID).First(&access2).Error, ShouldBeNil)
|
||||
So(access2.Status, ShouldEqual, consts.ContentAccessStatusRevoked)
|
||||
So(access2.RevokedAt.IsZero(), ShouldBeFalse)
|
||||
|
||||
refunded2, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因2", "", now.Add(2*time.Minute))
|
||||
So(err, ShouldBeNil)
|
||||
So(refunded2.Status, ShouldEqual, consts.OrderStatusRefunded)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, buyerUserID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 300)
|
||||
|
||||
var ledgers []*models.TenantLedger
|
||||
So(_db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND user_id = ? AND idempotency_key = ?", tenantID, buyerUserID, fmt.Sprintf("refund:%d", orderModel.ID)).
|
||||
Find(&ledgers).Error, ShouldBeNil)
|
||||
So(len(ledgers), ShouldEqual, 1)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *OrderTestSuite) Test_PurchaseContent() {
|
||||
Convey("Order.PurchaseContent", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
ownerUserID := int64(100)
|
||||
buyerUserID := int64(200)
|
||||
|
||||
s.truncate(
|
||||
ctx,
|
||||
models.TableNameTenantLedger,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameOrderItem,
|
||||
models.TableNameOrder,
|
||||
models.TableNameContentPrice,
|
||||
models.TableNameContent,
|
||||
models.TableNameTenantUser,
|
||||
)
|
||||
|
||||
Convey("参数非法应返回错误", func() {
|
||||
_, err := Order.PurchaseContent(ctx, nil)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
_, err = Order.PurchaseContent(ctx, &PurchaseContentParams{TenantID: 0, UserID: 1, ContentID: 1})
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("内容未发布应返回前置条件失败", func() {
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 1000, 0)
|
||||
|
||||
content := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerUserID,
|
||||
Title: "标题",
|
||||
Description: "描述",
|
||||
Status: consts.ContentStatusDraft,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
}
|
||||
So(content.Create(ctx), ShouldBeNil)
|
||||
|
||||
_, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_not_published",
|
||||
Now: now,
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
var appErr *errorx.AppError
|
||||
So(errors.As(err, &appErr), ShouldBeTrue)
|
||||
So(appErr.Code, ShouldEqual, errorx.CodePreconditionFailed)
|
||||
})
|
||||
|
||||
Convey("免费内容购买应创建订单并授予权益(幂等)", func() {
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 1000, 0)
|
||||
content := s.seedPublishedContent(ctx, tenantID, ownerUserID)
|
||||
|
||||
res1, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_free_1",
|
||||
Now: now,
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res1, ShouldNotBeNil)
|
||||
So(res1.AmountPaid, ShouldEqual, 0)
|
||||
So(res1.Order, ShouldNotBeNil)
|
||||
So(res1.Order.Status, ShouldEqual, consts.OrderStatusPaid)
|
||||
So(res1.Access, ShouldNotBeNil)
|
||||
So(res1.Access.Status, ShouldEqual, consts.ContentAccessStatusActive)
|
||||
|
||||
res2, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_free_1",
|
||||
Now: now.Add(time.Second),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res2.Order.ID, ShouldEqual, res1.Order.ID)
|
||||
})
|
||||
|
||||
Convey("付费内容购买应冻结+扣款并授予权益(幂等)", func() {
|
||||
s.truncate(
|
||||
ctx,
|
||||
models.TableNameTenantLedger,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameOrderItem,
|
||||
models.TableNameOrder,
|
||||
models.TableNameContentPrice,
|
||||
models.TableNameContent,
|
||||
models.TableNameTenantUser,
|
||||
)
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 1000, 0)
|
||||
content := s.seedPublishedContent(ctx, tenantID, ownerUserID)
|
||||
s.seedContentPrice(ctx, tenantID, content.ID, 300)
|
||||
|
||||
res1, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_paid_1",
|
||||
Now: now,
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res1, ShouldNotBeNil)
|
||||
So(res1.AmountPaid, ShouldEqual, 300)
|
||||
So(res1.Order, ShouldNotBeNil)
|
||||
So(res1.Order.Status, ShouldEqual, consts.OrderStatusPaid)
|
||||
So(res1.Access, ShouldNotBeNil)
|
||||
So(res1.Access.Status, ShouldEqual, consts.ContentAccessStatusActive)
|
||||
|
||||
var tu models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, buyerUserID).First(&tu).Error, ShouldBeNil)
|
||||
So(tu.Balance, ShouldEqual, 700)
|
||||
So(tu.BalanceFrozen, ShouldEqual, 0)
|
||||
|
||||
res2, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_paid_1",
|
||||
Now: now.Add(2 * time.Second),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res2.Order.ID, ShouldEqual, res1.Order.ID)
|
||||
|
||||
var tu2 models.TenantUser
|
||||
So(_db.WithContext(ctx).Where("tenant_id = ? AND user_id = ?", tenantID, buyerUserID).First(&tu2).Error, ShouldBeNil)
|
||||
So(tu2.Balance, ShouldEqual, 700)
|
||||
So(tu2.BalanceFrozen, ShouldEqual, 0)
|
||||
})
|
||||
|
||||
Convey("存在回滚标记时应稳定返回“失败+已回滚”", func() {
|
||||
s.truncate(
|
||||
ctx,
|
||||
models.TableNameTenantLedger,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameOrderItem,
|
||||
models.TableNameOrder,
|
||||
models.TableNameContentPrice,
|
||||
models.TableNameContent,
|
||||
models.TableNameTenantUser,
|
||||
)
|
||||
s.seedTenantUser(ctx, tenantID, buyerUserID, 1000, 0)
|
||||
content := s.seedPublishedContent(ctx, tenantID, ownerUserID)
|
||||
s.seedContentPrice(ctx, tenantID, content.ID, 300)
|
||||
|
||||
rollbackKey := "idem_rollback_1:rollback"
|
||||
ledger := &models.TenantLedger{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
OrderID: 0,
|
||||
Type: consts.TenantLedgerTypeUnfreeze,
|
||||
Amount: 1,
|
||||
BalanceBefore: 0,
|
||||
BalanceAfter: 0,
|
||||
FrozenBefore: 0,
|
||||
FrozenAfter: 0,
|
||||
IdempotencyKey: rollbackKey,
|
||||
Remark: "rollback marker",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(ledger.Create(ctx), ShouldBeNil)
|
||||
|
||||
_, err := Order.PurchaseContent(ctx, &PurchaseContentParams{
|
||||
TenantID: tenantID,
|
||||
UserID: buyerUserID,
|
||||
ContentID: content.ID,
|
||||
IdempotencyKey: "idem_rollback_1",
|
||||
Now: now.Add(time.Second),
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err.Error(), ShouldContainSubstring, "失败+已回滚")
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func (t *UserTestSuite) Test_FindByUsername() {
|
||||
|
||||
// Test_Page
|
||||
func (t *UserTestSuite) Test_Page() {
|
||||
FocusConvey("test page", t.T(), func() {
|
||||
Convey("test page", t.T(), func() {
|
||||
Convey("filter username", func() {
|
||||
database.Truncate(t.T().Context(), t.DB, models.TableNameUser)
|
||||
|
||||
@@ -124,7 +124,7 @@ func (t *UserTestSuite) Test_Page() {
|
||||
So(pager.Total, ShouldEqual, 1)
|
||||
})
|
||||
|
||||
FocusConvey("filter tenant users", func() {
|
||||
Convey("filter tenant users", func() {
|
||||
database.Truncate(
|
||||
t.T().Context(),
|
||||
t.DB,
|
||||
@@ -205,53 +205,6 @@ func (t *UserTestSuite) Test_Page() {
|
||||
|
||||
func (t *UserTestSuite) Test_Relations() {
|
||||
Convey("test page", t.T(), func() {
|
||||
// database.Truncate(
|
||||
// t.T().Context(),
|
||||
// t.DB,
|
||||
// models.TableNameUser,
|
||||
// models.TableNameTenant,
|
||||
// models.TableNameTenantUser,
|
||||
// )
|
||||
|
||||
// username := "test-user"
|
||||
// mUser01 := &models.User{
|
||||
// Username: username,
|
||||
// Password: "test-password",
|
||||
// Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||||
// Status: consts.UserStatusPendingVerify,
|
||||
// }
|
||||
|
||||
// err := mUser01.Create(t.T().Context())
|
||||
// So(err, ShouldBeNil)
|
||||
|
||||
// tenantModel := &models.Tenant{
|
||||
// UserID: 1,
|
||||
// Code: "abc",
|
||||
// UUID: types.NewUUIDv4(),
|
||||
// Name: "T01",
|
||||
// Status: consts.TenantStatusVerified,
|
||||
// }
|
||||
|
||||
// err = tenantModel.Create(t.T().Context())
|
||||
// So(err, ShouldBeNil)
|
||||
|
||||
// count := 10
|
||||
// for i := 0; i < count; i++ {
|
||||
// mUser := &models.User{
|
||||
// Username: fmt.Sprintf("user_%d", i),
|
||||
// Password: "test-password",
|
||||
// Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||||
// Status: consts.UserStatusPendingVerify,
|
||||
// }
|
||||
|
||||
// err = mUser.Create(t.T().Context())
|
||||
// So(err, ShouldBeNil)
|
||||
|
||||
// // create tenant user
|
||||
// err = Tenant.AddUser(t.T().Context(), 1, mUser.ID)
|
||||
// So(err, ShouldBeNil)
|
||||
// }
|
||||
|
||||
Convey("filter tenant users", func() {
|
||||
m, err := User.FindByID(t.T().Context(), 1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Reference in New Issue
Block a user