Commit 69ac69b7 by Emil Hessman Committed by GitHub

Chore: Rewrite tsdb testdatasource scenarios test to standard library (#29591)

Signed-off-by: Emil Hessman <emil@hessman.se>
parent 10539896
...@@ -6,14 +6,14 @@ import ( ...@@ -6,14 +6,14 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/require"
) )
func TestTestdataScenarios(t *testing.T) { func TestTestdataScenarios(t *testing.T) {
Convey("random walk ", t, func() { t.Run("random walk ", func(t *testing.T) {
scenario := ScenarioRegistry["random_walk"] scenario := ScenarioRegistry["random_walk"]
Convey("Should start at the requested value", func() { t.Run("Should start at the requested value", func(t *testing.T) {
req := &tsdb.TsdbQuery{ req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()), TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{ Queries: []*tsdb.Query{
...@@ -24,17 +24,17 @@ func TestTestdataScenarios(t *testing.T) { ...@@ -24,17 +24,17 @@ func TestTestdataScenarios(t *testing.T) {
query.Model.Set("startValue", 1.234) query.Model.Set("startValue", 1.234)
result := scenario.Handler(req.Queries[0], req) result := scenario.Handler(req.Queries[0], req)
points := result.Series[0].Points require.NotNil(t, result.Series)
So(result.Series, ShouldNotBeNil) points := result.Series[0].Points
So(points[0][0].Float64, ShouldEqual, 1.234) require.Equal(t, 1.234, points[0][0].Float64)
}) })
}) })
Convey("random walk table", t, func() { t.Run("random walk table", func(t *testing.T) {
scenario := ScenarioRegistry["random_walk_table"] scenario := ScenarioRegistry["random_walk_table"]
Convey("Should return a table that looks like value/min/max", func() { t.Run("Should return a table that looks like value/min/max", func(t *testing.T) {
req := &tsdb.TsdbQuery{ req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()), TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{ Queries: []*tsdb.Query{
...@@ -45,18 +45,18 @@ func TestTestdataScenarios(t *testing.T) { ...@@ -45,18 +45,18 @@ func TestTestdataScenarios(t *testing.T) {
result := scenario.Handler(req.Queries[0], req) result := scenario.Handler(req.Queries[0], req)
table := result.Tables[0] table := result.Tables[0]
So(len(table.Rows), ShouldBeGreaterThan, 50) require.Greater(t, len(table.Rows), 50)
for _, row := range table.Rows { for _, row := range table.Rows {
value := row[1] value := row[1]
min := row[2] min := row[2]
max := row[3] max := row[3]
So(min, ShouldBeLessThan, value) require.Less(t, min, value)
So(max, ShouldBeGreaterThan, value) require.Greater(t, max, value)
} }
}) })
Convey("Should return a table with some nil values", func() { t.Run("Should return a table with some nil values", func(t *testing.T) {
req := &tsdb.TsdbQuery{ req := &tsdb.TsdbQuery{
TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()), TimeRange: tsdb.NewFakeTimeRange("5m", "now", time.Now()),
Queries: []*tsdb.Query{ Queries: []*tsdb.Query{
...@@ -73,7 +73,7 @@ func TestTestdataScenarios(t *testing.T) { ...@@ -73,7 +73,7 @@ func TestTestdataScenarios(t *testing.T) {
nil2 := false nil2 := false
nil3 := false nil3 := false
So(len(table.Rows), ShouldBeGreaterThan, 50) require.Greater(t, len(table.Rows), 50)
for _, row := range table.Rows { for _, row := range table.Rows {
if row[1] == nil { if row[1] == nil {
nil1 = true nil1 = true
...@@ -86,41 +86,37 @@ func TestTestdataScenarios(t *testing.T) { ...@@ -86,41 +86,37 @@ func TestTestdataScenarios(t *testing.T) {
} }
} }
So(nil1, ShouldBeTrue) require.True(t, nil1)
So(nil2, ShouldBeTrue) require.True(t, nil2)
So(nil3, ShouldBeTrue) require.True(t, nil3)
}) })
}) })
} }
func TestToLabels(t *testing.T) { func TestParseLabels(t *testing.T) {
Convey("read labels", t, func() { expectedTags := map[string]string{
tags := make(map[string]string) "job": "foo",
tags["job"] = "foo" "instance": "bar",
tags["instance"] = "bar" }
query1 := tsdb.Query{ query1 := tsdb.Query{
Model: simplejson.NewFromAny(map[string]interface{}{ Model: simplejson.NewFromAny(map[string]interface{}{
"labels": `{job="foo", instance="bar"}`, "labels": `{job="foo", instance="bar"}`,
}), }),
} }
require.Equal(t, expectedTags, parseLabels(&query1))
So(parseLabels(&query1), ShouldResemble, tags)
query2 := tsdb.Query{
query2 := tsdb.Query{ Model: simplejson.NewFromAny(map[string]interface{}{
Model: simplejson.NewFromAny(map[string]interface{}{ "labels": `job=foo, instance=bar`,
"labels": `job=foo, instance=bar`, }),
}), }
} require.Equal(t, expectedTags, parseLabels(&query2))
So(parseLabels(&query2), ShouldResemble, tags) query3 := tsdb.Query{
Model: simplejson.NewFromAny(map[string]interface{}{
query3 := tsdb.Query{ "labels": `job = foo,instance = bar`,
Model: simplejson.NewFromAny(map[string]interface{}{ }),
"labels": `job = foo,instance = bar`, }
}), require.Equal(t, expectedTags, parseLabels(&query3))
}
So(parseLabels(&query3), ShouldResemble, tags)
})
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment