macros_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
  48. })
  49. Convey("interpolate __timeFrom function", func() {
  50. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom()")
  51. So(err, ShouldBeNil)
  52. So(sql, ShouldEqual, fmt.Sprintf("select FROM_UNIXTIME(%d)", from.Unix()))
  53. })
  54. Convey("interpolate __timeTo function", func() {
  55. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo()")
  56. So(err, ShouldBeNil)
  57. So(sql, ShouldEqual, fmt.Sprintf("select FROM_UNIXTIME(%d)", to.Unix()))
  58. })
  59. Convey("interpolate __unixEpochFilter function", func() {
  60. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  61. So(err, ShouldBeNil)
  62. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  63. })
  64. Convey("interpolate __unixEpochNanoFilter function", func() {
  65. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time)")
  66. So(err, ShouldBeNil)
  67. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
  68. })
  69. Convey("interpolate __unixEpochNanoFrom function", func() {
  70. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFrom()")
  71. So(err, ShouldBeNil)
  72. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.UnixNano()))
  73. })
  74. Convey("interpolate __unixEpochNanoTo function", func() {
  75. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoTo()")
  76. So(err, ShouldBeNil)
  77. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.UnixNano()))
  78. })
  79. Convey("interpolate __unixEpochGroup function", func() {
  80. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  81. So(err, ShouldBeNil)
  82. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  83. So(err, ShouldBeNil)
  84. So(sql, ShouldEqual, "SELECT time_column DIV 300 * 300")
  85. So(sql2, ShouldEqual, sql+" AS \"time\"")
  86. })
  87. })
  88. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  89. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  90. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  91. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  92. Convey("interpolate __timeFilter function", func() {
  93. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  94. So(err, ShouldBeNil)
  95. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
  96. })
  97. Convey("interpolate __unixEpochFilter function", func() {
  98. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  99. So(err, ShouldBeNil)
  100. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  101. })
  102. })
  103. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  104. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  105. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  106. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  107. Convey("interpolate __timeFilter function", func() {
  108. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  109. So(err, ShouldBeNil)
  110. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", from.Unix(), to.Unix()))
  111. })
  112. Convey("interpolate __unixEpochFilter function", func() {
  113. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  114. So(err, ShouldBeNil)
  115. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  116. })
  117. })
  118. })
  119. }