2016-07-30 10:20:43 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-08-09 13:50:18 +00:00
|
|
|
"strings"
|
2016-07-30 10:20:43 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testData struct {
|
|
|
|
Region string
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectJSON = `{
|
|
|
|
"ID": "1",
|
2017-02-16 00:28:28 +00:00
|
|
|
"Name": "example",
|
|
|
|
"Region": "global"
|
2016-07-30 10:20:43 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
var (
|
|
|
|
tData = testData{"global", "1", "example"}
|
|
|
|
testFormat = map[string]string{"json": "", "template": "{{.Region}}"}
|
|
|
|
expectOutput = map[string]string{"json": expectJSON, "template": "global"}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDataFormat(t *testing.T) {
|
|
|
|
for k, v := range testFormat {
|
|
|
|
fm, err := DataFormat(k, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := fm.TransformData(tData)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != expectOutput[k] {
|
2017-02-16 00:28:28 +00:00
|
|
|
t.Fatalf("expected output:\n%s\nactual:\n%s", expectOutput[k], result)
|
2016-07-30 10:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 13:50:18 +00:00
|
|
|
|
|
|
|
func TestInvalidJSONTemplate(t *testing.T) {
|
|
|
|
// Invalid template {{.foo}}
|
|
|
|
fm, err := DataFormat("template", "{{.foo}}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
_, err = fm.TransformData(tData)
|
2016-08-17 00:53:49 +00:00
|
|
|
if !strings.Contains(err.Error(), "can't evaluate field foo") {
|
2016-08-09 13:50:18 +00:00
|
|
|
t.Fatalf("expected invalid template error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// No template is specified
|
|
|
|
fm, err = DataFormat("template", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
_, err = fm.TransformData(tData)
|
|
|
|
if !strings.Contains(err.Error(), "template needs to be specified the golang templates.") {
|
|
|
|
t.Fatalf("expected not specified template error, got: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|