macros_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package mssql
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestMacroEngine(t *testing.T) {
  12. Convey("MacroEngine", t, func() {
  13. engine := &msSqlMacroEngine{}
  14. query := &tsdb.Query{
  15. Model: simplejson.New(),
  16. }
  17. Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() {
  18. from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC)
  19. to := from.Add(5 * time.Minute)
  20. timeRange := tsdb.NewFakeTimeRange("5m", "now", to)
  21. Convey("interpolate __time function", func() {
  22. sql, err := engine.Interpolate(query, nil, "select $__time(time_column)")
  23. So(err, ShouldBeNil)
  24. So(sql, ShouldEqual, "select time_column AS time")
  25. })
  26. Convey("interpolate __timeEpoch function", func() {
  27. sql, err := engine.Interpolate(query, nil, "select $__timeEpoch(time_column)")
  28. So(err, ShouldBeNil)
  29. So(sql, ShouldEqual, "select DATEDIFF(second, '1970-01-01', time_column) AS time")
  30. })
  31. Convey("interpolate __timeEpoch function wrapped in aggregation", func() {
  32. sql, err := engine.Interpolate(query, nil, "select min($__timeEpoch(time_column))")
  33. So(err, ShouldBeNil)
  34. So(sql, ShouldEqual, "select min(DATEDIFF(second, '1970-01-01', time_column) AS time)")
  35. })
  36. Convey("interpolate __timeFilter function", func() {
  37. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  38. So(err, ShouldBeNil)
  39. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  40. })
  41. Convey("interpolate __timeFrom function", func() {
  42. sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom()")
  43. So(err, ShouldBeNil)
  44. So(sql, ShouldEqual, "select '2018-04-12T18:00:00Z'")
  45. })
  46. Convey("interpolate __timeTo function", func() {
  47. sql, err := engine.Interpolate(query, timeRange, "select $__timeTo()")
  48. So(err, ShouldBeNil)
  49. So(sql, ShouldEqual, "select '2018-04-12T18:05:00Z'")
  50. })
  51. Convey("interpolate __timeGroup function", func() {
  52. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
  53. So(err, ShouldBeNil)
  54. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column,'5m')")
  55. So(err, ShouldBeNil)
  56. So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300")
  57. So(sql2, ShouldEqual, sql+" AS [time]")
  58. })
  59. Convey("interpolate __timeGroup function with spaces around arguments", func() {
  60. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
  61. So(err, ShouldBeNil)
  62. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column , '5m')")
  63. So(err, ShouldBeNil)
  64. So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300")
  65. So(sql2, ShouldEqual, sql+" AS [time]")
  66. })
  67. Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
  68. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
  69. fill := query.Model.Get("fill").MustBool()
  70. fillMode := query.Model.Get("fillMode").MustString()
  71. fillInterval := query.Model.Get("fillInterval").MustInt()
  72. So(err, ShouldBeNil)
  73. So(fill, ShouldBeTrue)
  74. So(fillMode, ShouldEqual, "null")
  75. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  76. })
  77. Convey("interpolate __timeGroup function with fill (value = previous)", func() {
  78. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', previous)")
  79. fill := query.Model.Get("fill").MustBool()
  80. fillMode := query.Model.Get("fillMode").MustString()
  81. fillInterval := query.Model.Get("fillInterval").MustInt()
  82. So(err, ShouldBeNil)
  83. So(fill, ShouldBeTrue)
  84. So(fillMode, ShouldEqual, "previous")
  85. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  86. })
  87. Convey("interpolate __timeGroup function with fill (value = float)", func() {
  88. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
  89. fill := query.Model.Get("fill").MustBool()
  90. fillValue := query.Model.Get("fillValue").MustFloat64()
  91. fillInterval := query.Model.Get("fillInterval").MustInt()
  92. So(err, ShouldBeNil)
  93. So(fill, ShouldBeTrue)
  94. So(fillValue, ShouldEqual, 1.5)
  95. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  96. })
  97. Convey("interpolate __unixEpochFilter function", func() {
  98. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  99. So(err, ShouldBeNil)
  100. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix()))
  101. })
  102. Convey("interpolate __unixEpochGroup function", func() {
  103. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  104. So(err, ShouldBeNil)
  105. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  106. So(err, ShouldBeNil)
  107. So(sql, ShouldEqual, "SELECT FLOOR(time_column/300)*300")
  108. So(sql2, ShouldEqual, sql+" AS [time]")
  109. })
  110. })
  111. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  112. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  113. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  114. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  115. Convey("interpolate __timeFilter function", func() {
  116. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  117. So(err, ShouldBeNil)
  118. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  119. })
  120. Convey("interpolate __unixEpochFilter function", func() {
  121. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  122. So(err, ShouldBeNil)
  123. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), 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 __unixEpochFilter function", func() {
  136. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  137. So(err, ShouldBeNil)
  138. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix()))
  139. })
  140. })
  141. })
  142. }