yumi-golang/internal/service/wheel/records_test.go

192 lines
5.4 KiB
Go

package wheel
import (
"context"
"testing"
"time"
"chatapp3-golang/internal/model"
)
func TestPageAdminRecordsIncludesUserInfoAndRangeSummary(t *testing.T) {
service, _, db := newWheelDrawTestService(t)
now := time.Now()
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
recordTime := todayStart.Add(time.Hour)
oldRecordTime := todayStart.Add(-time.Hour)
giftID := int64(9001)
if err := db.Create([]model.UserBaseInfo{
{
ID: 1001,
Account: "short1001",
UserAvatar: "https://example.com/a.png",
UserNickname: "Alice",
OriginSys: "LIKEI",
CreateTime: recordTime,
},
{
ID: 1002,
Account: "short1002",
UserAvatar: "https://example.com/b.png",
UserNickname: "Bob",
OriginSys: "LIKEI",
CreateTime: recordTime,
},
}).Error; err != nil {
t.Fatalf("seed users: %v", err)
}
rows := []model.WheelDrawRecord{
{
ID: 1,
DrawNo: "WHEEL-1001-1",
EventID: "event-1",
SysOrigin: "LIKEI",
Category: categoryClassic,
UserID: 1001,
DrawTimes: 10,
PaidGold: 1000,
RewardType: rewardTypeResource,
ResourceID: &giftID,
ResourceType: resourceTypeGift,
ResourceName: "Gift A",
DisplayGoldAmount: 50,
Probability: 500000,
Status: drawStatusSuccess,
CreateTime: recordTime,
UpdateTime: recordTime,
},
{
ID: 2,
DrawNo: "WHEEL-1001-1",
EventID: "event-2",
SysOrigin: "LIKEI",
Category: categoryClassic,
UserID: 1001,
DrawTimes: 10,
PaidGold: 1000,
RewardType: rewardTypeGold,
GoldAmount: 70,
DisplayGoldAmount: 999,
Probability: 500000,
Status: drawStatusSuccess,
CreateTime: recordTime.Add(time.Minute),
UpdateTime: recordTime.Add(time.Minute),
},
{
ID: 3,
DrawNo: "WHEEL-1002-1",
EventID: "event-3",
SysOrigin: "LIKEI",
Category: categoryClassic,
UserID: 1002,
DrawTimes: 1,
PaidGold: 100,
RewardType: rewardTypeResource,
ResourceID: &giftID,
ResourceType: resourceTypeGift,
ResourceName: "Gift Failed",
DisplayGoldAmount: 999,
Probability: 500000,
Status: drawStatusFailed,
CreateTime: recordTime.Add(2 * time.Minute),
UpdateTime: recordTime.Add(2 * time.Minute),
},
{
ID: 4,
DrawNo: "WHEEL-1002-2",
EventID: "event-4",
SysOrigin: "LIKEI",
Category: categoryClassic,
UserID: 1002,
DrawTimes: 1,
PaidGold: 200,
RewardType: rewardTypeResource,
ResourceID: &giftID,
ResourceType: "AVATAR_FRAME",
ResourceName: "Frame B",
DisplayGoldAmount: 30,
Probability: 500000,
Status: drawStatusSuccess,
CreateTime: recordTime.Add(3 * time.Minute),
UpdateTime: recordTime.Add(3 * time.Minute),
},
{
ID: 5,
DrawNo: "WHEEL-OLD",
EventID: "event-old",
SysOrigin: "LIKEI",
Category: categoryClassic,
UserID: 1001,
DrawTimes: 50,
PaidGold: 5000,
RewardType: rewardTypeResource,
ResourceID: &giftID,
ResourceType: resourceTypeGift,
ResourceName: "Old Gift",
DisplayGoldAmount: 500,
Probability: 500000,
Status: drawStatusSuccess,
CreateTime: oldRecordTime,
UpdateTime: oldRecordTime,
},
}
if err := db.Create(&rows).Error; err != nil {
t.Fatalf("seed records: %v", err)
}
resp, err := service.PageAdminRecords(context.Background(), "LIKEI", "", 0, "", 1, 20, "", "")
if err != nil {
t.Fatalf("PageAdminRecords() error = %v", err)
}
if resp.Total != 4 {
t.Fatalf("total = %d, want 4", resp.Total)
}
if resp.TotalPaidGold != 1300 {
t.Fatalf("total paid = %d, want 1300", resp.TotalPaidGold)
}
if resp.TotalRewardGold != 150 {
t.Fatalf("total reward = %d, want 150", resp.TotalRewardGold)
}
if resp.TotalGiftRewardGold != 50 {
t.Fatalf("total gift reward = %d, want 50", resp.TotalGiftRewardGold)
}
if resp.TotalDecorationRewardGold != 30 {
t.Fatalf("total decoration reward = %d, want 30", resp.TotalDecorationRewardGold)
}
if resp.TotalDrawTimes != 12 {
t.Fatalf("total draw times = %d, want 12", resp.TotalDrawTimes)
}
if resp.ParticipantCount != 2 {
t.Fatalf("participant count = %d, want 2", resp.ParticipantCount)
}
var user1001 *DrawRecordPayload
var user1002 *DrawRecordPayload
for index := range resp.Records {
record := &resp.Records[index]
switch record.UserID {
case 1001:
user1001 = record
case 1002:
user1002 = record
}
}
if user1001 == nil {
t.Fatal("missing user 1001 record")
}
if user1001.UserShortID != "short1001" || user1001.UserAvatar == "" || user1001.UserNickname != "Alice" {
t.Fatalf("user 1001 info = %+v", user1001)
}
if user1001.UserTotalDrawTimes != 10 {
t.Fatalf("user 1001 total draw times = %d, want 10", user1001.UserTotalDrawTimes)
}
if user1002 == nil {
t.Fatal("missing user 1002 record")
}
if user1002.UserTotalDrawTimes != 2 {
t.Fatalf("user 1002 total draw times = %d, want 2", user1002.UserTotalDrawTimes)
}
}