Finish
This commit is contained in:
@@ -3,10 +3,12 @@ package main
|
||||
import (
|
||||
"testing"
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
"gotest.tools/assert"
|
||||
"gotest.tools/assert/cmp"
|
||||
)
|
||||
|
||||
func TestZabbixBackend_getCachedDatasource(t *testing.T) {
|
||||
@@ -79,3 +81,63 @@ func TestZabbixBackend_getCachedDatasource(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildResponse(t *testing.T) {
|
||||
jsonData := simplejson.New()
|
||||
jsonData.Set("testing", []int{5, 12, 75})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
responseData interface{}
|
||||
want *datasource.DatasourceResponse
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "simplejson Response",
|
||||
responseData: jsonData,
|
||||
want: &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: `{"testing":[5,12,75]}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Connetion Status Response",
|
||||
responseData: connectionTestResponse{
|
||||
ZabbixVersion: "2.4",
|
||||
DbConnectorStatus: &dbConnectionStatus{
|
||||
DsType: "mysql",
|
||||
DsName: "MyDatabase",
|
||||
},
|
||||
},
|
||||
want: &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: `{"zabbixVersion":"2.4","dbConnectorStatus":{"dsType":"mysql","dsName":"MyDatabase"}}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Unmarshalable",
|
||||
responseData: 2i,
|
||||
wantErr: "json: unsupported type: complex128",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := BuildResponse(tt.responseData)
|
||||
if tt.wantErr != "" {
|
||||
assert.Error(t, err, tt.wantErr)
|
||||
assert.Assert(t, cmp.Nil(got))
|
||||
return
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, got, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user