macros_test.go 8.3 KB

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