172 lines
5.3 KiB
Go
172 lines
5.3 KiB
Go
package order
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
"hfb_sys/backend/internal/tenancy"
|
|
|
|
"gorm.io/datatypes"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func orderTenantContext(tenantID uint64) context.Context {
|
|
return tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: tenantID})
|
|
}
|
|
|
|
func createOrderTenantListing(t *testing.T, db *gorm.DB, tenantIDs ...uint64) (*model.RentalListing, []model.User) {
|
|
t.Helper()
|
|
var tenants []model.Tenant
|
|
for index, tenantID := range tenantIDs {
|
|
tenant := model.Tenant{
|
|
Code: "tenant_" + string(rune('a'+index)),
|
|
Name: "租户",
|
|
Status: "active",
|
|
}
|
|
tenant.ID = tenantID
|
|
if err := db.Create(&tenant).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tenants = append(tenants, tenant)
|
|
}
|
|
owner := model.User{TenantID: tenants[0].ID, Phone: "13000000001", Status: "active"}
|
|
if err := db.Create(&owner).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
account := model.GameAccount{
|
|
SourceTenantID: tenants[0].ID, OwnerID: owner.ID, ServerRegion: "国服",
|
|
LoginPlatform: "steam", Title: "跨租户商品", Status: "published",
|
|
AssetSummary: datatypes.JSON([]byte(`{
|
|
"price_breakdown": {
|
|
"seller_total_price": 80,
|
|
"platform_markup_amount": 20
|
|
}
|
|
}`)),
|
|
}
|
|
if err := db.Create(&account).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listing := model.RentalListing{
|
|
SourceTenantID: tenants[0].ID, ListingNo: "L-ORDER-TENANT-1",
|
|
AccountID: account.ID, OwnerID: owner.ID, PriceCent: 10000,
|
|
Status: listingStatusPublished, ReviewStatus: listingReviewStatusApproved,
|
|
}
|
|
if err := db.Create(&listing).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for index, tenantID := range tenantIDs {
|
|
var price *int64
|
|
if index > 0 {
|
|
value := int64(10000 + index*2000)
|
|
price = &value
|
|
}
|
|
if err := db.Create(&model.TenantListingOffer{
|
|
TenantID: tenantID, ListingID: listing.ID, DisplayStatus: "enabled", PriceCent: price,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
renters := make([]model.User, 0, len(tenantIDs)-1)
|
|
for index, tenantID := range tenantIDs[1:] {
|
|
renter := model.User{
|
|
TenantID: tenantID,
|
|
Phone: "1300000001" + string(rune('0'+index+1)),
|
|
Status: "active",
|
|
}
|
|
if err := db.Create(&renter).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
renters = append(renters, renter)
|
|
}
|
|
return &listing, renters
|
|
}
|
|
|
|
func TestCreateOrderUsesBuyerTenantOfferAndSnapshotsTenantRoles(t *testing.T) {
|
|
db := setupOrderTestDB(t)
|
|
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
|
repo := NewRepository(db)
|
|
|
|
noOfferTenant := model.Tenant{ID: 404, Code: "tenant_no_offer", Name: "无货架租户", Status: "active"}
|
|
if err := db.Create(&noOfferTenant).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
noOfferRenter := model.User{TenantID: noOfferTenant.ID, Phone: "13000000019", Status: "active"}
|
|
if err := db.Create(&noOfferRenter).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := repo.Create(orderTenantContext(noOfferTenant.ID), noOfferRenter.ID, CreateRequest{ListingID: listing.ID}); !errors.Is(err, ErrListingUnavailable) {
|
|
t.Fatalf("tenant without offer error = %v, want ErrListingUnavailable", err)
|
|
}
|
|
dto, err := repo.Create(orderTenantContext(202), renters[0].ID, CreateRequest{ListingID: listing.ID})
|
|
if err != nil {
|
|
t.Fatalf("create cross-tenant order: %v", err)
|
|
}
|
|
if dto.BuyerTenantID != 202 || dto.SourceTenantID != 101 || dto.PaymentTenantID != 202 {
|
|
t.Fatalf("order tenant snapshots = buyer:%d source:%d payment:%d",
|
|
dto.BuyerTenantID, dto.SourceTenantID, dto.PaymentTenantID)
|
|
}
|
|
if dto.BasePriceCent != 12000 || dto.ShelfMarkupCent != 0 {
|
|
t.Fatalf("offer price snapshot = base:%d markup:%d", dto.BasePriceCent, dto.ShelfMarkupCent)
|
|
}
|
|
if dto.SourceAmountCent != 8000 {
|
|
t.Fatalf("source amount snapshot = %d, want 8000", dto.SourceAmountCent)
|
|
}
|
|
var saved model.RentalOrder
|
|
if err := db.First(&saved, dto.ID).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if saved.BuyerTenantID != 202 || saved.SourceTenantID != 101 || saved.PaymentTenantID != 202 {
|
|
t.Fatalf("saved tenant roles = %+v", saved)
|
|
}
|
|
if saved.OwnerRentAmountCent != saved.SourceAmountCent {
|
|
t.Fatalf("source amount = %d, owner rent = %d", saved.SourceAmountCent, saved.OwnerRentAmountCent)
|
|
}
|
|
}
|
|
|
|
func TestConcurrentTenantsCanOnlyRentListingOnce(t *testing.T) {
|
|
db := setupOrderTestDB(t)
|
|
listing, renters := createOrderTenantListing(t, db, 101, 202, 303)
|
|
repo := NewRepository(db)
|
|
|
|
start := make(chan struct{})
|
|
results := make(chan error, 2)
|
|
var wg sync.WaitGroup
|
|
for index, tenantID := range []uint64{202, 303} {
|
|
wg.Add(1)
|
|
go func(renterID uint64, tenantID uint64) {
|
|
defer wg.Done()
|
|
<-start
|
|
_, err := repo.Create(orderTenantContext(tenantID), renterID, CreateRequest{ListingID: listing.ID})
|
|
results <- err
|
|
}(renters[index].ID, tenantID)
|
|
}
|
|
close(start)
|
|
wg.Wait()
|
|
close(results)
|
|
|
|
var success, unavailable int
|
|
for err := range results {
|
|
switch {
|
|
case err == nil:
|
|
success++
|
|
case errors.Is(err, ErrListingUnavailable):
|
|
unavailable++
|
|
default:
|
|
t.Fatalf("unexpected create order error: %v", err)
|
|
}
|
|
}
|
|
if success != 1 || unavailable != 1 {
|
|
t.Fatalf("concurrent order result = success:%d unavailable:%d", success, unavailable)
|
|
}
|
|
var count int64
|
|
if err := db.Model(&model.RentalOrder{}).Where("listing_id = ?", listing.ID).Count(&count).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("order count = %d, want 1", count)
|
|
}
|
|
}
|