macros_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package postgres
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/tsdb"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestMacroEngine(t *testing.T) {
  13. Convey("MacroEngine", t, func() {
  14. engine := newPostgresMacroEngine()
  15. query := &tsdb.Query{DataSource: &models.DataSource{JsonData: simplejson.New()}}
  16. queryTS := &tsdb.Query{DataSource: &models.DataSource{JsonData: simplejson.New()}}
  17. queryTS.DataSource.JsonData.Set("timescaledb", true)
  18. Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
  19. from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
  20. to := from.Add(5 * time.Minute)
  21. timeRange := tsdb.NewFakeTimeRange("5m", "now", to)
  22. Convey("interpolate __time function", func() {
  23. sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)")
  24. So(err, ShouldBeNil)
  25. So(sql, ShouldEqual, "select time_column AS \"time\"")
  26. })
  27. Convey("interpolate __time function wrapped in aggregation", func() {
  28. sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))")
  29. So(err, ShouldBeNil)
  30. So(sql, ShouldEqual, "select min(time_column AS \"time\")")
  31. })
  32. Convey("interpolate __timeFilter function", func() {
  33. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  34. So(err, ShouldBeNil)
  35. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  36. })
  37. Convey("interpolate __timeFrom function", func() {
  38. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  39. So(err, ShouldBeNil)
  40. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  41. })
  42. Convey("interpolate __timeGroup function pre 5.3 compatibility", func() {
  43. sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m'), value")
  44. So(err, ShouldBeNil)
  45. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 AS \"time\", value")
  46. sql, err = engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m') as time, value")
  47. So(err, ShouldBeNil)
  48. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300 as time, value")
  49. })
  50. Convey("interpolate __timeGroup function", func() {
  51. sql, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroup(time_column,'5m')")
  52. So(err, ShouldBeNil)
  53. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__timeGroupAlias(time_column,'5m')")
  54. So(err, ShouldBeNil)
  55. So(sql, ShouldEqual, "SELECT floor(extract(epoch from time_column)/300)*300")
  56. So(sql2, ShouldEqual, sql+" AS \"time\"")
  57. })
  58. Convey("interpolate __timeGroup function with spaces between args", func() {
  59. sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column , '5m')")
  60. So(err, ShouldBeNil)
  61. sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column , '5m')")
  62. So(err, ShouldBeNil)
  63. So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
  64. So(sql2, ShouldEqual, sql+" AS \"time\"")
  65. })
  66. Convey("interpolate __timeGroup function with TimescaleDB enabled", func() {
  67. sql, err := engine.Interpolate(queryTS, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
  68. So(err, ShouldBeNil)
  69. So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
  70. })
  71. Convey("interpolate __timeGroup function with spaces between args and TimescaleDB enabled", func() {
  72. sql, err := engine.Interpolate(queryTS, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
  73. So(err, ShouldBeNil)
  74. So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)")
  75. })
  76. Convey("interpolate __timeTo function", func() {
  77. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  78. So(err, ShouldBeNil)
  79. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  80. })
  81. Convey("interpolate __unixEpochFilter function", func() {
  82. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  83. So(err, ShouldBeNil)
  84. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  85. })
  86. Convey("interpolate __unixEpochFrom function", func() {
  87. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  88. So(err, ShouldBeNil)
  89. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  90. })
  91. Convey("interpolate __unixEpochTo function", func() {
  92. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  93. So(err, ShouldBeNil)
  94. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  95. })
  96. })
  97. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  98. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  99. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  100. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  101. Convey("interpolate __timeFilter function", func() {
  102. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  103. So(err, ShouldBeNil)
  104. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  105. })
  106. Convey("interpolate __timeFrom function", func() {
  107. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  108. So(err, ShouldBeNil)
  109. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  110. })
  111. Convey("interpolate __timeTo function", func() {
  112. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  113. So(err, ShouldBeNil)
  114. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  115. })
  116. Convey("interpolate __unixEpochFilter function", func() {
  117. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  118. So(err, ShouldBeNil)
  119. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  120. })
  121. Convey("interpolate __unixEpochFrom function", func() {
  122. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  123. So(err, ShouldBeNil)
  124. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  125. })
  126. Convey("interpolate __unixEpochTo function", func() {
  127. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  128. So(err, ShouldBeNil)
  129. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  130. })
  131. })
  132. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  133. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  134. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  135. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  136. Convey("interpolate __timeFilter function", func() {
  137. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  138. So(err, ShouldBeNil)
  139. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  140. })
  141. Convey("interpolate __timeFrom function", func() {
  142. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)")
  143. So(err, ShouldBeNil)
  144. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339)))
  145. })
  146. Convey("interpolate __timeTo function", func() {
  147. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)")
  148. So(err, ShouldBeNil)
  149. So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339)))
  150. })
  151. Convey("interpolate __unixEpochFilter function", func() {
  152. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)")
  153. So(err, ShouldBeNil)
  154. So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix()))
  155. })
  156. Convey("interpolate __unixEpochFrom function", func() {
  157. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()")
  158. So(err, ShouldBeNil)
  159. So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix()))
  160. })
  161. Convey("interpolate __unixEpochTo function", func() {
  162. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()")
  163. So(err, ShouldBeNil)
  164. So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
  165. })
  166. })
  167. })
  168. }