scenarios_test.go 2.3 KB

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