Skip to content

Commit 6631da7

Browse files
authored
unit-tests for zero-rows-returned situations (#119)
* unit-tests for zero-rows-returned situations * added more formats
1 parent cf826a5 commit 6631da7

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

dataframe_test.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package sqlds_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
"time"
8+
9+
"github.com/grafana/grafana-plugin-sdk-go/backend"
10+
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
11+
"github.com/grafana/sqlds/v3"
12+
"github.com/grafana/sqlds/v3/test"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
// we test how no-rows sql responses are converted to dataframes
17+
func TestNoRowsFrame(t *testing.T) {
18+
19+
tts := []struct {
20+
name string
21+
data test.Data
22+
format sqlutil.FormatQueryOption
23+
expectedFieldCount int
24+
}{
25+
{
26+
name: "empty table",
27+
format: sqlutil.FormatOptionTable,
28+
data: test.Data{
29+
Cols: []test.Column{
30+
{
31+
Name: "name",
32+
DataType: "TEXT",
33+
Kind: "",
34+
},
35+
{
36+
Name: "age",
37+
DataType: "INTEGER",
38+
Kind: int64(0),
39+
},
40+
},
41+
Rows: [][]any{},
42+
},
43+
expectedFieldCount: 0,
44+
},
45+
{
46+
name: "empty wide",
47+
format: sqlutil.FormatOptionTimeSeries,
48+
data: test.Data{
49+
Cols: []test.Column{
50+
{
51+
Name: "time",
52+
DataType: "TIMESTAMP",
53+
Kind: time.Unix(0, 0),
54+
},
55+
{
56+
Name: "v1",
57+
DataType: "FLOAT",
58+
Kind: float64(0),
59+
},
60+
{
61+
Name: "v2",
62+
DataType: "FLOAT",
63+
Kind: float64(0),
64+
},
65+
},
66+
Rows: [][]any{},
67+
},
68+
expectedFieldCount: 0,
69+
},
70+
{
71+
name: "empty long",
72+
format: sqlutil.FormatOptionTimeSeries,
73+
data: test.Data{
74+
Cols: []test.Column{
75+
{
76+
Name: "time",
77+
DataType: "TIMESTAMP",
78+
Kind: time.Unix(0, 0),
79+
},
80+
{
81+
Name: "tag",
82+
DataType: "TEXT",
83+
Kind: "",
84+
},
85+
{
86+
Name: "value",
87+
DataType: "FLOAT",
88+
Kind: float64(0),
89+
},
90+
},
91+
Rows: [][]any{},
92+
},
93+
expectedFieldCount: 0,
94+
},
95+
{
96+
name: "empty multi",
97+
format: sqlutil.FormatOptionMulti,
98+
data: test.Data{
99+
Cols: []test.Column{
100+
{
101+
Name: "time",
102+
DataType: "TIMESTAMP",
103+
Kind: time.Unix(0, 0),
104+
},
105+
{
106+
Name: "tag",
107+
DataType: "TEXT",
108+
Kind: "",
109+
},
110+
{
111+
Name: "value",
112+
DataType: "FLOAT",
113+
Kind: float64(0),
114+
},
115+
},
116+
Rows: [][]any{},
117+
},
118+
expectedFieldCount: 0,
119+
},
120+
{
121+
name: "logs",
122+
format: sqlutil.FormatOptionLogs,
123+
data: test.Data{
124+
Cols: []test.Column{
125+
{
126+
Name: "time",
127+
DataType: "TIMESTAMP",
128+
Kind: time.Unix(0, 0),
129+
},
130+
{
131+
Name: "text",
132+
DataType: "TEXT",
133+
Kind: "",
134+
},
135+
},
136+
Rows: [][]any{},
137+
},
138+
expectedFieldCount: 0,
139+
},
140+
{
141+
name: "trace",
142+
format: sqlutil.FormatOptionLogs,
143+
data: test.Data{
144+
Cols: []test.Column{
145+
{
146+
Name: "time",
147+
DataType: "TIMESTAMP",
148+
Kind: time.Unix(0, 0),
149+
},
150+
// FIXME: i do not know what kind of data is in trace-frames
151+
},
152+
Rows: [][]any{},
153+
},
154+
expectedFieldCount: 0,
155+
},
156+
}
157+
158+
for _, tt := range tts {
159+
t.Run(tt.name, func(t *testing.T) {
160+
id := "empty-frames" + tt.name
161+
driver, _ := test.NewDriver(id, tt.data, nil, test.DriverOpts{})
162+
ds := sqlds.NewDatasource(driver)
163+
164+
settings := backend.DataSourceInstanceSettings{UID: id, JSONData: []byte("{}")}
165+
_, err := ds.NewDatasource(context.Background(), settings)
166+
167+
require.NoError(t, err)
168+
169+
req := backend.QueryDataRequest{
170+
PluginContext: backend.PluginContext{
171+
DataSourceInstanceSettings: &settings,
172+
},
173+
Queries: []backend.DataQuery{
174+
{
175+
RefID: "A",
176+
JSON: []byte(fmt.Sprintf(`{ "rawSql": "SELECT 42", "format": %d }`, tt.format)),
177+
},
178+
},
179+
}
180+
181+
r, err := ds.QueryData(context.Background(), &req)
182+
require.NoError(t, err)
183+
d := r.Responses["A"]
184+
require.NotNil(t, d)
185+
require.Len(t, d.Frames, 1)
186+
require.Len(t, d.Frames[0].Fields, tt.expectedFieldCount)
187+
188+
})
189+
}
190+
}

0 commit comments

Comments
 (0)