1166 lines
35 KiB
Go
1166 lines
35 KiB
Go
package services
|
||
|
||
import (
|
||
"context"
|
||
"database/sql"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"testing"
|
||
"time"
|
||
|
||
"quyun/v2/app/commands/testx"
|
||
"quyun/v2/app/errorx"
|
||
"quyun/v2/app/http/tenant/dto"
|
||
"quyun/v2/app/requests"
|
||
"quyun/v2/database"
|
||
"quyun/v2/database/fields"
|
||
"quyun/v2/database/models"
|
||
"quyun/v2/pkg/consts"
|
||
|
||
"github.com/samber/lo"
|
||
. "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"
|
||
)
|
||
|
||
func newLegacyOrderSnapshot() types.JSONType[fields.OrdersSnapshot] {
|
||
return types.NewJSONType(fields.OrdersSnapshot{
|
||
Kind: "legacy",
|
||
Data: json.RawMessage([]byte("{}")),
|
||
})
|
||
}
|
||
|
||
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) {
|
||
now := time.Now().UTC()
|
||
_, err := s.DB.ExecContext(ctx, `
|
||
INSERT INTO users (id, username, password, roles, status, metas, created_at, updated_at, balance, balance_frozen)
|
||
VALUES ($1, $2, 'x', ARRAY['user'], $3, '{}'::jsonb, $4, $4, $5, $6)
|
||
ON CONFLICT (id) DO UPDATE
|
||
SET balance = EXCLUDED.balance, balance_frozen = EXCLUDED.balance_frozen, updated_at = EXCLUDED.updated_at
|
||
`, userID, fmt.Sprintf("u%d", userID), consts.UserStatusVerified, now, balance, frozen)
|
||
So(err, ShouldBeNil)
|
||
|
||
_, err = s.DB.ExecContext(ctx, `
|
||
INSERT INTO tenant_users (tenant_id, user_id, role, status, created_at, updated_at)
|
||
VALUES ($1, $2, ARRAY['member'], $3, $4, $4)
|
||
ON CONFLICT (tenant_id, user_id) DO UPDATE
|
||
SET role = EXCLUDED.role, status = EXCLUDED.status, updated_at = EXCLUDED.updated_at
|
||
`, tenantID, userID, consts.UserStatusVerified, now)
|
||
So(err, 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_MyOrderPage() {
|
||
Convey("Order.MyOrderPage", s.T(), func() {
|
||
ctx := s.T().Context()
|
||
now := time.Now().UTC()
|
||
tenantID := int64(1)
|
||
userID := int64(2)
|
||
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
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)
|
||
})
|
||
|
||
Convey("按 content_id 过滤", func() {
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
OrderID: o1.ID,
|
||
ContentID: 111,
|
||
ContentUserID: 1,
|
||
AmountPaid: 100,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 200,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now.Add(time.Minute),
|
||
CreatedAt: now.Add(time.Minute),
|
||
UpdatedAt: now.Add(time.Minute),
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
OrderID: o2.ID,
|
||
ContentID: 222,
|
||
ContentUserID: 1,
|
||
AmountPaid: 200,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now.Add(time.Minute),
|
||
UpdatedAt: now.Add(time.Minute),
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
pager, err := Order.MyOrderPage(ctx, tenantID, userID, &dto.MyOrderListFilter{
|
||
ContentID: lo.ToPtr(int64(111)),
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
})
|
||
}
|
||
|
||
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()
|
||
now := time.Now().UTC()
|
||
tenantID := int64(1)
|
||
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
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)
|
||
})
|
||
|
||
Convey("按 paid_at 时间窗过滤", func() {
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now.Add(-time.Hour),
|
||
CreatedAt: now.Add(-time.Hour),
|
||
UpdatedAt: now.Add(-time.Hour),
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
OrderID: o1.ID,
|
||
ContentID: 333,
|
||
ContentUserID: 1,
|
||
AmountPaid: 100,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now.Add(-time.Hour),
|
||
UpdatedAt: now.Add(-time.Hour),
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 200,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
OrderID: o2.ID,
|
||
ContentID: 444,
|
||
ContentUserID: 1,
|
||
AmountPaid: 200,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
from := now.Add(-10 * time.Minute)
|
||
to := now.Add(10 * time.Minute)
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
PaidAtFrom: &from,
|
||
PaidAtTo: &to,
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
|
||
Convey("按 content_id 过滤", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
OrderID: o1.ID,
|
||
ContentID: 555,
|
||
ContentUserID: 1,
|
||
AmountPaid: 100,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
ContentID: lo.ToPtr(int64(555)),
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
|
||
Convey("按 username 关键字过滤", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder, models.TableNameUser)
|
||
|
||
u1 := &models.User{
|
||
Username: "alice",
|
||
Password: "x",
|
||
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||
Status: consts.UserStatusVerified,
|
||
Metas: types.JSON([]byte("{}")),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(u1.Create(ctx), ShouldBeNil)
|
||
u2 := &models.User{
|
||
Username: "bob",
|
||
Password: "x",
|
||
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||
Status: consts.UserStatusVerified,
|
||
Metas: types.JSON([]byte("{}")),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(u2.Create(ctx), ShouldBeNil)
|
||
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: u1.ID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: u2.ID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
|
||
username := "ali"
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
Username: &username,
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
items, ok := pager.Items.([]*models.Order)
|
||
So(ok, ShouldBeTrue)
|
||
So(items[0].UserID, ShouldEqual, u1.ID)
|
||
})
|
||
|
||
Convey("按 content_title 关键字过滤", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder, models.TableNameContent)
|
||
|
||
c1 := &models.Content{
|
||
TenantID: tenantID,
|
||
UserID: 1,
|
||
Title: "Go 教程",
|
||
Description: "desc",
|
||
Status: consts.ContentStatusPublished,
|
||
Visibility: consts.ContentVisibilityTenantOnly,
|
||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||
PreviewDownloadable: false,
|
||
PublishedAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(c1.Create(ctx), ShouldBeNil)
|
||
c2 := &models.Content{
|
||
TenantID: tenantID,
|
||
UserID: 1,
|
||
Title: "Rust 教程",
|
||
Description: "desc",
|
||
Status: consts.ContentStatusPublished,
|
||
Visibility: consts.ContentVisibilityTenantOnly,
|
||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||
PreviewDownloadable: false,
|
||
PublishedAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(c2.Create(ctx), ShouldBeNil)
|
||
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
OrderID: o1.ID,
|
||
ContentID: c1.ID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 100,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
OrderID: o2.ID,
|
||
ContentID: c2.ID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 100,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
title := "Go"
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
ContentTitle: &title,
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
|
||
Convey("按 created_at 时间窗过滤", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now.Add(-time.Hour),
|
||
UpdatedAt: now.Add(-time.Hour),
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 200,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
|
||
from := now.Add(-10 * time.Minute)
|
||
to := now.Add(10 * time.Minute)
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
CreatedAtFrom: &from,
|
||
CreatedAtTo: &to,
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
|
||
Convey("按排序字段(asc/desc)排序(白名单)", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
o1 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 2,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 500,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now.Add(-time.Hour),
|
||
UpdatedAt: now.Add(-time.Hour),
|
||
}
|
||
So(o1.Create(ctx), ShouldBeNil)
|
||
|
||
o2 := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 3,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 100,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o2.Create(ctx), ShouldBeNil)
|
||
|
||
asc := "amount_paid"
|
||
pagerAsc, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
SortQueryFilter: requests.SortQueryFilter{Asc: &asc},
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pagerAsc.Total, ShouldEqual, 2)
|
||
itemsAsc, ok := pagerAsc.Items.([]*models.Order)
|
||
So(ok, ShouldBeTrue)
|
||
So(itemsAsc[0].AmountPaid, ShouldEqual, 100)
|
||
|
||
desc := "created_at"
|
||
pagerDesc, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
SortQueryFilter: requests.SortQueryFilter{Desc: &desc},
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pagerDesc.Total, ShouldEqual, 2)
|
||
itemsDesc, ok := pagerDesc.Items.([]*models.Order)
|
||
So(ok, ShouldBeTrue)
|
||
So(itemsDesc[0].CreatedAt.After(itemsDesc[1].CreatedAt), ShouldBeTrue)
|
||
})
|
||
|
||
Convey("组合筛选:user_id + status + amount_paid 区间 + content_id", func() {
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
|
||
|
||
statusPaid := consts.OrderStatusPaid
|
||
userID := int64(7)
|
||
contentID := int64(777)
|
||
|
||
// 命中:user_id=7, status=paid, amount_paid=500, content_id=777
|
||
oHit := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 500,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(oHit.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
OrderID: oHit.ID,
|
||
ContentID: contentID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 500,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
// 不命中:amount_paid 不在区间
|
||
oNoAmount := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 50,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(oNoAmount.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
OrderID: oNoAmount.ID,
|
||
ContentID: contentID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 50,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
// 不命中:status 不同
|
||
oNoStatus := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusCreated,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 500,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(oNoStatus.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: userID,
|
||
OrderID: oNoStatus.ID,
|
||
ContentID: contentID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 500,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
// 不命中:user_id 不同
|
||
oNoUser := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: 8,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 500,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(oNoUser.Create(ctx), ShouldBeNil)
|
||
So((&models.OrderItem{
|
||
TenantID: tenantID,
|
||
UserID: 8,
|
||
OrderID: oNoUser.ID,
|
||
ContentID: contentID,
|
||
ContentUserID: 1,
|
||
AmountPaid: 500,
|
||
Snapshot: types.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}).Create(ctx), ShouldBeNil)
|
||
|
||
min := int64(100)
|
||
max := int64(900)
|
||
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
|
||
UserID: &userID,
|
||
Status: &statusPaid,
|
||
ContentID: &contentID,
|
||
AmountPaidMin: &min,
|
||
AmountPaidMax: &max,
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(pager.Total, ShouldEqual, 1)
|
||
})
|
||
})
|
||
}
|
||
|
||
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_AdminOrderExportCSV() {
|
||
Convey("Order.AdminOrderExportCSV", s.T(), func() {
|
||
ctx := s.T().Context()
|
||
now := time.Now().UTC()
|
||
tenantID := int64(1)
|
||
|
||
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder, models.TableNameUser, models.TableNameContent)
|
||
|
||
Convey("参数非法应返回错误", func() {
|
||
_, err := Order.AdminOrderExportCSV(ctx, 0, &dto.AdminOrderListFilter{})
|
||
So(err, ShouldNotBeNil)
|
||
})
|
||
|
||
Convey("导出应返回 CSV 且包含表头", func() {
|
||
u := &models.User{
|
||
Username: "alice",
|
||
Password: "x",
|
||
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
|
||
Status: consts.UserStatusVerified,
|
||
Metas: types.JSON([]byte("{}")),
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(u.Create(ctx), ShouldBeNil)
|
||
|
||
o := &models.Order{
|
||
TenantID: tenantID,
|
||
UserID: u.ID,
|
||
Type: consts.OrderTypeContentPurchase,
|
||
Status: consts.OrderStatusPaid,
|
||
Currency: consts.CurrencyCNY,
|
||
AmountPaid: 123,
|
||
Snapshot: newLegacyOrderSnapshot(),
|
||
PaidAt: now,
|
||
CreatedAt: now,
|
||
UpdatedAt: now,
|
||
}
|
||
So(o.Create(ctx), ShouldBeNil)
|
||
|
||
resp, err := Order.AdminOrderExportCSV(ctx, tenantID, &dto.AdminOrderListFilter{})
|
||
So(err, ShouldBeNil)
|
||
So(resp, ShouldNotBeNil)
|
||
So(resp.ContentType, ShouldEqual, "text/csv")
|
||
So(resp.Filename, ShouldContainSubstring, "tenant_1_orders_")
|
||
So(resp.CSV, ShouldContainSubstring, "id,tenant_id,user_id,type,status,amount_paid,paid_at,created_at")
|
||
So(resp.CSV, ShouldContainSubstring, "content_purchase")
|
||
})
|
||
})
|
||
}
|
||
|
||
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,
|
||
models.TableNameUser,
|
||
)
|
||
|
||
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: newLegacyOrderSnapshot(),
|
||
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: newLegacyOrderSnapshot(),
|
||
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: newLegacyOrderSnapshot(),
|
||
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.NewJSONType(fields.OrderItemsSnapshot{}),
|
||
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)
|
||
|
||
refunding, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因", "", now.Add(time.Minute))
|
||
So(err, ShouldBeNil)
|
||
So(refunding, ShouldNotBeNil)
|
||
So(refunding.Status, ShouldEqual, consts.OrderStatusRefunding)
|
||
|
||
// refunding 期间重复请求应幂等返回 refunding(并允许重复触发入队,不影响最终结果)。
|
||
refunding2, err := Order.AdminRefundOrder(ctx, tenantID, operatorUserID, orderModel.ID, false, "原因2", "", now.Add(90*time.Second))
|
||
So(err, ShouldBeNil)
|
||
So(refunding2, ShouldNotBeNil)
|
||
So(refunding2.Status, ShouldEqual, consts.OrderStatusRefunding)
|
||
|
||
refunded, err := Order.ProcessRefundingOrder(ctx, &ProcessRefundingOrderParams{
|
||
TenantID: tenantID,
|
||
OrderID: orderModel.ID,
|
||
OperatorUserID: operatorUserID,
|
||
Force: false,
|
||
Reason: "原因",
|
||
Now: now.Add(2 * time.Minute),
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(refunded, ShouldNotBeNil)
|
||
So(refunded.Status, ShouldEqual, consts.OrderStatusRefunded)
|
||
|
||
// worker 重试/重复执行应幂等:不重复入账、不重复回收权益。
|
||
refundedRetry, err := Order.ProcessRefundingOrder(ctx, &ProcessRefundingOrderParams{
|
||
TenantID: tenantID,
|
||
OrderID: orderModel.ID,
|
||
OperatorUserID: operatorUserID,
|
||
Force: false,
|
||
Reason: "原因",
|
||
Now: now.Add(5 * time.Minute),
|
||
})
|
||
So(err, ShouldBeNil)
|
||
So(refundedRetry, ShouldNotBeNil)
|
||
So(refundedRetry.Status, ShouldEqual, consts.OrderStatusRefunded)
|
||
|
||
var u models.User
|
||
So(_db.WithContext(ctx).Where("id = ?", buyerUserID).First(&u).Error, ShouldBeNil)
|
||
So(u.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(3*time.Minute))
|
||
So(err, ShouldBeNil)
|
||
So(refunded2.Status, ShouldEqual, consts.OrderStatusRefunded)
|
||
|
||
var u2 models.User
|
||
So(_db.WithContext(ctx).Where("id = ?", buyerUserID).First(&u2).Error, ShouldBeNil)
|
||
So(u2.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)
|
||
})
|
||
|
||
Convey("不可重试错误分类应稳定", func() {
|
||
So(IsRefundJobNonRetryableError(nil), ShouldBeFalse)
|
||
So(IsRefundJobNonRetryableError(errors.New("x")), ShouldBeFalse)
|
||
|
||
So(IsRefundJobNonRetryableError(errorx.ErrInvalidParameter), ShouldBeTrue)
|
||
So(IsRefundJobNonRetryableError(errorx.ErrRecordNotFound), ShouldBeTrue)
|
||
So(IsRefundJobNonRetryableError(errorx.ErrStatusConflict), ShouldBeTrue)
|
||
So(IsRefundJobNonRetryableError(errorx.ErrPreconditionFailed), ShouldBeTrue)
|
||
So(IsRefundJobNonRetryableError(errorx.ErrPermissionDenied), ShouldBeTrue)
|
||
|
||
So(IsRefundJobNonRetryableError(errorx.ErrInternalError), ShouldBeFalse)
|
||
})
|
||
})
|
||
}
|
||
|
||
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)
|
||
|
||
snap := res1.Order.Snapshot.Data()
|
||
So(snap.Kind, ShouldEqual, string(consts.OrderTypeContentPurchase))
|
||
|
||
var snapData fields.OrdersContentPurchaseSnapshot
|
||
So(json.Unmarshal(snap.Data, &snapData), ShouldBeNil)
|
||
So(snapData.ContentID, ShouldEqual, content.ID)
|
||
So(snapData.ContentTitle, ShouldEqual, content.Title)
|
||
So(snapData.AmountPaid, ShouldEqual, int64(0))
|
||
|
||
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,
|
||
models.TableNameUser,
|
||
)
|
||
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)
|
||
|
||
snap := res1.Order.Snapshot.Data()
|
||
So(snap.Kind, ShouldEqual, string(consts.OrderTypeContentPurchase))
|
||
|
||
var snapData fields.OrdersContentPurchaseSnapshot
|
||
So(json.Unmarshal(snap.Data, &snapData), ShouldBeNil)
|
||
So(snapData.ContentID, ShouldEqual, content.ID)
|
||
So(snapData.AmountPaid, ShouldEqual, int64(300))
|
||
So(snapData.AmountOriginal, ShouldEqual, int64(300))
|
||
|
||
itemSnap := res1.OrderItem.Snapshot.Data()
|
||
So(itemSnap.ContentID, ShouldEqual, content.ID)
|
||
So(itemSnap.AmountPaid, ShouldEqual, int64(300))
|
||
|
||
var u models.User
|
||
So(_db.WithContext(ctx).Where("id = ?", buyerUserID).First(&u).Error, ShouldBeNil)
|
||
So(u.Balance, ShouldEqual, 700)
|
||
So(u.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 u2 models.User
|
||
So(_db.WithContext(ctx).Where("id = ?", buyerUserID).First(&u2).Error, ShouldBeNil)
|
||
So(u2.Balance, ShouldEqual, 700)
|
||
So(u2.BalanceFrozen, ShouldEqual, 0)
|
||
})
|
||
|
||
Convey("存在回滚标记时应稳定返回“失败+已回滚”", func() {
|
||
s.truncate(
|
||
ctx,
|
||
models.TableNameTenantLedger,
|
||
models.TableNameContentAccess,
|
||
models.TableNameOrderItem,
|
||
models.TableNameOrder,
|
||
models.TableNameContentPrice,
|
||
models.TableNameContent,
|
||
models.TableNameTenantUser,
|
||
models.TableNameUser,
|
||
)
|
||
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, "失败+已回滚")
|
||
})
|
||
})
|
||
}
|