Files
grafana/pkg/plugins/datasource_plugin_wrapper_test.go
T

87 lines
2.5 KiB
Go
Raw Normal View History

2021-03-08 07:02:49 +01:00
package plugins
2018-01-11 17:29:25 +01:00
import (
"testing"
2018-05-24 15:26:27 +02:00
"github.com/grafana/grafana-plugin-model/go/datasource"
2019-05-13 14:45:54 +08:00
"github.com/grafana/grafana/pkg/infra/log"
"github.com/stretchr/testify/require"
2018-01-11 17:29:25 +01:00
)
func TestMapTables(t *testing.T) {
2021-03-08 07:02:49 +01:00
dpw := newDataSourcePluginWrapper(log.New("test-logger"), nil)
var qr = &datasource.QueryResult{}
qr.Tables = append(qr.Tables, &datasource.Table{
Columns: []*datasource.TableColumn{},
2018-01-11 17:29:25 +01:00
Rows: nil,
})
have, err := dpw.mapTables(qr)
require.NoError(t, err)
require.Len(t, have, 1)
2018-01-11 17:29:25 +01:00
}
func TestMapTable(t *testing.T) {
2021-03-08 07:02:49 +01:00
dpw := newDataSourcePluginWrapper(log.New("test-logger"), nil)
2018-01-11 17:29:25 +01:00
source := &datasource.Table{
Columns: []*datasource.TableColumn{{Name: "column1"}, {Name: "column2"}},
Rows: []*datasource.TableRow{{
Values: []*datasource.RowValue{
2018-01-11 17:29:25 +01:00
{
Kind: datasource.RowValue_TYPE_BOOL,
2018-01-11 17:29:25 +01:00
BoolValue: true,
},
{
Kind: datasource.RowValue_TYPE_INT64,
2018-01-11 17:29:25 +01:00
Int64Value: 42,
},
},
}},
}
2021-03-08 07:02:49 +01:00
want := DataTable{
Columns: []DataTableColumn{{Text: "column1"}, {Text: "column2"}},
2018-01-11 17:29:25 +01:00
}
have, err := dpw.mapTable(source)
require.NoError(t, err)
2018-01-11 17:29:25 +01:00
require.Equal(t, want.Columns, have.Columns)
require.Len(t, have.Rows, 1)
require.Len(t, have.Rows[0], 2)
2018-01-11 17:29:25 +01:00
}
func TestMappingRowValue(t *testing.T) {
2021-03-08 07:02:49 +01:00
dpw := newDataSourcePluginWrapper(log.New("test-logger"), nil)
2018-01-11 17:29:25 +01:00
2021-03-08 07:02:49 +01:00
boolRowValue, err := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BOOL, BoolValue: true})
require.NoError(t, err)
2018-01-11 17:29:25 +01:00
haveBool, ok := boolRowValue.(bool)
require.True(t, ok)
require.True(t, haveBool)
2018-01-11 17:29:25 +01:00
intRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_INT64, Int64Value: 42})
2018-01-11 17:29:25 +01:00
haveInt, ok := intRowValue.(int64)
require.True(t, ok)
require.Equal(t, int64(42), haveInt)
2018-01-11 17:29:25 +01:00
stringRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_STRING, StringValue: "grafana"})
2018-01-11 17:29:25 +01:00
haveString, ok := stringRowValue.(string)
require.True(t, ok)
require.Equal(t, "grafana", haveString)
2018-01-11 17:29:25 +01:00
doubleRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_DOUBLE, DoubleValue: 1.5})
2018-01-11 17:29:25 +01:00
haveDouble, ok := doubleRowValue.(float64)
require.True(t, ok)
require.Equal(t, 1.5, haveDouble)
2018-01-11 17:29:25 +01:00
bytesRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BYTES, BytesValue: []byte{66}})
2018-01-11 17:29:25 +01:00
haveBytes, ok := bytesRowValue.([]byte)
require.True(t, ok)
require.Equal(t, []byte{66}, haveBytes)
2018-01-11 17:29:25 +01:00
haveNil, err := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_NULL})
require.NoError(t, err)
require.Nil(t, haveNil)
2018-01-11 17:29:25 +01:00
}