Skip to content

Commit 2de27e0

Browse files
authored
rest-dashboard: add serialization test (#168)
1 parent 0b798a8 commit 2de27e0

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

rest-dashboard_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99
"reflect"
1010
"strconv"
11+
"strings"
1112
"testing"
1213

1314
"github.com/grafana-tools/sdk"
@@ -340,3 +341,72 @@ func getRawBoardRequest(t *testing.T, preserveId bool) sdk.RawBoardRequest {
340341

341342
return r
342343
}
344+
345+
// TestCustomPanelSerialization serializes and de-serializes a sample dashboard
346+
// object a few times and prints out final serialized JSON data to highlight
347+
// that CustomPanel was nested in a strange way.
348+
func TestCustomPanelSerialization(t *testing.T) {
349+
board := &sdk.Board{
350+
ID: 0,
351+
UID: "",
352+
Slug: "",
353+
Title: "",
354+
OriginalTitle: "",
355+
Tags: nil,
356+
Style: "",
357+
Timezone: "",
358+
Editable: false,
359+
HideControls: false,
360+
SharedCrosshair: false,
361+
Panels: []*sdk.Panel{
362+
{
363+
CommonPanel: sdk.CommonPanel{},
364+
GraphPanel: nil,
365+
TablePanel: nil,
366+
TextPanel: nil,
367+
SinglestatPanel: nil,
368+
DashlistPanel: nil,
369+
PluginlistPanel: nil,
370+
RowPanel: nil,
371+
AlertlistPanel: nil,
372+
CustomPanel: &sdk.CustomPanel{
373+
"key": "value",
374+
},
375+
},
376+
},
377+
Rows: nil,
378+
Templating: sdk.Templating{},
379+
Annotations: struct {
380+
List []sdk.Annotation `json:"list"`
381+
}{},
382+
Refresh: nil,
383+
SchemaVersion: 0,
384+
Version: 0,
385+
Links: nil,
386+
Time: sdk.Time{},
387+
Timepicker: sdk.Timepicker{},
388+
GraphTooltip: 0,
389+
}
390+
391+
jb, err := json.Marshal(board)
392+
if err != nil {
393+
t.Fatal(err)
394+
}
395+
396+
for i := 0; i < 10; i++ {
397+
newBoard := &sdk.Board{}
398+
if err := json.Unmarshal(jb, newBoard); err != nil {
399+
t.Fatal(err)
400+
}
401+
402+
jb, err = json.MarshalIndent(newBoard, "", " ")
403+
if err != nil {
404+
t.Fatal(err)
405+
}
406+
}
407+
408+
cnt := strings.Count(string(jb), "CustomPanel")
409+
if cnt != 0 {
410+
t.Fatalf("expected to not have any CustomPanel keys, got: %v", cnt)
411+
}
412+
}

0 commit comments

Comments
 (0)