103 lines
3.3 KiB
Go
103 lines
3.3 KiB
Go
package weekstar
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func TestCurrentRankingReturnsLiveRank(t *testing.T) {
|
|
ctx := context.Background()
|
|
service, db := newTestWeekStarService(t)
|
|
if err := db.AutoMigrate(&model.UserBaseInfo{}); err != nil {
|
|
t.Fatalf("auto migrate user_base_info: %v", err)
|
|
}
|
|
redisServer := miniredis.RunT(t)
|
|
redisClient := redis.NewClient(&redis.Options{Addr: redisServer.Addr()})
|
|
defer redisClient.Close()
|
|
service.repo.Redis = redisClient
|
|
|
|
now := time.Now().In(service.location)
|
|
cycleStart, cycleEnd := service.cycleBounds(now)
|
|
configRow := model.WeekStarActivityConfig{
|
|
ID: 101,
|
|
SysOrigin: "LIKEI",
|
|
Enabled: true,
|
|
StartAt: service.toStorageWallClock(cycleStart.Add(-time.Hour)),
|
|
EndAt: service.toStorageWallClock(cycleEnd.Add(time.Hour)),
|
|
Timezone: "Asia/Riyadh",
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}
|
|
if err := db.Create(&configRow).Error; err != nil {
|
|
t.Fatalf("create config: %v", err)
|
|
}
|
|
if err := db.Create(&model.WeekStarActivityGiftConfig{
|
|
ConfigID: configRow.ID,
|
|
GiftID: 2001,
|
|
GiftName: "Gift A",
|
|
GiftCandy: 100,
|
|
Sort: 1,
|
|
}).Error; err != nil {
|
|
t.Fatalf("create gift config: %v", err)
|
|
}
|
|
if err := db.Create([]model.UserBaseInfo{
|
|
{ID: 3001, Account: "3001", UserNickname: "Alice", CountryCode: "SA", CountryName: "Saudi Arabia"},
|
|
{ID: 3002, Account: "3002", UserNickname: "Bob", CountryCode: "AE", CountryName: "UAE"},
|
|
}).Error; err != nil {
|
|
t.Fatalf("create users: %v", err)
|
|
}
|
|
if err := redisClient.ZAdd(ctx, service.rankRedisKey(configRow.ID, service.cycleKey(cycleStart)),
|
|
redis.Z{Member: "3001", Score: 250},
|
|
redis.Z{Member: "3002", Score: 500},
|
|
).Err(); err != nil {
|
|
t.Fatalf("zadd rank: %v", err)
|
|
}
|
|
|
|
resp, err := service.CurrentRanking(ctx, "likei", 20)
|
|
if err != nil {
|
|
t.Fatalf("CurrentRanking() error = %v", err)
|
|
}
|
|
if resp.ActivityStatus != weekStarActivityStatusOngoing {
|
|
t.Fatalf("ActivityStatus = %s, want %s", resp.ActivityStatus, weekStarActivityStatusOngoing)
|
|
}
|
|
if resp.ConfigID != configRow.ID {
|
|
t.Fatalf("ConfigID = %d, want %d", resp.ConfigID, configRow.ID)
|
|
}
|
|
if resp.CycleKey != service.cycleKey(cycleStart) {
|
|
t.Fatalf("CycleKey = %s, want %s", resp.CycleKey, service.cycleKey(cycleStart))
|
|
}
|
|
if len(resp.GiftConfigs) != 1 || resp.GiftConfigs[0].GiftID != 2001 {
|
|
t.Fatalf("GiftConfigs = %#v", resp.GiftConfigs)
|
|
}
|
|
if len(resp.Records) != 2 {
|
|
t.Fatalf("Records length = %d, want 2", len(resp.Records))
|
|
}
|
|
if resp.Records[0].UserID != 3002 || resp.Records[0].Rank != 1 || resp.Records[0].ScoreGold != 500 {
|
|
t.Fatalf("top record = %#v", resp.Records[0])
|
|
}
|
|
if resp.Records[1].UserID != 3001 || resp.Records[1].Rank != 2 || resp.Records[1].ScoreGold != 250 {
|
|
t.Fatalf("second record = %#v", resp.Records[1])
|
|
}
|
|
}
|
|
|
|
func TestCurrentRankingReturnsDisabledWhenNoActiveConfig(t *testing.T) {
|
|
service, _ := newTestWeekStarService(t)
|
|
|
|
resp, err := service.CurrentRanking(context.Background(), "LIKEI", 20)
|
|
if err != nil {
|
|
t.Fatalf("CurrentRanking() error = %v", err)
|
|
}
|
|
if resp.ActivityStatus != weekStarActivityStatusDisabled {
|
|
t.Fatalf("ActivityStatus = %s, want %s", resp.ActivityStatus, weekStarActivityStatusDisabled)
|
|
}
|
|
if len(resp.Records) != 0 {
|
|
t.Fatalf("Records length = %d, want 0", len(resp.Records))
|
|
}
|
|
}
|