macros_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 __timeFrom function", func() {
  50. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  51. So(err, ShouldBeNil)
  52. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  53. })
  54. Convey("interpolate __timeTo function", func() {
  55. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  56. So(err, ShouldBeNil)
  57. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  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 __unixEpochFrom function", func() {
  65. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  66. So(err, ShouldBeNil)
  67. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  68. })
  69. Convey("interpolate __unixEpochTo function", func() {
  70. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  71. So(err, ShouldBeNil)
  72. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  73. })
  74. })
  75. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  76. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  77. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  78. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  79. Convey("interpolate __timeFilter function", func() {
  80. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  81. So(err, ShouldBeNil)
  82. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  83. })
  84. Convey("interpolate __timeFrom function", func() {
  85. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  86. So(err, ShouldBeNil)
  87. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  88. })
  89. Convey("interpolate __timeTo function", func() {
  90. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  91. So(err, ShouldBeNil)
  92. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  93. })
  94. Convey("interpolate __unixEpochFilter function", func() {
  95. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  96. So(err, ShouldBeNil)
  97. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  98. })
  99. Convey("interpolate __unixEpochFrom function", func() {
  100. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  101. So(err, ShouldBeNil)
  102. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  103. })
  104. Convey("interpolate __unixEpochTo function", func() {
  105. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  106. So(err, ShouldBeNil)
  107. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  108. })
  109. })
  110. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  111. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  112. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  113. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  114. Convey("interpolate __timeFilter function", func() {
  115. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  116. So(err, ShouldBeNil)
  117. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  118. })
  119. Convey("interpolate __timeFrom function", func() {
  120. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  121. So(err, ShouldBeNil)
  122. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  123. })
  124. Convey("interpolate __timeTo function", func() {
  125. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  126. So(err, ShouldBeNil)
  127. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  128. })
  129. Convey("interpolate __unixEpochFilter function", func() {
  130. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  131. So(err, ShouldBeNil)
  132. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  133. })
  134. Convey("interpolate __unixEpochFrom function", func() {
  135. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  136. So(err, ShouldBeNil)
  137. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  138. })
  139. Convey("interpolate __unixEpochTo function", func() {
  140. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  141. So(err, ShouldBeNil)
  142. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  143. })
  144. })
  145. })
  146. }