macros_test.go 8.6 KB

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