macros_test.go 8.1 KB

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