datasource_plugin_wrapper_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package wrapper
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana-plugin-model/go/datasource"
  5. "github.com/grafana/grafana/pkg/infra/log"
  6. "github.com/grafana/grafana/pkg/tsdb"
  7. )
  8. func TestMapTables(t *testing.T) {
  9. dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
  10. var qr = &datasource.QueryResult{}
  11. qr.Tables = append(qr.Tables, &datasource.Table{
  12. Columns: []*datasource.TableColumn{},
  13. Rows: nil,
  14. })
  15. want := []*tsdb.Table{{}}
  16. have, err := dpw.mapTables(qr)
  17. if err != nil {
  18. t.Errorf("failed to map tables. error: %v", err)
  19. }
  20. if len(want) != len(have) {
  21. t.Errorf("could not map all tables")
  22. }
  23. }
  24. func TestMapTable(t *testing.T) {
  25. dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
  26. source := &datasource.Table{
  27. Columns: []*datasource.TableColumn{{Name: "column1"}, {Name: "column2"}},
  28. Rows: []*datasource.TableRow{{
  29. Values: []*datasource.RowValue{
  30. {
  31. Kind: datasource.RowValue_TYPE_BOOL,
  32. BoolValue: true,
  33. },
  34. {
  35. Kind: datasource.RowValue_TYPE_INT64,
  36. Int64Value: 42,
  37. },
  38. },
  39. }},
  40. }
  41. want := &tsdb.Table{
  42. Columns: []tsdb.TableColumn{{Text: "column1"}, {Text: "column2"}},
  43. }
  44. have, err := dpw.mapTable(source)
  45. if err != nil {
  46. t.Fatalf("failed to map table. error: %v", err)
  47. }
  48. for i := range have.Columns {
  49. if want.Columns[i] != have.Columns[i] {
  50. t.Fatalf("have column: %s, want %s", have, want)
  51. }
  52. }
  53. if len(have.Rows) != 1 {
  54. t.Fatalf("Expects one row but got %d", len(have.Rows))
  55. }
  56. rowValuesCount := len(have.Rows[0])
  57. if rowValuesCount != 2 {
  58. t.Fatalf("Expects two row values, got %d", rowValuesCount)
  59. }
  60. }
  61. func TestMappingRowValue(t *testing.T) {
  62. dpw := NewDatasourcePluginWrapper(log.New("test-logger"), nil)
  63. boolRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BOOL, BoolValue: true})
  64. haveBool, ok := boolRowValue.(bool)
  65. if !ok || !haveBool {
  66. t.Fatalf("Expected true, was %v", haveBool)
  67. }
  68. intRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_INT64, Int64Value: 42})
  69. haveInt, ok := intRowValue.(int64)
  70. if !ok || haveInt != 42 {
  71. t.Fatalf("Expected %d, was %d", 42, haveInt)
  72. }
  73. stringRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_STRING, StringValue: "grafana"})
  74. haveString, ok := stringRowValue.(string)
  75. if !ok || haveString != "grafana" {
  76. t.Fatalf("Expected %s, was %s", "grafana", haveString)
  77. }
  78. doubleRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_DOUBLE, DoubleValue: 1.5})
  79. haveDouble, ok := doubleRowValue.(float64)
  80. if !ok || haveDouble != 1.5 {
  81. t.Fatalf("Expected %v, was %v", 1.5, haveDouble)
  82. }
  83. bytesRowValue, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_BYTES, BytesValue: []byte{66}})
  84. haveBytes, ok := bytesRowValue.([]byte)
  85. if !ok || len(haveBytes) != 1 || haveBytes[0] != 66 {
  86. t.Fatalf("Expected %v, was %v", []byte{66}, haveBytes)
  87. }
  88. haveNil, _ := dpw.mapRowValue(&datasource.RowValue{Kind: datasource.RowValue_TYPE_NULL})
  89. if haveNil != nil {
  90. t.Fatalf("Expected %v, was %v", nil, haveNil)
  91. }
  92. }