From ca145c9acbaf6746c6774c522f269056c7cf53dc Mon Sep 17 00:00:00 2001 From: Alec Sears Date: Thu, 17 Oct 2019 17:12:10 -0500 Subject: [PATCH] Test BuildResponse --- pkg/models.go | 8 ++--- pkg/zabbix_api.go | 4 +-- pkg/zabbix_api_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 pkg/zabbix_api_test.go diff --git a/pkg/models.go b/pkg/models.go index f2a01d3..b252f00 100644 --- a/pkg/models.go +++ b/pkg/models.go @@ -1,11 +1,11 @@ package main type connectionTestResponse struct { - ZabbixVersion string `json:"zabbixVersion"` - DbConnectorStatus dbConnectionStatus `json:"dbConnectorStatus"` + ZabbixVersion string `json:"zabbixVersion"` + DbConnectorStatus *dbConnectionStatus `json:"dbConnectorStatus"` } type dbConnectionStatus struct { - dsType string - dsName string + DsType string `json:"dsType"` + DsName string `json:"dsName"` } diff --git a/pkg/zabbix_api.go b/pkg/zabbix_api.go index 4734d4a..311d9ce 100644 --- a/pkg/zabbix_api.go +++ b/pkg/zabbix_api.go @@ -140,8 +140,8 @@ func (ds *ZabbixDatasource) TestConnection(ctx context.Context, tsdbReq *datasou } // BuildResponse transforms a Zabbix API response to a DatasourceResponse -func (ds *ZabbixDatasource) BuildResponse(result interface{}) (*datasource.DatasourceResponse, error) { - jsonBytes, err := json.Marshal(result) +func (ds *ZabbixDatasource) BuildResponse(responseData interface{}) (*datasource.DatasourceResponse, error) { + jsonBytes, err := json.Marshal(responseData) if err != nil { return nil, err } diff --git a/pkg/zabbix_api_test.go b/pkg/zabbix_api_test.go new file mode 100644 index 0000000..8f82576 --- /dev/null +++ b/pkg/zabbix_api_test.go @@ -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) + }) + } +}