Files
kra-new/internal/biz/business/report_dates_test.go
T

79 lines
2.9 KiB
Go

package business
import (
"testing"
"time"
)
func TestAnalyticsDateRulesAgreeForListAndExport(t *testing.T) {
date := func(value string) *time.Time {
parsed, err := time.ParseInLocation("2006-01-02", value, portraitLocation)
if err != nil {
t.Fatal(err)
}
return &parsed
}
for _, kind := range []AnalyticsKind{ChannelAnalytics, GameAnalytics} {
for _, sample := range []struct {
name, start, end, wantStart, wantEnd string
now time.Time
}{
{"empty", "", "", "", "2026-11-04", time.Date(2026, 11, 4, 9, 0, 0, 0, portraitLocation)},
{"China day across UTC midnight", "", "", "", "2026-01-01", time.Date(2025, 12, 31, 17, 0, 0, 0, time.UTC)},
{"only end across leap day", "", "2024-03-01", "", "2024-03-01", time.Now()},
{"only start", "2026-01-01", "", "2026-01-01", "2026-11-04", time.Date(2026, 11, 4, 9, 0, 0, 0, portraitLocation)},
{"explicit historical year", "2023-01-01", "2023-12-31", "2023-01-01", "2023-12-31", time.Now()},
} {
t.Run(string(kind)+"/"+sample.name, func(t *testing.T) {
if kind == GameAnalytics && sample.wantStart == "" {
sample.wantStart = sample.wantEnd
}
uc, repo := analyticsFixture()
uc.now = func() time.Time { return sample.now }
filter := AnalyticsFilter{}
if sample.start != "" {
filter.StartDate = date(sample.start)
}
if sample.end != "" {
filter.EndDate = date(sample.end)
}
for _, export := range []bool{false, true} {
var page *AnalyticsPage
var err error
if export {
page, err = uc.ListAll(accountContext(7, 901), kind, filter)
} else {
page, err = uc.List(accountContext(7, 901), kind, filter, 1, 10)
}
if err != nil {
t.Fatal(err)
}
if page.Begin != sample.wantStart || page.End != sample.wantEnd || repo.filter.Begin != page.Begin || repo.filter.End != page.End || (repo.filter.StartDate == nil) != (sample.wantStart == "") || repo.filter.EndDate == nil {
t.Fatalf("export=%v expected %s..%s, got %+v", export, sample.wantStart, sample.wantEnd, repo.filter)
}
}
})
}
}
}
func TestLedgerPartialDateDoesNotDiscardHistoricalEnd(t *testing.T) {
uc, repo, _ := ledgerFixture()
end := time.Date(2024, 3, 1, 0, 0, 0, 0, portraitLocation)
for _, export := range []bool{false, true} {
filter := LedgerFilter{EndDate: &end}
var err error
if export {
_, err = uc.ExportSpends(accountContext(7, 901), filter)
} else {
_, err = uc.Registrations(accountContext(7, 901), filter, LedgerListOptions{Limit: 10})
}
if err != nil {
t.Fatal(err)
}
if time.Unix(repo.filter.BeginUnix, 0).In(portraitLocation).Format("2006-01-02 15:04:05") != "2024-02-01 00:00:00" || time.Unix(repo.filter.EndUnix, 0).In(portraitLocation).Format("2006-01-02 15:04:05") != "2024-03-01 23:59:59" {
t.Fatalf("historical end date was discarded: %+v", repo.filter)
}
}
}