@@ -4,9 +4,11 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"errors"
7
+ "fmt"
7
8
"testing"
8
9
9
10
"github.com/grafana/grafana-plugin-sdk-go/backend"
11
+ "github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
10
12
"github.com/grafana/sqlds/v4"
11
13
"github.com/grafana/sqlds/v4/test"
12
14
"github.com/stretchr/testify/assert"
@@ -31,7 +33,7 @@ func Test_query_retries(t *testing.T) {
31
33
QueryError : errors .New ("foo" ),
32
34
}
33
35
34
- req , handler , ds := queryRequest (t , "error" , opts , cfg )
36
+ req , handler , ds := queryRequest (t , "error" , opts , cfg , nil )
35
37
36
38
data , err := ds .QueryData (context .Background (), req )
37
39
assert .Nil (t , err )
@@ -56,7 +58,7 @@ func Test_query_apply_headers(t *testing.T) {
56
58
}
57
59
cfg := `{ "timeout": 0, "retries": 1, "retryOn": ["missing token"], "forwardHeaders": true }`
58
60
59
- req , handler , ds := queryRequest (t , "headers" , opts , cfg )
61
+ req , handler , ds := queryRequest (t , "headers" , opts , cfg , nil )
60
62
61
63
req .SetHTTPHeader ("foo" , "bar" )
62
64
@@ -99,8 +101,98 @@ func Test_no_errors(t *testing.T) {
99
101
assert .Equal (t , expected , result .Message )
100
102
}
101
103
102
- func queryRequest (t * testing.T , name string , opts test.DriverOpts , cfg string ) (* backend.QueryDataRequest , * test.SqlHandler , * sqlds.SQLDatasource ) {
103
- driver , handler := test .NewDriver (name , test.Data {}, nil , opts )
104
+ func Test_custom_marco_errors (t * testing.T ) {
105
+ cfg := `{ "timeout": 0, "retries": 0, "retryOn": ["foo"], query: "badArgumentCount" }`
106
+ opts := test.DriverOpts {}
107
+
108
+ badArgumentCountFunc := func (query * sqlds.Query , args []string ) (string , error ) {
109
+ return "" , sqlutil .ErrorBadArgumentCount
110
+ }
111
+ macros := sqlds.Macros {
112
+ "foo" : badArgumentCountFunc ,
113
+ }
114
+
115
+ req , _ , ds := queryRequest (t , "interpolate" , opts , cfg , macros )
116
+
117
+ req .Queries [0 ].JSON = []byte (`{ "rawSql": "select $__foo from bar;" }` )
118
+
119
+ data , err := ds .QueryData (context .Background (), req )
120
+ assert .Nil (t , err )
121
+
122
+ res := data .Responses ["foo" ]
123
+ assert .NotNil (t , res .Error )
124
+ assert .Equal (t , backend .ErrorSourceDownstream , res .ErrorSource )
125
+ assert .Contains (t , res .Error .Error (), sqlutil .ErrorBadArgumentCount .Error ())
126
+ }
127
+
128
+ func Test_default_macro_errors (t * testing.T ) {
129
+ tests := []struct {
130
+ name string
131
+ rawSQL string
132
+ wantError string
133
+ }{
134
+ {
135
+ name : "missing parameters" ,
136
+ rawSQL : "select * from bar where $__timeGroup(" ,
137
+ wantError : "missing close bracket" ,
138
+ },
139
+ {
140
+ name : "incorrect argument count 0 - timeGroup" ,
141
+ rawSQL : "select * from bar where $__timeGroup()" ,
142
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
143
+ },
144
+ {
145
+ name : "incorrect argument count 3 - timeGroup" ,
146
+ rawSQL : "select * from bar where $__timeGroup(1,2,3)" ,
147
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
148
+ },
149
+ {
150
+ name : "incorrect argument count 0 - timeFilter" ,
151
+ rawSQL : "select * from bar where $__timeFilter" ,
152
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
153
+ },
154
+ {
155
+ name : "incorrect argument count 3 - timeFilter" ,
156
+ rawSQL : "select * from bar where $__timeFilter(1,2,3)" ,
157
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
158
+ },
159
+ {
160
+ name : "incorrect argument count 0 - timeFrom" ,
161
+ rawSQL : "select * from bar where $__timeFrom" ,
162
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
163
+ },
164
+ {
165
+ name : "incorrect argument count 3 - timeFrom" ,
166
+ rawSQL : "select * from bar where $__timeFrom(1,2,3)" ,
167
+ wantError : sqlutil .ErrorBadArgumentCount .Error (),
168
+ },
169
+ }
170
+
171
+ // Common test configuration
172
+ cfg := `{ "timeout": 0, "retries": 0, "retryOn": ["foo"], query: "badArgumentCount" }`
173
+ opts := test.DriverOpts {}
174
+
175
+ for _ , tt := range tests {
176
+ t .Run (tt .name , func (t * testing.T ) {
177
+ // Setup request
178
+ req , _ , ds := queryRequest (t , "interpolate" , opts , cfg , nil )
179
+ req .Queries [0 ].JSON = []byte (fmt .Sprintf (`{ "rawSql": "%s" }` , tt .rawSQL ))
180
+
181
+ // Execute query
182
+ data , err := ds .QueryData (context .Background (), req )
183
+ assert .Nil (t , err )
184
+
185
+ // Verify response
186
+ res := data .Responses ["foo" ]
187
+ assert .NotNil (t , res .Error )
188
+ assert .Equal (t , backend .ErrorSourceDownstream , res .ErrorSource )
189
+ assert .Contains (t , res .Error .Error (), tt .wantError )
190
+ })
191
+ }
192
+ }
193
+
194
+ func queryRequest (t * testing.T , name string , opts test.DriverOpts , cfg string , marcos sqlds.Macros ) (* backend.QueryDataRequest , * test.SqlHandler , * sqlds.SQLDatasource ) {
195
+ driver , handler := test .NewDriver (name , test.Data {}, nil , opts , marcos )
104
196
ds := sqlds .NewDatasource (driver )
105
197
106
198
req , settings := setupQueryRequest (name , cfg )
@@ -126,7 +218,7 @@ func setupQueryRequest(id string, cfg string) (*backend.QueryDataRequest, backen
126
218
}
127
219
128
220
func healthRequest (t * testing.T , name string , opts test.DriverOpts , cfg string ) (backend.CheckHealthRequest , * test.SqlHandler , * sqlds.SQLDatasource ) {
129
- driver , handler := test .NewDriver (name , test.Data {}, nil , opts )
221
+ driver , handler := test .NewDriver (name , test.Data {}, nil , opts , nil )
130
222
ds := sqlds .NewDatasource (driver )
131
223
132
224
req , settings := setupHealthRequest (name , cfg )
0 commit comments