scenarios_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package testdata
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestTestdataScenarios(t *testing.T) {
  10. Convey("random walk ", t, func() {
  11. scenario, exist := ScenarioRegistry["random_walk"]
  12. So(exist, ShouldBeTrue)
  13. Convey("Should start at the requested value", func() {
  14. req := &tsdb.TsdbQuery{
  15. TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
  16. Queries: []*tsdb.Query{
  17. {RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
  18. },
  19. }
  20. query := req.Queries[0]
  21. query.Model.Set("startValue", 1.234)
  22. result := scenario.Handler(req.Queries[0], req)
  23. points := result.Series[0].Points
  24. So(result.Series, ShouldNotBeNil)
  25. So(points[0][0].Float64, ShouldEqual, 1.234)
  26. })
  27. })
  28. Convey("random walk table", t, func() {
  29. scenario, exist := ScenarioRegistry["random_walk_table"]
  30. So(exist, ShouldBeTrue)
  31. Convey("Should return a table that looks like value/min/max", func() {
  32. req := &tsdb.TsdbQuery{
  33. TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
  34. Queries: []*tsdb.Query{
  35. {RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
  36. },
  37. }
  38. result := scenario.Handler(req.Queries[0], req)
  39. table := result.Tables[0]
  40. So(len(table.Rows), ShouldBeGreaterThan, 50)
  41. for _, row := range table.Rows {
  42. value := row[1]
  43. min := row[2]
  44. max := row[3]
  45. So(min, ShouldBeLessThan, value)
  46. So(max, ShouldBeGreaterThan, value)
  47. }
  48. })
  49. Convey("Should return a table with some nil values", func() {
  50. req := &tsdb.TsdbQuery{
  51. TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
  52. Queries: []*tsdb.Query{
  53. {RefId: "A", IntervalMs: 100, MaxDataPoints: 100, Model: simplejson.New()},
  54. },
  55. }
  56. query := req.Queries[0]
  57. query.Model.Set("withNil", true)
  58. result := scenario.Handler(req.Queries[0], req)
  59. table := result.Tables[0]
  60. nil1 := false
  61. nil2 := false
  62. nil3 := false
  63. So(len(table.Rows), ShouldBeGreaterThan, 50)
  64. for _, row := range table.Rows {
  65. if row[1] == nil {
  66. nil1 = true
  67. }
  68. if row[2] == nil {
  69. nil2 = true
  70. }
  71. if row[3] == nil {
  72. nil3 = true
  73. }
  74. }
  75. So(nil1, ShouldBeTrue)
  76. So(nil2, ShouldBeTrue)
  77. So(nil3, ShouldBeTrue)
  78. })
  79. })
  80. }