Skip to content

Commit

Permalink
add envvar to test with binary_mode and add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilling committed May 10, 2015
1 parent 18fa75e commit e5debbe
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ env:

script:
- go test -v ./...
- PGTEST_BINARY_MODE=on go test -v ./...

before_script:
- psql -c 'create database pqgotest' -U postgres
Expand Down
17 changes: 8 additions & 9 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co
}
}


func (cn *conn) simpleExec(q string) (res driver.Result, commandTag string, err error) {
b := cn.writeBuf('Q')
b.string(q)
Expand Down Expand Up @@ -748,8 +747,8 @@ func (cn *conn) Query(query string, args []driver.Value) (_ driver.Rows, err err

st.exec(args)
return &rows{
cn: cn,
cols: st.cols,
cn: cn,
cols: st.cols,
rowTyps: st.rowTyps,
}, nil
}
Expand Down Expand Up @@ -1306,8 +1305,8 @@ func (st *stmt) Query(v []driver.Value) (r driver.Rows, err error) {

st.exec(v)
return &rows{
cn: st.cn,
cols: st.cols,
cn: st.cn,
cols: st.cols,
rowTyps: st.rowTyps,
}, nil
}
Expand Down Expand Up @@ -1412,11 +1411,11 @@ func (cn *conn) parseComplete(commandTag string) (driver.Result, string) {
}

type rows struct {
cn *conn
cols []string
cn *conn
cols []string
rowTyps []oid.Oid
done bool
rb readBuf
done bool
rb readBuf
}

func (rs *rows) Close() error {
Expand Down
9 changes: 9 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"reflect"
"strings"
"testing"
"time"
)
Expand All @@ -15,6 +16,10 @@ type Fatalistic interface {
Fatal(args ...interface{})
}

func forceBinaryMode() bool {
return os.Getenv("PGTEST_BINARY_MODE") == "on"
}

func openTestConnConninfo(conninfo string) (*sql.DB, error) {
datname := os.Getenv("PGDATABASE")
sslmode := os.Getenv("PGSSLMODE")
Expand All @@ -32,6 +37,10 @@ func openTestConnConninfo(conninfo string) (*sql.DB, error) {
os.Setenv("PGCONNECT_TIMEOUT", "20")
}

if forceBinaryMode() && !strings.HasPrefix(conninfo, "postgres://") {
conninfo = conninfo + " binary_mode=on"
}

return sql.Open("postgres", conninfo)
}

Expand Down
67 changes: 65 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,13 @@ func TestByteaToText(t *testing.T) {
b := []byte("hello world")
row := db.QueryRow("SELECT $1::text", b)

var result []byte
var result string
err := row.Scan(&result)
if err != nil {
t.Fatal(err)
}

if string(result) != string(b) {
if result != string(b) {
t.Fatalf("expected %v but got %v", b, result)
}
}
Expand All @@ -436,6 +436,69 @@ func TestTextToBytea(t *testing.T) {
}
}

func TestByteaToUUID(t *testing.T) {
if forceBinaryMode() {
t.Skip("ByteaToUUID is intended to fail with binary_mode")
}
db := openTestConn(t)
defer db.Close()

b := []byte("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
row := db.QueryRow("SELECT $1::uuid", b)

var result []byte
err := row.Scan(&result)
if err != nil {
t.Fatal(err)
}

if string(result) != string(b) {
t.Fatalf("expected %v but got %v", b, result)
}
}

func TestStringToUUID(t *testing.T) {
db := openTestConn(t)
defer db.Close()

s := "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
row := db.QueryRow("SELECT $1::uuid", s)

var result string
err := row.Scan(&result)
if err != nil {
t.Fatal(err)
}

if result != s {
t.Fatalf("expected %v but got %v", s, result)
}
}

func TestByteaToInt(t *testing.T) {
db := openTestConn(t)
defer db.Close()

expected := 12345678
b := []byte(fmt.Sprintf("%d", expected))
row := db.QueryRow("SELECT $1::int", b)

var result int
err := row.Scan(&result)
if forceBinaryMode() {
if err == nil {
t.Fatalf("Expected error on query")
}
} else {
if err != nil {
t.Fatal(err)
}
if result != expected {
t.Fatalf("expected %v but got %v", expected, result)
}
}
}

func TestByteaOutputFormatEncoding(t *testing.T) {
input := []byte("\\x\x00\x01\x02\xFF\xFEabcdefg0123")
want := []byte("\\x5c78000102fffe6162636465666730313233")
Expand Down

0 comments on commit e5debbe

Please sign in to comment.