fix: expose complete dataset period catalog

This commit is contained in:
2026-08-20 22:28:07 +03:30
parent d6de48fd15
commit 6523e90a03
8 changed files with 140 additions and 72 deletions

View File

@@ -31,7 +31,7 @@ func TestJalaliGregorianRoundTrip(t *testing.T) {
}
}
func TestLatestCompletePeriods(t *testing.T) {
func TestCompletePeriods(t *testing.T) {
t.Parallel()
location, err := time.LoadLocation("Asia/Tehran")
if err != nil {
@@ -40,37 +40,57 @@ func TestLatestCompletePeriods(t *testing.T) {
datasetStart := time.Date(2026, time.January, 1, 0, 0, 0, 0, location)
datasetMax := time.Date(2026, time.June, 30, 23, 59, 0, 0, location)
periods := latestCompletePeriods(datasetStart, datasetMax, location)
if len(periods) != 4 {
t.Fatalf("latestCompletePeriods() returned %d periods, want 4", len(periods))
periods := completePeriods(datasetStart, datasetMax, location)
if len(periods) != 212 {
t.Fatalf("completePeriods() returned %d periods, want 212", len(periods))
}
want := []struct {
index int
id, kind, start, end string
isDefault bool
}{
{id: "d-2026-06-30", kind: "DAY", start: "2026-06-30", end: "2026-07-01"},
{id: "w-2026-06-20", kind: "WEEK", start: "2026-06-20", end: "2026-06-27"},
{id: "jm-1405-03", kind: "JALALI_MONTH", start: "2026-05-22", end: "2026-06-22", isDefault: true},
{id: "ps-1405-01", kind: "PERSIAN_SEASON", start: "2026-03-21", end: "2026-06-22"},
{index: 0, id: "d-2026-06-30", kind: "DAY", start: "2026-06-30", end: "2026-07-01"},
{index: 180, id: "d-2026-01-01", kind: "DAY", start: "2026-01-01", end: "2026-01-02"},
{index: 181, id: "w-2026-06-20", kind: "WEEK", start: "2026-06-20", end: "2026-06-27"},
{index: 205, id: "w-2026-01-03", kind: "WEEK", start: "2026-01-03", end: "2026-01-10"},
{index: 206, id: "jm-1405-03", kind: "JALALI_MONTH", start: "2026-05-22", end: "2026-06-22", isDefault: true},
{index: 210, id: "jm-1404-11", kind: "JALALI_MONTH", start: "2026-01-21", end: "2026-02-20"},
{index: 211, id: "ps-1405-01", kind: "PERSIAN_SEASON", start: "2026-03-21", end: "2026-06-22"},
}
for index, expected := range want {
period := periods[index]
for _, expected := range want {
period := periods[expected.index]
if period.PeriodID != expected.id || period.PeriodType != expected.kind {
t.Errorf("period[%d] identity = %s/%s, want %s/%s", index, period.PeriodID, period.PeriodType, expected.id, expected.kind)
t.Errorf("period[%d] identity = %s/%s, want %s/%s", expected.index, period.PeriodID, period.PeriodType, expected.id, expected.kind)
}
if got := period.LocalStartAt.Format(time.DateOnly); got != expected.start {
t.Errorf("period[%d] start = %s, want %s", index, got, expected.start)
t.Errorf("period[%d] start = %s, want %s", expected.index, got, expected.start)
}
if got := period.LocalEndAt.Format(time.DateOnly); got != expected.end {
t.Errorf("period[%d] end = %s, want %s", index, got, expected.end)
t.Errorf("period[%d] end = %s, want %s", expected.index, got, expected.end)
}
if period.IsDefault != expected.isDefault {
t.Errorf("period[%d] IsDefault = %v, want %v", index, period.IsDefault, expected.isDefault)
t.Errorf("period[%d] IsDefault = %v, want %v", expected.index, period.IsDefault, expected.isDefault)
}
if period.StartAt.Location() != time.UTC || period.EndAt.Location() != time.UTC {
t.Errorf("period[%d] UTC boundaries do not use UTC", index)
t.Errorf("period[%d] UTC boundaries do not use UTC", expected.index)
}
}
defaultCount := 0
for _, period := range periods {
if !period.IsComplete {
t.Errorf("period %s is not complete", period.PeriodID)
}
if period.LocalStartAt.Before(datasetStart) || period.LocalEndAt.After(datasetMax.Add(time.Minute)) {
t.Errorf("period %s lies outside dataset coverage", period.PeriodID)
}
if period.IsDefault {
defaultCount++
}
}
if defaultCount != 1 {
t.Fatalf("completePeriods() returned %d defaults, want 1", defaultCount)
}
}