From 1f99576a379aa2015821c701b2c66c661ecea181 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 4 Jan 2016 18:33:54 -0600 Subject: [PATCH] Add test cases for result struct --- pkg/client/result_test.go | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 pkg/client/result_test.go diff --git a/pkg/client/result_test.go b/pkg/client/result_test.go new file mode 100644 index 0000000..7a58e67 --- /dev/null +++ b/pkg/client/result_test.go @@ -0,0 +1,46 @@ +package client + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_CSV(t *testing.T) { + result := Result{ + Columns: []string{"id", "name", "email"}, + Rows: []Row{ + Row{1, "John", "john@example.com"}, + Row{2, "Bob", "bob@example.com"}, + }, + } + + expected := "id,name,email\n1,John,john@example.com\n2,Bob,bob@example.com\n" + output := string(result.CSV()) + + assert.Equal(t, expected, output) +} + +func Test_JSON(t *testing.T) { + result := Result{ + Columns: []string{"id", "name", "email"}, + Rows: []Row{ + Row{1, "John", "john@example.com"}, + Row{2, "Bob", "bob@example.com"}, + }, + } + + output := result.JSON() + obj := []map[string]interface{}{} + err := json.Unmarshal(output, &obj) + + assert.NoError(t, err) + assert.Equal(t, 2, len(obj)) + + for i, row := range obj { + for j, col := range result.Columns { + assert.Equal(t, result.Rows[i][j], row[col]) + } + } +}