2
0

Add more testing of Encode*

Handle case where TextEncoder is used to a core type that the driver
could otherwise have handled as binary.
This commit is contained in:
Jack Christensen
2014-07-11 11:16:12 -05:00
parent 6884fdfb52
commit 24395d98df
4 changed files with 177 additions and 92 deletions
+44
View File
@@ -1,6 +1,7 @@
package pgx_test
import (
"fmt"
"github.com/jackc/pgx"
"strings"
"testing"
@@ -185,3 +186,46 @@ func TestTimestampTzTranscode(t *testing.T) {
t.Errorf("Did not transcode time successfully: %v is not %v", outputTime, inputTime)
}
}
func TestNullX(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
type allTypes struct {
i64 pgx.NullInt64
}
var actual, zero allTypes
tests := []struct {
sql string
queryArgs []interface{}
scanArgs []interface{}
expected allTypes
}{
{"select $1::int8", []interface{}{&pgx.NullInt64{Int64: 1, Valid: true}}, []interface{}{&actual.i64}, allTypes{i64: pgx.NullInt64{Int64: 1, Valid: true}}},
{"select $1::int8", []interface{}{&pgx.NullInt64{Int64: 1, Valid: false}}, []interface{}{&actual.i64}, allTypes{i64: pgx.NullInt64{Int64: 0, Valid: false}}},
}
for i, tt := range tests {
psName := fmt.Sprintf("success%d", i)
mustPrepare(t, conn, psName, tt.sql)
for _, sql := range []string{tt.sql, psName} {
actual = zero
err := conn.QueryRow(sql, tt.queryArgs...).Scan(tt.scanArgs...)
if err != nil {
t.Errorf("%d. Unexpected failure: %v (sql -> %v, queryArgs -> %v)", i, err, sql, tt.queryArgs)
}
if actual != tt.expected {
t.Errorf("%d. Expected %v, got %v (sql -> %v, queryArgs -> %v)", i, tt.expected, actual, sql, tt.queryArgs)
}
ensureConnValid(t, conn)
}
}
}