Improve setting exporter tests

This commit is contained in:
Tomaž Jerman
2019-10-16 16:30:48 +02:00
parent 0b3a19dd0f
commit 346f9bd4df
+47 -45
View File
@@ -4,61 +4,63 @@ import (
"testing"
"github.com/jmoiron/sqlx/types"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestExport(t *testing.T) {
ss := ValueSet{}
ss = append(ss, ([]*Value{
&Value{
Name: "v_string",
Value: types.JSONText("\"string\""),
req := require.New(t)
tests := []struct {
name, rawValue string
parsedValue interface{}
}{
{
name: "v_string",
rawValue: `"string"`,
parsedValue: yaml.MapItem{Key: "v_string", Value: "string"},
},
&Value{
Name: "v_float",
Value: types.JSONText("123"),
{
name: "v_int-as-float",
rawValue: "12.34",
parsedValue: yaml.MapItem{Key: "v_int-as-float", Value: 12.34},
},
&Value{
Name: "v_float",
Value: types.JSONText("12.34"),
{
name: "v_float",
rawValue: "123",
parsedValue: yaml.MapItem{Key: "v_float", Value: 123.0},
},
&Value{
Name: "v_bool",
Value: types.JSONText("true"),
{
name: "v_bool_true",
rawValue: "true",
parsedValue: yaml.MapItem{Key: "v_bool_true", Value: true},
},
&Value{
Name: "v_slice",
Value: types.JSONText("[1, \"string\", true]"),
{
name: "v_bool_false",
rawValue: "false",
parsedValue: yaml.MapItem{Key: "v_bool_false", Value: false},
},
&Value{
Name: "v_map",
Value: types.JSONText("{\"k1\": \"v1\",\"k2\": 2}"),
{
name: "v_slice",
rawValue: `[1, 1.23, "string", true, false]`,
parsedValue: yaml.MapItem{Key: "v_slice", Value: []interface{}{1.0, 1.23, "string", true, false}},
},
{
name: "v_map",
rawValue: `{"k1": "string","k2": 1,"k3":1.23,"k4":true,"k5":false}`,
parsedValue: yaml.MapItem{Key: "v_map", Value: map[string]interface{}{"k1": "string", "k2": 1.0, "k3": 1.23, "k4": true, "k5": false}},
},
})...)
tt := Export(ss)
if _, ok := tt[0].Value.(string); !ok {
t.Errorf("Expecting %v to be string", tt[0].Value)
}
if _, ok := tt[1].Value.(float64); !ok {
t.Errorf("Expecting %v to be float64", tt[1].Value)
}
if _, ok := tt[2].Value.(float64); !ok {
t.Errorf("Expecting %v to be float64", tt[2].Value)
}
if _, ok := tt[3].Value.(bool); !ok {
t.Errorf("Expecting %v to be bool", tt[3].Value)
}
if _, ok := tt[4].Value.([]interface{}); !ok {
t.Errorf("Expecting %v to be []interface{}", tt[4].Value)
}
if _, ok := tt[5].Value.(map[string]interface{}); !ok {
t.Errorf("Expecting %v to be map[string]interface {}", tt[5].Value)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tt := Export(ValueSet{
&Value{
Name: test.name,
Value: types.JSONText(test.rawValue),
},
})
req.Len(tt, 1)
req.Equal(test.parsedValue, tt[0])
})
}
}