From ec40e49dcb92449a3f9e8ed1aa4ca06efe1bff4d Mon Sep 17 00:00:00 2001 From: Arve Knudsen Date: Wed, 21 Oct 2020 10:09:42 +0200 Subject: [PATCH] Chore: Rewrite some tests to use testify (#28420) * Chore: Improve tests to use testify Signed-off-by: Arve Knudsen --- .../metrics/graphitebridge/graphite_test.go | 17 ++--- .../wrapper/datasource_plugin_wrapper_test.go | 63 ++++++------------- pkg/tsdb/sqleng/sql_engine_test.go | 18 ++---- 3 files changed, 29 insertions(+), 69 deletions(-) diff --git a/pkg/infra/metrics/graphitebridge/graphite_test.go b/pkg/infra/metrics/graphitebridge/graphite_test.go index 4116c4db63f..dd4af83ef6c 100644 --- a/pkg/infra/metrics/graphitebridge/graphite_test.go +++ b/pkg/infra/metrics/graphitebridge/graphite_test.go @@ -12,6 +12,7 @@ import ( "github.com/prometheus/client_golang/prometheus" dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/model" + "github.com/stretchr/testify/require" ) func TestCountersAsDelta(t *testing.T) { @@ -30,14 +31,10 @@ func TestCountersAsDelta(t *testing.T) { var got float64 want = float64(1) got = b.replaceCounterWithDelta(mf, m, model.SampleValue(1)) - if got != want { - t.Fatalf("want %v got %v", want, got) - } + require.Equal(t, want, got) got = b.replaceCounterWithDelta(mf, m, model.SampleValue(2)) - if got != want { - t.Fatalf("want %v got %v", want, got) - } + require.Equal(t, want, got) } func TestCountersAsDeltaDisabled(t *testing.T) { @@ -56,15 +53,11 @@ func TestCountersAsDeltaDisabled(t *testing.T) { var got float64 want = float64(1) got = b.replaceCounterWithDelta(mf, m, model.SampleValue(1)) - if got != want { - t.Fatalf("want %v got %v", want, got) - } + require.Equal(t, want, got) want = float64(2) got = b.replaceCounterWithDelta(mf, m, model.SampleValue(2)) - if got != want { - t.Fatalf("want %v got %v", want, got) - } + require.Equal(t, want, got) } func TestSanitize(t *testing.T) { diff --git a/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go b/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go index 2086ea4b991..fbb9518333c 100644 --- a/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go +++ b/pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go @@ -6,6 +6,7 @@ import ( "github.com/grafana/grafana-plugin-model/go/datasource" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/tsdb" + "github.com/stretchr/testify/require" ) func TestMapTables(t *testing.T) { @@ -15,15 +16,10 @@ func TestMapTables(t *testing.T) { Columns: []*datasource.TableColumn{}, Rows: nil, }) - want := []*tsdb.Table{{}} have, err := dpw.mapTables(qr) - if err != nil { - t.Errorf("failed to map tables. error: %v", err) - } - if len(want) != len(have) { - t.Errorf("could not map all tables") - } + require.NoError(t, err) + require.Len(t, have, 1) } func TestMapTable(t *testing.T) { @@ -49,24 +45,11 @@ func TestMapTable(t *testing.T) { Columns: []tsdb.TableColumn{{Text: "column1"}, {Text: "column2"}}, } have, err := dpw.mapTable(source) - if err != nil { - t.Fatalf("failed to map table. error: %v", err) - } + require.NoError(t, err) - for i := range have.Columns { - if want.Columns[i] != have.Columns[i] { - t.Fatalf("have column: %s, want %s", have, want) - } - } - - if len(have.Rows) != 1 { - t.Fatalf("Expects one row but got %d", len(have.Rows)) - } - - rowValuesCount := len(have.Rows[0]) - if rowValuesCount != 2 { - t.Fatalf("Expects two row values, got %d", rowValuesCount) - } + require.Equal(t, want.Columns, have.Columns) + require.Len(t, have.Rows, 1) + require.Len(t, have.Rows[0], 2) } func TestMappingRowValue(t *testing.T) { @@ -74,36 +57,30 @@ func TestMappingRowValue(t *testing.T) { boolRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BOOL, BoolValue: true}) haveBool, ok := boolRowValue.(bool) - if !ok || !haveBool { - t.Fatalf("Expected true, was %v", haveBool) - } + require.True(t, ok) + require.True(t, haveBool) intRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_INT64, Int64Value: 42}) haveInt, ok := intRowValue.(int64) - if !ok || haveInt != 42 { - t.Fatalf("Expected %d, was %d", 42, haveInt) - } + require.True(t, ok) + require.Equal(t, int64(42), haveInt) stringRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_STRING, StringValue: "grafana"}) haveString, ok := stringRowValue.(string) - if !ok || haveString != "grafana" { - t.Fatalf("Expected %s, was %s", "grafana", haveString) - } + require.True(t, ok) + require.Equal(t, "grafana", haveString) doubleRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_DOUBLE, DoubleValue: 1.5}) haveDouble, ok := doubleRowValue.(float64) - if !ok || haveDouble != 1.5 { - t.Fatalf("Expected %v, was %v", 1.5, haveDouble) - } + require.True(t, ok) + require.Equal(t, 1.5, haveDouble) bytesRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BYTES, BytesValue: []byte{66}}) haveBytes, ok := bytesRowValue.([]byte) - if !ok || len(haveBytes) != 1 || haveBytes[0] != 66 { - t.Fatalf("Expected %v, was %v", []byte{66}, haveBytes) - } + require.True(t, ok) + require.Equal(t, []byte{66}, haveBytes) - haveNil, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_NULL}) - if haveNil != nil { - t.Fatalf("Expected %v, was %v", nil, haveNil) - } + haveNil, err := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_NULL}) + require.NoError(t, err) + require.Nil(t, haveNil) } diff --git a/pkg/tsdb/sqleng/sql_engine_test.go b/pkg/tsdb/sqleng/sql_engine_test.go index 00e5bdccbc0..0c80ec64534 100644 --- a/pkg/tsdb/sqleng/sql_engine_test.go +++ b/pkg/tsdb/sqleng/sql_engine_test.go @@ -307,27 +307,17 @@ func TestSqlEngine(t *testing.T) { for _, f := range fixtures { value, _ := ConvertSqlValueColumnToFloat("col", f) - if !value.Valid { - t.Fatalf("Failed to convert %T value, expected a valid float value", f) - } - - if value.Float64 != null.FloatFrom(1).Float64 { - t.Fatalf("Failed to convert %T value, expected a float value of 1.000, but got %v", f, value) - } + So(value.Valid, ShouldBeTrue) + So(value.Float64, ShouldEqual, null.FloatFrom(1).Float64) } }) Convey("When converting nil pointer values to float should return expected value", func() { for _, f := range nilPointerFixtures { value, err := ConvertSqlValueColumnToFloat("col", f) + So(err, ShouldBeNil) - if err != nil { - t.Fatalf("Failed to convert %T value, expected a non nil error, but got %v", f, err) - } - - if value.Valid { - t.Fatalf("Failed to convert %T value, expected an invalid float value", f) - } + So(value.Valid, ShouldBeFalse) } }) })