mirror of
https://github.com/grafana/grafana.git
synced 2024-11-30 12:44:10 -06:00
159 lines
5.9 KiB
Go
159 lines
5.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
)
|
|
|
|
func TestMacroEngine(t *testing.T) {
|
|
Convey("MacroEngine", t, func() {
|
|
timescaledbEnabled := false
|
|
engine := newPostgresMacroEngine(timescaledbEnabled)
|
|
timescaledbEnabled = true
|
|
engineTS := newPostgresMacroEngine(timescaledbEnabled)
|
|
query := &tsdb.Query{}
|
|
|
|
Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
|
|
from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
|
|
to := from.Add(5 * time.Minute)
|
|
timeRange := tsdb.NewFakeTimeRange("5m", "now", to)
|
|
|
|
Convey("interpolate __time function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "select time_column AS \"time\"")
|
|
})
|
|
|
|
Convey("interpolate __time function wrapped in aggregation", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "select min(time_column AS \"time\")")
|
|
})
|
|
|
|
Convey("interpolate __timeFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
|
|
})
|
|
|
|
Convey("interpolate __timeGroup function pre 5.3 compatibility", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m'), value")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 AS \"time\", value")
|
|
|
|
sql, err = engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m') as time, value")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 as time, value")
|
|
})
|
|
|
|
Convey("interpolate __timeGroup function", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m')")
|
|
So(err, ShouldBeNil)
|
|
sql2, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroupAlias(time_column,'5m')")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300")
|
|
So(sql2, ShouldEqual, sql+" AS \"time\"")
|
|
})
|
|
|
|
Convey("interpolate __timeGroup function with spaces between args", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column , '5m')")
|
|
So(err, ShouldBeNil)
|
|
sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column , '5m')")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
|
|
So(sql2, ShouldEqual, sql+" AS \"time\"")
|
|
})
|
|
|
|
Convey("interpolate __timeGroup function with TimescaleDB enabled", func() {
|
|
|
|
sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
|
|
})
|
|
|
|
Convey("interpolate __timeGroup function with spaces between args and TimescaleDB enabled", func() {
|
|
|
|
sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
|
|
})
|
|
|
|
Convey("interpolate __unixEpochFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
|
|
})
|
|
|
|
Convey("interpolate __unixEpochGroup function", func() {
|
|
|
|
sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
|
|
So(err, ShouldBeNil)
|
|
sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, "SELECT floor(time_column/300)*300")
|
|
So(sql2, ShouldEqual, sql+" AS \"time\"")
|
|
})
|
|
|
|
})
|
|
|
|
Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
|
|
from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
|
|
to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
|
|
timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
|
|
|
|
Convey("interpolate __timeFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
|
|
})
|
|
|
|
Convey("interpolate __unixEpochFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
|
|
})
|
|
})
|
|
|
|
Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
|
|
from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
|
|
to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
|
|
timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
|
|
|
|
Convey("interpolate __timeFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
|
|
})
|
|
|
|
Convey("interpolate __unixEpochFilter function", func() {
|
|
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
|
|
So(err, ShouldBeNil)
|
|
|
|
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
|
|
})
|
|
})
|
|
})
|
|
}
|