grafana/pkg/services/live/pipeline/converter_json_exact_test.go

97 lines
2.2 KiB
Go

package pipeline
import (
"context"
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/experimental"
"github.com/stretchr/testify/require"
)
func checkExactConversion(t *testing.T, file string, fields []Field) *backend.DataResponse {
t.Helper()
content := loadTestJson(t, file)
converter := NewExactJsonConverter(ExactJsonConverterConfig{
Fields: fields,
})
converter.nowTimeFunc = func() time.Time {
return time.Date(2021, 01, 01, 12, 12, 12, 0, time.UTC)
}
channelFrames, err := converter.Convert(context.Background(), Vars{}, content)
require.NoError(t, err)
dr := &backend.DataResponse{}
for _, cf := range channelFrames {
require.Empty(t, cf.Channel)
dr.Frames = append(dr.Frames, cf.Frame)
}
experimental.CheckGoldenJSONResponse(t, "testdata", file+".golden", dr, *update)
return dr
}
func BenchmarkExactJsonConverter_Convert(b *testing.B) {
content := loadTestJson(b, "json_exact")
converter := NewExactJsonConverter(ExactJsonConverterConfig{
Fields: []Field{
{
Name: "ax",
Value: "$.ax",
Type: data.FieldTypeNullableFloat64,
}, {
Name: "array_value",
Value: "$.string_array[0]",
Type: data.FieldTypeNullableString,
}, {
Name: "map_key",
Value: "$.map_with_floats['key1']",
Type: data.FieldTypeNullableFloat64,
},
},
})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := converter.Convert(context.Background(), Vars{}, content)
require.NoError(b, err)
//require.Len(b, cf, 1)
//require.Len(b, cf[0].Frame.Fields, 3)
}
}
func TestExactJsonConverter_Convert(t *testing.T) {
checkExactConversion(t, "json_exact", []Field{
{
Name: "time",
Value: "#{now}",
Type: data.FieldTypeTime,
},
{
Name: "ax",
Value: "$.ax",
Type: data.FieldTypeNullableFloat64,
},
{
Name: "key1",
Value: "{x.map_with_floats.key1}",
Type: data.FieldTypeNullableFloat64,
Labels: []Label{
{
Name: "label1",
Value: "{x.map_with_floats.key2.toString()}",
},
{
Name: "label2",
Value: "$.map_with_floats.key2",
},
},
},
})
}