Test BuildResponse
This commit is contained in:
@@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
type connectionTestResponse struct {
|
type connectionTestResponse struct {
|
||||||
ZabbixVersion string `json:"zabbixVersion"`
|
ZabbixVersion string `json:"zabbixVersion"`
|
||||||
DbConnectorStatus dbConnectionStatus `json:"dbConnectorStatus"`
|
DbConnectorStatus *dbConnectionStatus `json:"dbConnectorStatus"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type dbConnectionStatus struct {
|
type dbConnectionStatus struct {
|
||||||
dsType string
|
DsType string `json:"dsType"`
|
||||||
dsName string
|
DsName string `json:"dsName"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,8 +140,8 @@ func (ds *ZabbixDatasource) TestConnection(ctx context.Context, tsdbReq *datasou
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BuildResponse transforms a Zabbix API response to a DatasourceResponse
|
// BuildResponse transforms a Zabbix API response to a DatasourceResponse
|
||||||
func (ds *ZabbixDatasource) BuildResponse(result interface{}) (*datasource.DatasourceResponse, error) {
|
func (ds *ZabbixDatasource) BuildResponse(responseData interface{}) (*datasource.DatasourceResponse, error) {
|
||||||
jsonBytes, err := json.Marshal(result)
|
jsonBytes, err := json.Marshal(responseData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
71
pkg/zabbix_api_test.go
Normal file
71
pkg/zabbix_api_test.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
simplejson "github.com/bitly/go-simplejson"
|
||||||
|
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||||
|
"gotest.tools/assert"
|
||||||
|
"gotest.tools/assert/cmp"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestZabbixDatasource_BuildResponse(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) {
|
||||||
|
ds := NewZabbixDatasource()
|
||||||
|
got, err := ds.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