forked from grafana/sqlds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
154 lines (125 loc) · 3.89 KB
/
query_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package sqlds
import (
"context"
"database/sql"
"errors"
"fmt"
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/stretchr/testify/require"
)
var (
errorPingCompleted = errors.New("ping completed")
errorQueryCompleted = errors.New("query completed")
)
type testConnection struct {
PingWait time.Duration
QueryWait time.Duration
QueryRunCount int
}
func (t *testConnection) Close() error {
t.QueryRunCount = 0
return nil
}
func (t *testConnection) Ping() error {
return errorPingCompleted
}
func (t *testConnection) PingContext(ctx context.Context) error {
done := make(chan bool)
go func() {
time.Sleep(t.QueryWait)
done <- true
}()
select {
case <-ctx.Done():
return context.Canceled
case <-done:
return errorPingCompleted
}
}
func (t *testConnection) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
t.QueryRunCount++
done := make(chan bool)
go func() {
time.Sleep(t.QueryWait)
done <- true
}()
select {
case <-ctx.Done():
return nil, context.Canceled
case <-done:
return nil, errorQueryCompleted
}
}
func TestQuery_Timeout(t *testing.T) {
t.Run("it should return context.Canceled if the query timeout is exceeded", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
conn := &testConnection{
PingWait: time.Second * 5,
QueryWait: time.Second * 5,
}
defer conn.Close()
_, err := QueryDB(ctx, conn, []sqlutil.Converter{}, nil, &Query{})
if !errors.Is(err, context.Canceled) {
t.Fatal("expected error to be context.Canceled, received", err)
}
if conn.QueryRunCount != 1 {
t.Fatal("expected the querycontext function to run only once, but ran", conn.QueryRunCount, "times")
}
})
t.Run("it should run to completion and not return a query timeout error", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
conn := &testConnection{
PingWait: time.Second,
QueryWait: time.Second,
}
defer conn.Close()
_, err := QueryDB(ctx, conn, []sqlutil.Converter{}, nil, &Query{})
if !errors.Is(err, ErrorQuery) {
t.Fatal("expected function to complete, received error: ", err)
}
})
}
func TestFixFrameForLongToMulti(t *testing.T) {
t.Run("fix time", func(t *testing.T) {
time1 := time.UnixMilli(1)
time2 := time.UnixMilli(2)
frame := data.NewFrame("",
data.NewField("time", nil, []*time.Time{&time1, &time2}),
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("iface", nil, []string{"eth0", "eth0"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
data.NewField("out_bytes", nil, []int64{3, 4}),
)
err := fixFrameForLongToMulti(frame)
require.NoError(t, err)
require.Equal(t, frame.Fields[0].Type(), data.FieldTypeTime)
require.Equal(t, frame.Fields[0].Len(), 2)
require.Equal(t, frame.Fields[0].At(0).(time.Time), time1)
require.Equal(t, frame.Fields[0].At(1).(time.Time), time2)
require.Equal(t, frame.Meta.Type, data.FrameTypeTimeSeriesLong)
require.Equal(t, frame.Meta.TypeVersion, data.FrameTypeVersion{0, 1})
})
t.Run("errors for null time", func(t *testing.T) {
time1 := time.UnixMilli(1)
frame := data.NewFrame("",
data.NewField("time", nil, []*time.Time{&time1, nil}),
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
)
err := fixFrameForLongToMulti(frame)
require.Equal(t, err, fmt.Errorf("can not convert to wide series, input has null time values"))
})
t.Run("error for no time", func(t *testing.T) {
frame := data.NewFrame("",
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
)
err := fixFrameForLongToMulti(frame)
require.Equal(t, err, fmt.Errorf("can not convert to wide series, input is missing a time field"))
})
}