macros_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package mysql
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/tsdb"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestMacroEngine(t *testing.T) {
  11. Convey("MacroEngine", t, func() {
  12. engine := &mySqlMacroEngine{}
  13. query := &tsdb.Query{}
  14. Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
  15. from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
  16. to := from.Add(5 * time.Minute)
  17. timeRange := tsdb.NewFakeTimeRange("5m", "now", to)
  18. Convey("interpolate __time function", func() {
  19. sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)")
  20. So(err, ShouldBeNil)
  21. So(sql, ShouldEqual, "select UNIX_TIMESTAMP(time_column) as time_sec")
  22. })
  23. Convey("interpolate __time function wrapped in aggregation", func() {
  24. sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))")
  25. So(err, ShouldBeNil)
  26. So(sql, ShouldEqual, "select min(UNIX_TIMESTAMP(time_column) as time_sec)")
  27. })
  28. Convey("interpolate __timeGroup function", func() {
  29. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
  30. So(err, ShouldBeNil)
  31. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column,'5m')")
  32. So(err, ShouldBeNil)
  33. So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
  34. So(sql2, ShouldEqual, sql+" AS \"time\"")
  35. })
  36. Convey("interpolate __timeGroup function with spaces around arguments", func() {
  37. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
  38. So(err, ShouldBeNil)
  39. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column , '5m')")
  40. So(err, ShouldBeNil)
  41. So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
  42. So(sql2, ShouldEqual, sql+" AS \"time\"")
  43. })
  44. Convey("interpolate __timeFilter function", func() {
  45. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  46. So(err, ShouldBeNil)
  47. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  48. })
  49. Convey("interpolate __unixEpochFilter function", func() {
  50. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  51. So(err, ShouldBeNil)
  52. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  53. })
  54. Convey("interpolate __unixEpochGroup function", func() {
  55. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  56. So(err, ShouldBeNil)
  57. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  58. So(err, ShouldBeNil)
  59. So(sql, ShouldEqual, "SELECT time_column DIV 300 * 300")
  60. So(sql2, ShouldEqual, sql+" AS \"time\"")
  61. })
  62. })
  63. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  64. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  65. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  66. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  67. Convey("interpolate __timeFilter function", func() {
  68. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  69. So(err, ShouldBeNil)
  70. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  71. })
  72. Convey("interpolate __unixEpochFilter function", func() {
  73. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  74. So(err, ShouldBeNil)
  75. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  76. })
  77. })
  78. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  79. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  80. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  81. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  82. Convey("interpolate __timeFilter function", func() {
  83. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  84. So(err, ShouldBeNil)
  85. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  86. })
  87. Convey("interpolate __unixEpochFilter function", func() {
  88. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  89. So(err, ShouldBeNil)
  90. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  91. })
  92. })
  93. })
  94. }