pgweb/pkg/client/result_test.go

93 lines
2.0 KiB
Go
Raw Normal View History

2016-01-04 18:33:54 -06:00
package client
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPostProcess(t *testing.T) {
t.Run("large numbers", func(t *testing.T) {
result := Result{
Columns: []string{"value"},
Rows: []Row{
{int(1234)},
{int64(9223372036854775807)},
{int64(-9223372036854775808)},
{float64(9223372036854775808.9223372036854775808)},
{float64(999999999999999.9)},
},
}
result.PostProcess()
assert.Equal(t, 1234, result.Rows[0][0])
assert.Equal(t, "9223372036854775807", result.Rows[1][0])
assert.Equal(t, "-9223372036854775808", result.Rows[2][0])
assert.Equal(t, "9.223372036854776e+18", result.Rows[3][0])
assert.Equal(t, "9.999999999999999e+14", result.Rows[4][0])
})
t.Run("binary encoding", func(t *testing.T) {
result := Result{
Columns: []string{"data"},
Rows: []Row{
{"text value"},
{"text with symbols !@#$%"},
{string([]byte{10, 11, 12, 13})},
},
}
2016-01-07 11:27:16 -06:00
result.PostProcess()
2016-01-07 11:27:16 -06:00
assert.Equal(t, "text value", result.Rows[0][0])
assert.Equal(t, "text with symbols !@#$%", result.Rows[1][0])
assert.Equal(t, "CgsMDQ==", result.Rows[2][0])
})
2016-01-07 11:27:16 -06:00
}
func TestCSV(t *testing.T) {
2016-01-04 18:33:54 -06:00
result := Result{
Columns: []string{"id", "name", "email"},
Rows: []Row{
{1, "John", "john@example.com"},
{2, "Bob", "bob@example.com"},
2016-01-04 18:33:54 -06:00
},
}
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 TestJSON(t *testing.T) {
2016-01-04 18:33:54 -06:00
result := Result{
Columns: []string{"id", "name", "email"},
Rows: []Row{
{1, "John", "john@example.com"},
{2, "Bob", "bob@example.com"},
2016-01-04 18:33:54 -06:00
},
}
2016-01-14 16:55:05 -06:00
obj := []struct {
Id int
Name string
Email string
}{}
2016-01-04 18:33:54 -06:00
2016-01-14 16:55:05 -06:00
expected := []struct {
Id int
Name string
Email string
}{
{1, "John", "john@example.com"},
{2, "Bob", "bob@example.com"},
2016-01-04 18:33:54 -06:00
}
2016-01-14 16:55:05 -06:00
assert.NoError(t, json.Unmarshal(result.JSON(), &obj))
assert.Equal(t, 2, len(obj))
assert.Equal(t, expected, obj)
2016-01-04 18:33:54 -06:00
}