forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-dashboard_test.go
342 lines (313 loc) · 9.64 KB
/
rest-dashboard_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package sdk_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"testing"
"github.com/grafana-tools/sdk"
"github.com/stretchr/testify/assert"
)
func TestClient_SearchDashboards(t *testing.T) {
type in struct {
Query string
Starred bool
Tags []string
}
type testCase struct {
In in
Out url.Values
}
dashboard := string(sdk.SearchTypeDashboard)
for i, tc := range []testCase{
{
In: in{},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"false"}},
},
{
In: in{Starred: true},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"true"}},
},
{
In: in{Query: "Foo"},
Out: url.Values{"type": []string{dashboard}, "query": []string{"Foo"}, "starred": []string{"false"}},
},
{
In: in{Tags: []string{"Foo", "Bar"}},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"false"}, "tag": []string{"Foo", "Bar"}},
},
} {
ts := httptest.NewServer(http.HandlerFunc(testSearchQuery(t, i, tc.Out)))
client, _ := sdk.NewClient(ts.URL, "", ts.Client())
ctx := context.Background()
_, err := client.SearchDashboards(ctx, tc.In.Query, tc.In.Starred, tc.In.Tags...)
ts.Close()
if err != nil {
t.Fatalf("SearchDashboards test %d failed: %s", i, err)
}
}
}
func TestClient_Search(t *testing.T) {
type testCase struct {
In []sdk.SearchParam
Out url.Values
}
for i, tc := range []testCase{
{
In: []sdk.SearchParam{},
Out: url.Values{},
},
{
// Test all options given their correct usage.
In: []sdk.SearchParam{
sdk.SearchDashboardID(234),
sdk.SearchDashboardID(432),
sdk.SearchFolderID(123),
sdk.SearchFolderID(321),
sdk.SearchLimit(10),
sdk.SearchPage(99),
sdk.SearchQuery("Q"),
sdk.SearchStarred(true),
sdk.SearchTag("Foo"),
sdk.SearchTag("Bar"),
sdk.SearchType(sdk.SearchTypeFolder),
},
Out: url.Values{
"dashboardIds": []string{"234", "432"},
"folderIds": []string{"123", "321"},
"limit": []string{"10"},
"page": []string{"99"},
"query": []string{"Q"},
"starred": []string{"true"},
"tag": []string{"Foo", "Bar"},
"type": []string{string(sdk.SearchTypeFolder)},
},
},
{
// Test non-repeatable options.
In: []sdk.SearchParam{
sdk.SearchLimit(10),
sdk.SearchLimit(100),
sdk.SearchPage(88),
sdk.SearchPage(99),
sdk.SearchQuery("Q1"),
sdk.SearchQuery("Q2"),
sdk.SearchStarred(true),
sdk.SearchStarred(false),
sdk.SearchType(sdk.SearchTypeFolder),
sdk.SearchType(sdk.SearchTypeDashboard),
},
Out: url.Values{
"limit": []string{"100"},
"page": []string{"99"},
"query": []string{"Q2"},
"starred": []string{"false"},
"type": []string{string(sdk.SearchTypeDashboard)},
},
},
} {
ts := httptest.NewServer(http.HandlerFunc(testSearchQuery(t, i, tc.Out)))
client, _ := sdk.NewClient(ts.URL, "", ts.Client())
ctx := context.Background()
_, err := client.Search(ctx, tc.In...)
ts.Close()
if err != nil {
t.Fatalf("SearchDashboards test %d failed: %s", i, err)
}
}
}
func testSearchQuery(t *testing.T, testID int, exp url.Values) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if m := r.Method; m != http.MethodGet {
t.Fatalf("unexpected http method for case %d: expected %s, got %s", testID, http.MethodGet, m)
}
if e := "/api/search"; r.URL.Path != e {
t.Fatalf("unexpected http handler called for case %d: expected %s, got %s", testID, r.URL.Path, e)
}
qv, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
t.Fatalf("failed to parse query for case %d: %s, got err %s", testID, r.URL.RawQuery, err)
}
if !reflect.DeepEqual(exp, qv) {
t.Fatalf("unexpected query arguments for case %d: expected %v, got %v", testID, exp, qv)
}
if _, err := w.Write([]byte("[]")); err != nil {
t.Fatalf("failed to write http answer for case %d: %s", testID, err)
}
}
}
func TestSearchQuery(t *testing.T) {
testStringSearchParam(t, sdk.SearchQuery, "query", []string{"foo", "bar"})
}
func TestSearchTag(t *testing.T) {
testRepeatableStringSearchParam(t, sdk.SearchTag, "tag", []string{"foo", "bar"})
}
func TestSearchDashboardID(t *testing.T) {
testRepeatableIntSearchParam(t, sdk.SearchDashboardID, "dashboardIds", []int{100, 200})
}
func TestSearchFolderID(t *testing.T) {
testRepeatableIntSearchParam(t, sdk.SearchFolderID, "folderIds", []int{100, 200})
}
func TestSearchPage(t *testing.T) {
testNonZeroUIntSearchParam(t, sdk.SearchPage, "page", []uint{100, 200})
}
func TestSearchLimit(t *testing.T) {
testNonZeroUIntSearchParam(t, sdk.SearchLimit, "limit", []uint{100, 200})
}
func TestSearchStarred(t *testing.T) {
testBoolSearchParam(t, sdk.SearchStarred, "starred", []bool{true, false})
}
func TestSearchType(t *testing.T) {
var (
sp = sdk.SearchType
key = "type"
testValues = []sdk.SearchParamType{sdk.SearchTypeFolder, sdk.SearchTypeDashboard}
)
v := make(url.Values)
for _, testValue := range testValues {
sp(testValue)(&v)
expectedLen := 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := v.Get(key)
if value != string(testValue) {
t.Errorf("expected value of %s to be %s, but was %s", key, testValue, value)
}
}
}
func testRepeatableStringSearchParam(t *testing.T, sp func(string) sdk.SearchParam, key string, testValues []string) {
v := make(url.Values)
for i, testValue := range testValues {
sp(testValue)(&v)
expectedLen := i + 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
last := v[key][i]
if last != testValue {
t.Errorf("expected last %s to be %s, but was %s", key, testValue, last)
}
}
}
func testStringSearchParam(t *testing.T, sp func(string) sdk.SearchParam, key string, testValues []string) {
v := make(url.Values)
for _, testValue := range testValues {
sp(testValue)(&v)
expectedLen := 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := v.Get(key)
if value != testValue {
t.Errorf("expected value of %s to be %s, but was %s", key, testValue, value)
}
}
}
func testBoolSearchParam(t *testing.T, sp func(bool) sdk.SearchParam, key string, testValues []bool) {
v := make(url.Values)
for _, testValue := range testValues {
sp(testValue)(&v)
expectedLen := 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := v.Get(key)
if value != strconv.FormatBool(testValue) {
t.Errorf("expected value of %s to be %t, but was %s", key, testValue, value)
}
}
}
func testRepeatableIntSearchParam(t *testing.T, sp func(int) sdk.SearchParam, key string, testValues []int) {
v := make(url.Values)
for i, testValue := range testValues {
sp(testValue)(&v)
expectedLen := i + 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
last := v[key][i]
if last != strconv.Itoa(testValue) {
t.Errorf("expected last %s to be %d, but was %s", key, testValue, last)
}
}
}
func testNonZeroUIntSearchParam(t *testing.T, sp func(uint) sdk.SearchParam, key string, testValues []uint) {
v := make(url.Values)
sp(0)(&v)
value := v.Get(key)
if value != "" {
t.Errorf("expected value of %s to be unset, but was %s", key, value)
}
testUIntSearchParam(t, sp, key, testValues)
}
func testUIntSearchParam(t *testing.T, sp func(uint) sdk.SearchParam, key string, testValues []uint) {
v := make(url.Values)
for _, testValue := range testValues {
sp(testValue)(&v)
expectedLen := 1
gotLen := len(v[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := v.Get(key)
if value != strconv.FormatUint(uint64(testValue), 10) {
t.Errorf("expected value of %s to be %d, but was %s", key, testValue, value)
}
}
}
func Test_DecodeRawBoardPreserveId(t *testing.T) {
r := getRawBoardRequest(t, true)
//Serialize object
data, err := json.Marshal(r)
assert.Nil(t, err)
//Deserialize data
var rawData map[string]interface{} = make(map[string]interface{})
assert.Nil(t, json.Unmarshal(data, &rawData))
folderId := rawData["FolderID"]
assert.Equal(t, folderId.(float64), float64(27))
assert.Equal(t, rawData["Overwrite"].(bool), true)
rawData = rawData["dashboard"].(map[string]interface{})
assert.Equal(t, rawData["id"].(float64), float64(25))
assert.Equal(t, rawData["title"].(string), "woot")
}
func Test_DecodeRawBoardWipeId(t *testing.T) {
r := getRawBoardRequest(t, false)
//Serialize object
data, err := json.Marshal(r)
assert.Nil(t, err)
//Deserialize data
var rawData map[string]interface{} = make(map[string]interface{})
err = json.Unmarshal(data, &rawData)
assert.Nil(t, err)
folderId := rawData["FolderID"]
assert.Equal(t, folderId.(float64), float64(27))
assert.Equal(t, rawData["Overwrite"].(bool), true)
rawData = rawData["dashboard"].(map[string]interface{})
assert.Equal(t, rawData["id"].(float64), float64(0))
assert.Equal(t, rawData["title"].(string), "woot")
}
func getRawBoardRequest(t *testing.T, preserveId bool) sdk.RawBoardRequest {
r := sdk.RawBoardRequest{}
board := sdk.Board{}
board.ID = 25
board.Title = "woot"
rawBoard, err := json.Marshal(board)
assert.Nil(t, err)
params := sdk.SetDashboardParams{
FolderID: 27,
Overwrite: true,
PreserveId: preserveId,
}
r.Dashboard = rawBoard
r.Parameters = params
return r
}