macros_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 __timeGroup function", func() {
  42. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
  43. So(err, ShouldBeNil)
  44. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column,'5m')")
  45. So(err, ShouldBeNil)
  46. So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300")
  47. So(sql2, ShouldEqual, sql+" AS [time]")
  48. })
  49. Convey("interpolate __timeGroup function with spaces around arguments", func() {
  50. sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
  51. So(err, ShouldBeNil)
  52. sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column , '5m')")
  53. So(err, ShouldBeNil)
  54. So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300")
  55. So(sql2, ShouldEqual, sql+" AS [time]")
  56. })
  57. Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
  58. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
  59. fill := query.Model.Get("fill").MustBool()
  60. fillMode := query.Model.Get("fillMode").MustString()
  61. fillInterval := query.Model.Get("fillInterval").MustInt()
  62. So(err, ShouldBeNil)
  63. So(fill, ShouldBeTrue)
  64. So(fillMode, ShouldEqual, "null")
  65. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  66. })
  67. Convey("interpolate __timeGroup function with fill (value = previous)", func() {
  68. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', previous)")
  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, "previous")
  75. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  76. })
  77. Convey("interpolate __timeGroup function with fill (value = float)", func() {
  78. _, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
  79. fill := query.Model.Get("fill").MustBool()
  80. fillValue := query.Model.Get("fillValue").MustFloat64()
  81. fillInterval := query.Model.Get("fillInterval").MustInt()
  82. So(err, ShouldBeNil)
  83. So(fill, ShouldBeTrue)
  84. So(fillValue, ShouldEqual, 1.5)
  85. So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
  86. })
  87. Convey("interpolate __unixEpochFilter function", func() {
  88. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  89. So(err, ShouldBeNil)
  90. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix()))
  91. })
  92. Convey("interpolate __unixEpochGroup function", func() {
  93. sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
  94. So(err, ShouldBeNil)
  95. sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
  96. So(err, ShouldBeNil)
  97. So(sql, ShouldEqual, "SELECT FLOOR(time_column/300)*300")
  98. So(sql2, ShouldEqual, sql+" AS [time]")
  99. })
  100. })
  101. Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
  102. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  103. to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC)
  104. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  105. Convey("interpolate __timeFilter function", func() {
  106. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  107. So(err, ShouldBeNil)
  108. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  109. })
  110. Convey("interpolate __unixEpochFilter function", func() {
  111. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  112. So(err, ShouldBeNil)
  113. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix()))
  114. })
  115. })
  116. Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() {
  117. from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC)
  118. to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC)
  119. timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10))
  120. Convey("interpolate __timeFilter function", func() {
  121. sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)")
  122. So(err, ShouldBeNil)
  123. So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339)))
  124. })
  125. Convey("interpolate __unixEpochFilter function", func() {
  126. sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)")
  127. So(err, ShouldBeNil)
  128. So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix()))
  129. })
  130. })
  131. })
  132. }