2
0

support decoding of []time.Time and []bool

This commit is contained in:
Karl Seguin
2014-12-21 14:35:38 +07:00
parent be663f648c
commit 109b55f9de
3 changed files with 169 additions and 19 deletions
+60
View File
@@ -3,6 +3,7 @@ package pgx_test
import (
"fmt"
"github.com/jackc/pgx"
"reflect"
"strings"
"testing"
"time"
@@ -159,6 +160,65 @@ func TestNullXMismatch(t *testing.T) {
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
tests := []struct {
sql string
query interface{}
scan interface{}
assert func(*testing.T, interface{}, interface{})
}{
{
"select $1::bool[]", []bool{true, false, true}, &[]bool{},
func(t *testing.T, query, scan interface{}) {
if reflect.DeepEqual(query, *(scan.(*[]bool))) == false {
t.Errorf("failed to encode bool[]")
}
},
},
{
"select $1::int[]", []int32{2, 4, 484}, &[]int32{},
func(t *testing.T, query, scan interface{}) {
if reflect.DeepEqual(query, *(scan.(*[]int32))) == false {
t.Errorf("failed to encode int[]")
}
},
},
{
"select $1::text[]", []string{"it's", "over", "9000!"}, &[]string{},
func(t *testing.T, query, scan interface{}) {
if reflect.DeepEqual(query, *(scan.(*[]string))) == false {
t.Errorf("failed to encode text[]")
}
},
},
{
"select $1::timestamp[]", []time.Time{time.Unix(323232, 0), time.Unix(3239949334, 00)}, &[]time.Time{},
func(t *testing.T, query, scan interface{}) {
if reflect.DeepEqual(query, *(scan.(*[]time.Time))) == false {
t.Errorf("failed to encode time.Time[]")
}
},
},
}
for i, tt := range tests {
psName := fmt.Sprintf("ps%d", i)
mustPrepare(t, conn, psName, tt.sql)
err := conn.QueryRow(psName, tt.query).Scan(tt.scan)
if err != nil {
t.Errorf(`error reading array: %v`, err)
}
tt.assert(t, tt.query, tt.scan)
ensureConnValid(t, conn)
}
}
func TestArrayDecoding(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
type allTypes struct {
s pgx.NullString
i16 pgx.NullInt16