88 lines
3.9 KiB
Go
88 lines
3.9 KiB
Go
package business
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type serverAnalysisFake struct {
|
|
data *ServerAnalysisData
|
|
scope AnalyticsScope
|
|
filter ServerAnalysisFilter
|
|
limit int
|
|
}
|
|
|
|
func (r *serverAnalysisFake) Aggregate(_ context.Context, s AnalyticsScope, f ServerAnalysisFilter) (*ServerAnalysisData, error) {
|
|
r.scope, r.filter = s, f
|
|
out := &ServerAnalysisData{Available: r.data.Available}
|
|
for _, row := range r.data.Rows {
|
|
c := *row
|
|
out.Rows = append(out.Rows, &c)
|
|
}
|
|
return out, nil
|
|
}
|
|
func (r *serverAnalysisFake) Servers(_ context.Context, viewer UserScope, _, _ []string, _ string, limit int) ([]*ServerOption, error) {
|
|
r.scope.Viewer = viewer
|
|
r.limit = limit
|
|
return []*ServerOption{}, nil
|
|
}
|
|
func TestServerAnalysisSummaryZerosSortingAndLimits(t *testing.T) {
|
|
r := &serverAnalysisFake{data: &ServerAnalysisData{Available: true, Rows: []*ServerAnalysisRow{
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "02"}, Counts: PortraitCounts{Register: 2, Active: 3, Pay: 1}, Pay: "10.005"},
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "hw", GameID: 1, ServerNum: "01"}, Counts: PortraitCounts{Register: 3, Active: 2, Pay: 1}, Pay: "20"},
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "zero"}, Pay: "0"},
|
|
}}}
|
|
a := &accountRepoFake{accounts: map[uint]*Account{7: {ID: 7, Level: 1, IDPath: "0"}}}
|
|
uc := NewServerAnalysisUsecase(r, NewAccountUsecase(a), NewUserPreferenceUsecase(&channelPreferenceFake{UserPreference{IncludeSubPromote: true, IDCardDedup: true}}))
|
|
uc.now = func() time.Time { return time.Date(2026, 11, 4, 9, 0, 0, 0, portraitLocation) }
|
|
f := ServerAnalysisFilter{AnalyticsFilter: AnalyticsFilter{GameNames: []string{"游戏"}}}
|
|
p, err := uc.List(accountContext(7, 901), f, 1, 1)
|
|
if err != nil || p.Total != 2 || len(p.Rows) != 1 || p.Rows[0].ServerNum != "01" || p.Summary.Pay != "30.01" || p.Summary.Counts.Register != 5 || p.Summary.ARPU != "6.00" || p.Begin != "2026-10-06" || p.End != "2026-11-04" {
|
|
t.Fatalf("汇总、四舍五入或分页错误: %+v %v", p, err)
|
|
}
|
|
if !r.scope.IncludeSub || !r.scope.IDCardDedup || r.scope.Viewer.All {
|
|
t.Fatal("查看者偏好或范围未传递")
|
|
}
|
|
p, err = uc.List(accountContext(7, 901), f, 999999999, 10)
|
|
if err != nil || len(p.Rows) != 0 || p.Summary.Pay != "30.01" {
|
|
t.Fatal("越界分页不应丢失汇总")
|
|
}
|
|
r.data.Rows = r.data.Rows[2:]
|
|
f.ServerNum = "zero"
|
|
p, err = uc.List(accountContext(7, 901), f, 1, 0)
|
|
if err != nil || p.Total != 1 {
|
|
t.Fatal("指定区服时应保留已有全零行")
|
|
}
|
|
r.data.Available = false
|
|
p, err = uc.List(accountContext(7, 901), f, 1, 10)
|
|
if err != nil || p.Summary != nil || p.Available {
|
|
t.Fatal("未接入不能返回零汇总")
|
|
}
|
|
if _, err := uc.List(accountContext(7, 901), ServerAnalysisFilter{}, 1, 10); err != ErrServerAnalysisGame {
|
|
t.Fatal("区服查询必须选择游戏")
|
|
}
|
|
for _, sample := range []struct{ in, want int }{{0, 50}, {100, 100}, {101, 100000}} {
|
|
if _, err := uc.Servers(accountContext(7, 901), nil, []string{"游戏"}, "", sample.in); err != nil || r.limit != sample.want {
|
|
t.Fatal("区服选项上限与旧行为不符")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestServerAnalysisCancelSortRestoresIdentityOrder(t *testing.T) {
|
|
rows := []*ServerAnalysisRow{
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "002"}, Pay: "5"},
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "001"}, Pay: "10"},
|
|
{ServerAnalysisKey: ServerAnalysisKey{SourceDB: "lx", GameID: 1, ServerNum: "003"}, Pay: "20"},
|
|
}
|
|
if err := sortServerAnalysis(rows, "totalPay", true); err != nil || rows[0].ServerNum != "002" {
|
|
t.Fatal("升序错误")
|
|
}
|
|
if err := sortServerAnalysis(rows, "totalPay", false); err != nil || rows[0].ServerNum != "003" {
|
|
t.Fatal("降序错误")
|
|
}
|
|
if err := sortServerAnalysis(rows, "", false); err != nil || rows[0].ServerNum != "001" {
|
|
t.Fatal("取消排序应恢复稳定的区服顺序")
|
|
}
|
|
}
|