2016-09-21 00:01:53 -05:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeRange(t *testing.T) {
|
|
|
|
Convey("Time range", t, func() {
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
Convey("Can parse 5m, now", func() {
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "5m",
|
|
|
|
To: "now",
|
|
|
|
Now: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("5m ago ", func() {
|
|
|
|
fiveMinAgo, _ := time.ParseDuration("-5m")
|
|
|
|
expected := now.Add(fiveMinAgo)
|
|
|
|
|
2016-09-21 06:41:25 -05:00
|
|
|
res, err := tr.FromTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("now ", func() {
|
2016-09-21 06:41:25 -05:00
|
|
|
res, err := tr.ToTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, now.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can parse 5h, now-10m", func() {
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "5h",
|
|
|
|
To: "now-10m",
|
|
|
|
Now: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("5h ago ", func() {
|
2016-09-21 06:41:25 -05:00
|
|
|
fiveHourAgo, _ := time.ParseDuration("-5h")
|
|
|
|
expected := now.Add(fiveHourAgo)
|
2016-09-21 00:01:53 -05:00
|
|
|
|
2016-09-21 06:41:25 -05:00
|
|
|
res, err := tr.FromTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("now-10m ", func() {
|
|
|
|
fiveMinAgo, _ := time.ParseDuration("-10m")
|
|
|
|
expected := now.Add(fiveMinAgo)
|
2016-09-21 06:41:25 -05:00
|
|
|
res, err := tr.ToTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-09-21 06:41:25 -05:00
|
|
|
So(res.Unix(), ShouldEqual, expected.Unix())
|
2016-09-21 00:01:53 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Cannot parse asdf", func() {
|
|
|
|
var err error
|
|
|
|
tr := TimeRange{
|
|
|
|
From: "asdf",
|
|
|
|
To: "asdf",
|
|
|
|
Now: now,
|
|
|
|
}
|
|
|
|
|
2016-09-21 06:41:25 -05:00
|
|
|
_, err = tr.FromTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
2016-09-21 06:41:25 -05:00
|
|
|
_, err = tr.ToTime()
|
2016-09-21 00:01:53 -05:00
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|