influxdb/variable_test.go

143 lines
2.8 KiB
Go
Raw Normal View History

package influxdb_test
2018-09-11 23:13:15 +00:00
import (
"encoding/json"
"reflect"
"testing"
platform "github.com/influxdata/influxdb/v2"
platformtesting "github.com/influxdata/influxdb/v2/testing"
2018-09-11 23:13:15 +00:00
)
var (
variableTestID = "debac1e0deadbeef"
variableTestOrgID = "deadbeefdeadbeef"
)
func TestVariable_UnmarshalJSON(t *testing.T) {
2018-09-11 23:13:15 +00:00
tests := []struct {
name string
json string
want platform.Variable
2018-09-11 23:13:15 +00:00
}{
{
name: "with organization",
json: `
{
"id": "debac1e0deadbeef",
"orgID": "deadbeefdeadbeef",
"name": "howdy",
"selected": [],
"arguments": {
"type": "constant",
"values": ["a", "b", "c", "d"]
}
}
`,
want: platform.Variable{
ID: platformtesting.MustIDBase16(variableTestID),
OrganizationID: platformtesting.MustIDBase16(variableTestOrgID),
Name: "howdy",
Selected: make([]string, 0),
Arguments: &platform.VariableArguments{
Type: "constant",
Values: platform.VariableConstantValues{"a", "b", "c", "d"},
},
},
},
2018-09-11 23:13:15 +00:00
{
name: "with constant arguments",
json: `
{
"id": "debac1e0deadbeef",
2018-09-11 23:13:15 +00:00
"name": "howdy",
"selected": [],
"arguments": {
"type": "constant",
"values": ["a", "b", "c"]
}
}
`,
want: platform.Variable{
ID: platformtesting.MustIDBase16(variableTestID),
2018-09-11 23:13:15 +00:00
Name: "howdy",
Selected: make([]string, 0),
Arguments: &platform.VariableArguments{
2018-09-11 23:13:15 +00:00
Type: "constant",
Values: platform.VariableConstantValues{"a", "b", "c"},
2018-09-11 23:13:15 +00:00
},
},
},
{
name: "with map arguments",
json: `
{
"id": "debac1e0deadbeef",
2018-09-11 23:13:15 +00:00
"name": "howdy",
"selected": [],
"arguments": {
"type": "map",
"values": {
"a": "A",
"b": "B"
}
}
}
`,
want: platform.Variable{
ID: platformtesting.MustIDBase16(variableTestID),
2018-09-11 23:13:15 +00:00
Name: "howdy",
Selected: make([]string, 0),
Arguments: &platform.VariableArguments{
2018-09-11 23:13:15 +00:00
Type: "map",
Values: platform.VariableMapValues{"a": "A", "b": "B"},
2018-09-11 23:13:15 +00:00
},
},
},
{
name: "with query arguments",
json: `
{
"id": "debac1e0deadbeef",
2018-09-11 23:13:15 +00:00
"name": "howdy",
"selected": [],
"arguments": {
"type": "query",
"values": {
"query": "howdy",
"language": "flux"
}
}
}
`,
want: platform.Variable{
ID: platformtesting.MustIDBase16(variableTestID),
2018-09-11 23:13:15 +00:00
Name: "howdy",
Selected: make([]string, 0),
Arguments: &platform.VariableArguments{
2018-09-11 23:13:15 +00:00
Type: "query",
Values: platform.VariableQueryValues{
2018-09-11 23:13:15 +00:00
Query: "howdy",
Language: "flux",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var m platform.Variable
2018-09-11 23:13:15 +00:00
err := json.Unmarshal([]byte(tt.json), &m)
if err != nil {
t.Fatalf("error unmarshalling json: %v", err)
}
if !reflect.DeepEqual(m, tt.want) {
t.Errorf("%q. got = %+v, want %+v", tt.name, m, tt.want)
}
})
}
}