2017-12-04 02:56:21 +08:00
|
|
|
package mssql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-03-19 20:32:04 +08:00
|
|
|
"time"
|
|
|
|
|
2018-03-15 20:11:26 +08:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2017-12-04 02:56:21 +08:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMacroEngine(t *testing.T) {
|
|
|
|
Convey("MacroEngine", t, func() {
|
|
|
|
engine := &MsSqlMacroEngine{}
|
|
|
|
timeRange := &tsdb.TimeRange{From: "5m", To: "now"}
|
2018-03-15 20:11:26 +08:00
|
|
|
query := &tsdb.Query{
|
|
|
|
Model: simplejson.New(),
|
|
|
|
}
|
2017-12-04 02:56:21 +08:00
|
|
|
|
|
|
|
Convey("interpolate __time function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, nil, "select $__time(time_column)")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2017-12-13 04:43:24 +08:00
|
|
|
So(sql, ShouldEqual, "select time_column AS time")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
2017-12-13 04:43:24 +08:00
|
|
|
Convey("interpolate __timeEpoch function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, nil, "select $__timeEpoch(time_column)")
|
2017-12-13 04:43:24 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "select DATEDIFF(second, '1970-01-01', time_column) AS time")
|
2017-12-13 04:43:24 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __timeEpoch function wrapped in aggregation", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, nil, "select min($__timeEpoch(time_column))")
|
2017-12-13 04:43:24 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "select min(DATEDIFF(second, '1970-01-01', time_column) AS time)")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __timeFilter function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "WHERE time_column >= DATEADD(s, 18446744066914186738, '1970-01-01') AND time_column <= DATEADD(s, 18446744066914187038, '1970-01-01')")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
2018-03-14 05:49:49 +08:00
|
|
|
Convey("interpolate __timeGroup function", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:49:40 +08:00
|
|
|
So(sql, ShouldEqual, "GROUP BY CAST(ROUND(DATEDIFF(second, '1970-01-01', time_column)/300.0, 0) as bigint)*300")
|
2018-03-14 05:49:49 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __timeGroup function with spaces around arguments", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:49:40 +08:00
|
|
|
So(sql, ShouldEqual, "GROUP BY CAST(ROUND(DATEDIFF(second, '1970-01-01', time_column)/300.0, 0) as bigint)*300")
|
2018-03-14 05:49:49 +08:00
|
|
|
})
|
|
|
|
|
2018-03-19 20:32:04 +08:00
|
|
|
Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
|
|
|
|
_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
|
|
|
|
|
|
|
|
fill := query.Model.Get("fill").MustBool()
|
|
|
|
fillNull := query.Model.Get("fillNull").MustBool()
|
|
|
|
fillInterval := query.Model.Get("fillInterval").MustInt()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(fill, ShouldBeTrue)
|
|
|
|
So(fillNull, ShouldBeTrue)
|
|
|
|
So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __timeGroup function with fill (value = float)", func() {
|
|
|
|
_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
|
|
|
|
|
|
|
|
fill := query.Model.Get("fill").MustBool()
|
|
|
|
fillValue := query.Model.Get("fillValue").MustFloat64()
|
|
|
|
fillInterval := query.Model.Get("fillInterval").MustInt()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(fill, ShouldBeTrue)
|
|
|
|
So(fillValue, ShouldEqual, 1.5)
|
|
|
|
So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
|
|
|
|
})
|
|
|
|
|
2017-12-04 02:56:21 +08:00
|
|
|
Convey("interpolate __timeFrom function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "select DATEADD(second, 18446744066914186738, '1970-01-01')")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __timeTo function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "select DATEADD(second, 18446744066914187038, '1970-01-01')")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __unixEpochFilter function", func() {
|
2018-03-22 21:55:44 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2018-03-22 21:55:44 +08:00
|
|
|
So(sql, ShouldEqual, "select time_column >= 18446744066914186738 AND time_column <= 18446744066914187038")
|
2017-12-04 02:56:21 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __unixEpochFrom function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(sql, ShouldEqual, "select 18446744066914186738")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("interpolate __unixEpochTo function", func() {
|
2018-03-13 23:03:02 +08:00
|
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
|
2017-12-04 02:56:21 +08:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(sql, ShouldEqual, "select 18446744066914187038")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|