Skip to content

Commit

Permalink
Fix templating init with options field and add unit test (grafana-too…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuzminykh Aleksandr Olegovich committed Oct 19, 2021
1 parent 56cdea6 commit 80ec5ce
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion board.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type (
AutoCount *int `json:"auto_count,omitempty"`
Datasource *string `json:"datasource"`
Refresh BoolInt `json:"refresh"`
Options []Option `json:"options"`
Options []Option `json:"options,omitempty"`
IncludeAll bool `json:"includeAll"`
AllFormat string `json:"allFormat"`
AllValue string `json:"allValue"`
Expand Down
49 changes: 49 additions & 0 deletions rest-dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,52 @@ func TestCustomPanelSerialization(t *testing.T) {
t.Fatalf("expected to not have any CustomPanel keys, got: %v", cnt)
}
}

func TestTemplatingOptionsSerialization(t *testing.T) {

for i := 0; i < 2; i++ {

var options []sdk.Option = nil

if i == 1 {
options = []sdk.Option{{}}
}

board := &sdk.Board{
Templating: sdk.Templating{List: []sdk.TemplateVar{
{
Options: options,
},
}},
}

boardJson, err := json.Marshal(board)
if err != nil {
t.Fatal(err)
}

var boardMap map[string]interface{}

err = json.Unmarshal(boardJson, &boardMap)
if err != nil {
t.Fatal(err)
}

templatingMap, found := boardMap["templating"].(map[string]interface{})
if !found {
t.Fatal()
}

list, found := templatingMap["list"].([]interface{})
if !found {
t.Fatal()
}

_, found = list[0].(map[string]interface{})["options"]
if options == nil && found {
t.Fatal()
} else if options != nil && !found {
t.Fatal()
}
}
}

0 comments on commit 80ec5ce

Please sign in to comment.