From 346f9bd4df105416eb6c6b278fd55d289c5b2673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 16 Oct 2019 13:18:40 +0200 Subject: [PATCH] Improve setting exporter tests --- pkg/settings/exporter_test.go | 92 ++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/pkg/settings/exporter_test.go b/pkg/settings/exporter_test.go index 2a9c1da29..6955564a5 100644 --- a/pkg/settings/exporter_test.go +++ b/pkg/settings/exporter_test.go @@ -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]) + }) } }