mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 23:12:30 +08:00

* Core: use go-datemath in time_range * Core: update timerange for negative epoch * Core: update gtime component * Core: clean up time_range tests * Update pkg/components/gtime/gtime.go Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com> * Core: clean gtime tests * components/gtime: Fix test Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package gtime
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestParseInterval(t *testing.T) {
|
|
now := time.Now()
|
|
|
|
tcs := []struct {
|
|
interval string
|
|
duration time.Duration
|
|
err string
|
|
}{
|
|
{interval: "1d", duration: now.Sub(now.AddDate(0, 0, -1))},
|
|
{interval: "1w", duration: now.Sub(now.AddDate(0, 0, -7))},
|
|
{interval: "2w", duration: now.Sub(now.AddDate(0, 0, -14))},
|
|
{interval: "1M", duration: now.Sub(now.AddDate(0, -1, 0))},
|
|
{interval: "1y", duration: now.Sub(now.AddDate(-1, 0, 0))},
|
|
{interval: "5y", duration: now.Sub(now.AddDate(-5, 0, 0))},
|
|
{interval: "invalid-duration", err: "time: invalid duration invalid-duration"},
|
|
}
|
|
|
|
for i, tc := range tcs {
|
|
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
|
|
res, err := ParseInterval(tc.interval)
|
|
if tc.err == "" {
|
|
require.NoError(t, err, "interval %q", tc.interval)
|
|
require.Equal(t, tc.duration, res, "interval %q", tc.interval)
|
|
} else {
|
|
require.EqualError(t, err, tc.err, "interval %q", tc.interval)
|
|
}
|
|
})
|
|
}
|
|
}
|