Chore: Refactor GoConvey in mysql package tests (#40855)

This commit is contained in:
Serge Zaitsev
2021-10-26 13:18:17 +02:00
committed by GitHub
parent c9507f8f8a
commit 249856e527

View File

@@ -8,117 +8,116 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/infra/log"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)
func TestMacroEngine(t *testing.T) {
Convey("MacroEngine", t, func() {
engine := &mySQLMacroEngine{
logger: log.New("test"),
}
query := &backend.DataQuery{}
Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
t.Run("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func(t *testing.T) {
from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
to := from.Add(5 * time.Minute)
timeRange := backend.TimeRange{From: from, To: to}
Convey("interpolate __time function", func() {
t.Run("interpolate __time function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, "select UNIX_TIMESTAMP(time_column) as time_sec")
require.Equal(t, "select UNIX_TIMESTAMP(time_column) as time_sec", sql)
})
Convey("interpolate __time function wrapped in aggregation", func() {
t.Run("interpolate __time function wrapped in aggregation", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, "select min(UNIX_TIMESTAMP(time_column) as time_sec)")
require.Equal(t, "select min(UNIX_TIMESTAMP(time_column) as time_sec)", sql)
})
Convey("interpolate __timeGroup function", func() {
t.Run("interpolate __timeGroup function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column,'5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
require.Equal(t, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300", sql)
require.Equal(t, sql+" AS \"time\"", sql2)
})
Convey("interpolate __timeGroup function with spaces around arguments", func() {
t.Run("interpolate __timeGroup function with spaces around arguments", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column , '5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
require.Equal(t, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300", sql)
require.Equal(t, sql+" AS \"time\"", sql2)
})
Convey("interpolate __timeFilter function", func() {
t.Run("interpolate __timeFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()), sql)
})
Convey("interpolate __timeFrom function", func() {
t.Run("interpolate __timeFrom function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom()")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select FROM_UNIXTIME(%d)", from.Unix()))
require.Equal(t, fmt.Sprintf("select FROM_UNIXTIME(%d)", from.Unix()), sql)
})
Convey("interpolate __timeTo function", func() {
t.Run("interpolate __timeTo function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__timeTo()")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select FROM_UNIXTIME(%d)", to.Unix()))
require.Equal(t, fmt.Sprintf("select FROM_UNIXTIME(%d)", to.Unix()), sql)
})
Convey("interpolate __unixEpochFilter function", func() {
t.Run("interpolate __unixEpochFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()), sql)
})
Convey("interpolate __unixEpochNanoFilter function", func() {
t.Run("interpolate __unixEpochNanoFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
require.Equal(t, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()), sql)
})
Convey("interpolate __unixEpochNanoFrom function", func() {
t.Run("interpolate __unixEpochNanoFrom function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFrom()")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select %d", from.UnixNano()))
require.Equal(t, fmt.Sprintf("select %d", from.UnixNano()), sql)
})
Convey("interpolate __unixEpochNanoTo function", func() {
t.Run("interpolate __unixEpochNanoTo function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoTo()")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select %d", to.UnixNano()))
require.Equal(t, fmt.Sprintf("select %d", to.UnixNano()), sql)
})
Convey("interpolate __unixEpochGroup function", func() {
t.Run("interpolate __unixEpochGroup function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, "SELECT time_column DIV 300 * 300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
require.Equal(t, "SELECT time_column DIV 300 * 300", sql)
require.Equal(t, sql+" AS \"time\"", sql2)
})
})
Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
t.Run("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func(t *testing.T) {
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 := backend.TimeRange{
@@ -126,22 +125,22 @@ func TestMacroEngine(t *testing.T) {
To: to,
}
Convey("interpolate __timeFilter function", func() {
t.Run("interpolate __timeFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()), sql)
})
Convey("interpolate __unixEpochFilter function", func() {
t.Run("interpolate __unixEpochFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()), sql)
})
})
Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
t.Run("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func(t *testing.T) {
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 := backend.TimeRange{
@@ -149,22 +148,22 @@ func TestMacroEngine(t *testing.T) {
To: to,
}
Convey("interpolate __timeFilter function", func() {
t.Run("interpolate __timeFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()), sql)
})
Convey("interpolate __unixEpochFilter function", func() {
t.Run("interpolate __unixEpochFilter function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
So(err, ShouldBeNil)
require.Nil(t, err)
So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
require.Equal(t, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()), sql)
})
})
Convey("Given queries that contains unallowed user functions", func() {
t.Run("Given queries that contains unallowed user functions", func(t *testing.T) {
tcs := []string{
"select \nSESSION_USER(), abc",
"SELECT session_User( ) ",
@@ -188,10 +187,9 @@ func TestMacroEngine(t *testing.T) {
for _, tc := range tcs {
_, err := engine.Interpolate(&backend.DataQuery{}, backend.TimeRange{}, tc)
So(err.Error(), ShouldEqual, "invalid query - inspect Grafana server log for details")
require.Equal(t, "invalid query - inspect Grafana server log for details", err.Error())
}
})
})
}
func TestMacroEngineConcurrency(t *testing.T) {