Use pgx_test package for tests

pgx-vs-pq
Jack Christensen 2013-07-16 08:29:59 -05:00
parent 8df9964ce8
commit bae49077ce
9 changed files with 140 additions and 128 deletions

View File

@ -1,7 +1,8 @@
package pgx
package pgx_test
import (
"fmt"
"github.com/JackC/pgx"
"math/rand"
"testing"
)
@ -15,7 +16,7 @@ var float4TextVsBinaryTestDataLoaded bool
var float8TextVsBinaryTestDataLoaded bool
var boolTextVsBinaryTestDataLoaded bool
func createNarrowTestData(b *testing.B, conn *Connection) {
func createNarrowTestData(b *testing.B, conn *pgx.Connection) {
if narrowTestDataLoaded {
return
}
@ -46,23 +47,23 @@ func createNarrowTestData(b *testing.B, conn *Connection) {
narrowTestDataLoaded = true
}
func removeBinaryEncoders() (encoders map[Oid]func(*MessageReader, int32) interface{}) {
encoders = make(map[Oid]func(*MessageReader, int32) interface{})
for k, v := range ValueTranscoders {
func removeBinaryEncoders() (encoders map[pgx.Oid]func(*pgx.MessageReader, int32) interface{}) {
encoders = make(map[pgx.Oid]func(*pgx.MessageReader, int32) interface{})
for k, v := range pgx.ValueTranscoders {
encoders[k] = v.DecodeBinary
ValueTranscoders[k].DecodeBinary = nil
pgx.ValueTranscoders[k].DecodeBinary = nil
}
return
}
func restoreBinaryEncoders(encoders map[Oid]func(*MessageReader, int32) interface{}) {
func restoreBinaryEncoders(encoders map[pgx.Oid]func(*pgx.MessageReader, int32) interface{}) {
for k, v := range encoders {
ValueTranscoders[k].DecodeBinary = v
pgx.ValueTranscoders[k].DecodeBinary = v
}
}
func BenchmarkSelectRowSimpleNarrow(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createNarrowTestData(b, conn)
// Get random ids outside of timing
@ -78,7 +79,7 @@ func BenchmarkSelectRowSimpleNarrow(b *testing.B) {
}
func BenchmarkSelectRowPreparedNarrow(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createNarrowTestData(b, conn)
// Get random ids outside of timing
@ -94,7 +95,7 @@ func BenchmarkSelectRowPreparedNarrow(b *testing.B) {
}
func BenchmarkSelectRowsSimpleNarrow(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createNarrowTestData(b, conn)
// Get random ids outside of timing
@ -110,7 +111,7 @@ func BenchmarkSelectRowsSimpleNarrow(b *testing.B) {
}
func BenchmarkSelectRowsPreparedNarrow(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createNarrowTestData(b, conn)
// Get random ids outside of timing
@ -125,7 +126,7 @@ func BenchmarkSelectRowsPreparedNarrow(b *testing.B) {
}
}
func createJoinsTestData(b *testing.B, conn *Connection) {
func createJoinsTestData(b *testing.B, conn *pgx.Connection) {
if testJoinsDataLoaded {
return
}
@ -197,7 +198,7 @@ func createJoinsTestData(b *testing.B, conn *Connection) {
}
func BenchmarkSelectRowsSimpleJoins(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createJoinsTestData(b, conn)
sql := `
@ -217,7 +218,7 @@ func BenchmarkSelectRowsSimpleJoins(b *testing.B) {
}
func BenchmarkSelectRowsPreparedJoins(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createJoinsTestData(b, conn)
b.ResetTimer()
@ -226,7 +227,7 @@ func BenchmarkSelectRowsPreparedJoins(b *testing.B) {
}
}
func createInt2TextVsBinaryTestData(b *testing.B, conn *Connection) {
func createInt2TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if int2TextVsBinaryTestDataLoaded {
return
}
@ -252,7 +253,7 @@ func createInt2TextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkInt2Text(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt2TextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -268,7 +269,7 @@ func BenchmarkInt2Text(b *testing.B) {
}
func BenchmarkInt2Binary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt2TextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectInt16", "select * from t")
defer func() { conn.Deallocate("selectInt16") }()
@ -279,7 +280,7 @@ func BenchmarkInt2Binary(b *testing.B) {
}
}
func createInt4TextVsBinaryTestData(b *testing.B, conn *Connection) {
func createInt4TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if int4TextVsBinaryTestDataLoaded {
return
}
@ -305,7 +306,7 @@ func createInt4TextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkInt4Text(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt4TextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -321,7 +322,7 @@ func BenchmarkInt4Text(b *testing.B) {
}
func BenchmarkInt4Binary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt4TextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectInt32", "select * from t")
defer func() { conn.Deallocate("selectInt32") }()
@ -332,7 +333,7 @@ func BenchmarkInt4Binary(b *testing.B) {
}
}
func createInt8TextVsBinaryTestData(b *testing.B, conn *Connection) {
func createInt8TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if int8TextVsBinaryTestDataLoaded {
return
}
@ -358,7 +359,7 @@ func createInt8TextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkInt8Text(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt8TextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -374,7 +375,7 @@ func BenchmarkInt8Text(b *testing.B) {
}
func BenchmarkInt8Binary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createInt8TextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectInt64", "select * from t")
defer func() { conn.Deallocate("selectInt64") }()
@ -385,7 +386,7 @@ func BenchmarkInt8Binary(b *testing.B) {
}
}
func createFloat4TextVsBinaryTestData(b *testing.B, conn *Connection) {
func createFloat4TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if float4TextVsBinaryTestDataLoaded {
return
}
@ -411,7 +412,7 @@ func createFloat4TextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkFloat4Text(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createFloat4TextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -427,7 +428,7 @@ func BenchmarkFloat4Text(b *testing.B) {
}
func BenchmarkFloat4Binary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createFloat4TextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectFloat32", "select * from t")
defer func() { conn.Deallocate("selectFloat32") }()
@ -438,7 +439,7 @@ func BenchmarkFloat4Binary(b *testing.B) {
}
}
func createFloat8TextVsBinaryTestData(b *testing.B, conn *Connection) {
func createFloat8TextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if float8TextVsBinaryTestDataLoaded {
return
}
@ -464,7 +465,7 @@ func createFloat8TextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkFloat8Text(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createFloat8TextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -480,7 +481,7 @@ func BenchmarkFloat8Text(b *testing.B) {
}
func BenchmarkFloat8Binary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createFloat8TextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectFloat32", "select * from t")
defer func() { conn.Deallocate("selectFloat32") }()
@ -491,7 +492,7 @@ func BenchmarkFloat8Binary(b *testing.B) {
}
}
func createBoolTextVsBinaryTestData(b *testing.B, conn *Connection) {
func createBoolTextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
if boolTextVsBinaryTestDataLoaded {
return
}
@ -517,7 +518,7 @@ func createBoolTextVsBinaryTestData(b *testing.B, conn *Connection) {
}
func BenchmarkBoolText(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createBoolTextVsBinaryTestData(b, conn)
encoders := removeBinaryEncoders()
@ -533,7 +534,7 @@ func BenchmarkBoolText(b *testing.B) {
}
func BenchmarkBoolBinary(b *testing.B) {
conn := getSharedConnection()
conn := GetSharedConnection()
createBoolTextVsBinaryTestData(b, conn)
mustPrepare(b, conn, "selectBool", "select * from t")
defer func() { conn.Deallocate("selectBool") }()

View File

@ -30,11 +30,11 @@ type ConnectionParameters struct {
type Connection struct {
conn net.Conn // the underlying TCP or unix domain socket connection
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
pid int32 // backend pid
secretKey int32 // key to use to send a cancel query message to the server
runtimeParams map[string]string // parameters that have been reported by the server
Pid int32 // backend pid
SecretKey int32 // key to use to send a cancel query message to the server
RuntimeParams map[string]string // parameters that have been reported by the server
parameters ConnectionParameters // parameters used when establishing this connection
txStatus byte
TxStatus byte
preparedStatements map[string]*preparedStatement
}
@ -92,7 +92,7 @@ func Connect(parameters ConnectionParameters) (c *Connection, err error) {
}
c.buf = bytes.NewBuffer(make([]byte, 0, sharedBufferSize))
c.runtimeParams = make(map[string]string)
c.RuntimeParams = make(map[string]string)
c.preparedStatements = make(map[string]*preparedStatement)
msg := newStartupMessage()
@ -498,7 +498,7 @@ func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool
return
}
defer func() {
if committed && c.txStatus == 'T' {
if committed && c.TxStatus == 'T' {
_, err = c.Execute("commit")
if err != nil {
committed = false
@ -593,7 +593,7 @@ func hexMD5(s string) string {
func (c *Connection) rxParameterStatus(r *MessageReader) {
key := r.ReadString()
value := r.ReadString()
c.runtimeParams[key] = value
c.RuntimeParams[key] = value
}
func (c *Connection) rxErrorResponse(r *MessageReader) (err PgError) {
@ -614,12 +614,12 @@ func (c *Connection) rxErrorResponse(r *MessageReader) (err PgError) {
}
func (c *Connection) rxBackendKeyData(r *MessageReader) {
c.pid = r.ReadInt32()
c.secretKey = r.ReadInt32()
c.Pid = r.ReadInt32()
c.SecretKey = r.ReadInt32()
}
func (c *Connection) rxReadyForQuery(r *MessageReader) {
c.txStatus = r.ReadByte()
c.TxStatus = r.ReadByte()
}
func (c *Connection) rxRowDescription(r *MessageReader) (fields []FieldDescription) {

View File

@ -36,7 +36,7 @@ func (p *ConnectionPool) Acquire() (c *Connection) {
// Release gives up use of a connection.
func (p *ConnectionPool) Release(c *Connection) {
if c.txStatus != 'I' {
if c.TxStatus != 'I' {
c.Execute("rollback")
}
p.connectionChannel <- c

View File

@ -1,12 +1,13 @@
package pgx
package pgx_test
import (
"fmt"
"github.com/JackC/pgx"
"testing"
)
func createConnectionPool(maxConnections int) *ConnectionPool {
pool, err := NewConnectionPool(*defaultConnectionParameters, maxConnections)
func createConnectionPool(maxConnections int) *pgx.ConnectionPool {
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, maxConnections)
if err != nil {
panic("Unable to create connection pool")
}
@ -14,7 +15,7 @@ func createConnectionPool(maxConnections int) *ConnectionPool {
}
func TestNewConnectionPool(t *testing.T) {
pool, err := NewConnectionPool(*defaultConnectionParameters, 5)
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, 5)
if err != nil {
t.Fatal("Unable to establish connection pool")
}
@ -32,8 +33,8 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
pool := createConnectionPool(maxConnections)
defer pool.Close()
acquireAll := func() (connections []*Connection) {
connections = make([]*Connection, maxConnections)
acquireAll := func() (connections []*pgx.Connection) {
connections = make([]*pgx.Connection, maxConnections)
for i := 0; i < maxConnections; i++ {
connections[i] = pool.Acquire()
}
@ -107,25 +108,25 @@ func TestPoolReleaseWithTransactions(t *testing.T) {
if _, err = conn.Execute("select"); err == nil {
t.Fatal("Did not receive expected error")
}
if conn.txStatus != 'E' {
t.Fatalf("Expected txStatus to be 'E', instead it was '%c'", conn.txStatus)
if conn.TxStatus != 'E' {
t.Fatalf("Expected TxStatus to be 'E', instead it was '%c'", conn.TxStatus)
}
pool.Release(conn)
if conn.txStatus != 'I' {
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.txStatus)
if conn.TxStatus != 'I' {
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.TxStatus)
}
conn = pool.Acquire()
mustExecute(t, conn, "begin")
if conn.txStatus != 'T' {
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.txStatus)
if conn.TxStatus != 'T' {
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.TxStatus)
}
pool.Release(conn)
if conn.txStatus != 'I' {
t.Fatalf("Expected release to rollback uncommitted transaction, but it did not: '%c'", conn.txStatus)
if conn.TxStatus != 'I' {
t.Fatalf("Expected release to rollback uncommitted transaction, but it did not: '%c'", conn.TxStatus)
}
}

View File

@ -1,18 +1,22 @@
package pgx
package pgx_test
var defaultConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
import (
"github.com/JackC/pgx"
)
var defaultpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// To skip tests for specific connection / authentication types set that connection param to nil
var tcpConnectionParameters *ConnectionParameters = nil
var unixSocketConnectionParameters *ConnectionParameters = nil
var md5ConnectionParameters *ConnectionParameters = nil
var plainPasswordConnectionParameters *ConnectionParameters = nil
var noPasswordConnectionParameters *ConnectionParameters = nil
var invalidUserConnectionParameters *ConnectionParameters = nil
var tcppgx.ConnectionParameters *pgx.ConnectionParameters = nil
var unixSocketpgx.ConnectionParameters *pgx.ConnectionParameters = nil
var md5pgx.ConnectionParameters *pgx.ConnectionParameters = nil
var plainPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = nil
var noPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = nil
var invalidUserpgx.ConnectionParameters *pgx.ConnectionParameters = nil
// var tcpConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var unixSocketConnectionParameters *ConnectionParameters = &ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
// var md5ConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var plainPasswordConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "pgx_pw", Password: "secret", Database: "pgx_test"}
// var noPasswordConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "pgx_none", Database: "pgx_test"}
// var invalidUserConnectionParameters *ConnectionParameters = &ConnectionParameters{Host: "127.0.0.1", User: "invalid", Database: "pgx_test"}
// var tcppgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var unixSocketpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
// var md5pgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"}
// var plainPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_pw", Password: "secret", Database: "pgx_test"}
// var noPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "pgx_none", Database: "pgx_test"}
// var invalidUserpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "invalid", Database: "pgx_test"}

View File

@ -1,40 +1,27 @@
package pgx
package pgx_test
import (
"bytes"
"github.com/JackC/pgx"
"strings"
"testing"
)
var SharedConnection *Connection
func getSharedConnection() (c *Connection) {
if SharedConnection == nil {
var err error
SharedConnection, err = Connect(*defaultConnectionParameters)
if err != nil {
panic("Unable to establish connection")
}
}
return SharedConnection
}
func TestConnect(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
if _, present := conn.runtimeParams["server_version"]; !present {
if _, present := conn.RuntimeParams["server_version"]; !present {
t.Error("Runtime parameters not stored")
}
if conn.pid == 0 {
if conn.Pid == 0 {
t.Error("Backend PID not stored")
}
if conn.secretKey == 0 {
if conn.SecretKey == 0 {
t.Error("Backend secret key not stored")
}
@ -59,7 +46,7 @@ func TestConnectWithUnixSocket(t *testing.T) {
return
}
conn, err := Connect(*unixSocketConnectionParameters)
conn, err := pgx.Connect(*unixSocketConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -75,7 +62,7 @@ func TestConnectWithTcp(t *testing.T) {
return
}
conn, err := Connect(*tcpConnectionParameters)
conn, err := pgx.Connect(*tcpConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -91,8 +78,8 @@ func TestConnectWithInvalidUser(t *testing.T) {
return
}
_, err := Connect(*invalidUserConnectionParameters)
pgErr, ok := err.(PgError)
_, err := pgx.Connect(*invalidUserConnectionParameters)
pgErr, ok := err.(pgx.PgError)
if !ok {
t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err)
}
@ -106,7 +93,7 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
return
}
conn, err := Connect(*plainPasswordConnectionParameters)
conn, err := pgx.Connect(*plainPasswordConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -122,7 +109,7 @@ func TestConnectWithMD5Password(t *testing.T) {
return
}
conn, err := Connect(*md5ConnectionParameters)
conn, err := pgx.Connect(*md5ConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -134,7 +121,7 @@ func TestConnectWithMD5Password(t *testing.T) {
}
func TestExecute(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
if results := mustExecute(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" {
t.Error("Unexpected results from Execute")
@ -161,7 +148,7 @@ func TestExecute(t *testing.T) {
}
func TestExecuteFailure(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -177,10 +164,10 @@ func TestExecuteFailure(t *testing.T) {
}
func TestSelectFunc(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
var sum, rowCount int32
onDataRow := func(r *DataRowReader) error {
onDataRow := func(r *pgx.DataRowReader) error {
rowCount++
sum += r.ReadValue().(int32)
return nil
@ -199,7 +186,7 @@ func TestSelectFunc(t *testing.T) {
}
func TestSelectFuncFailure(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -216,7 +203,7 @@ func TestSelectFuncFailure(t *testing.T) {
}
func TestSelectRows(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
rows := mustSelectRows(t, conn, "select $1 as name, null as position", "Jack")
@ -238,7 +225,7 @@ func TestSelectRows(t *testing.T) {
}
func TestSelectRow(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
row := mustSelectRow(t, conn, "select $1 as name, null as position", "Jack")
if row["name"] != "Jack" {
@ -254,18 +241,18 @@ func TestSelectRow(t *testing.T) {
}
_, err := conn.SelectRow("select 'Jack' as name where 1=2")
if _, ok := err.(NotSingleRowError); !ok {
if _, ok := err.(pgx.NotSingleRowError); !ok {
t.Error("No matching row should have returned NotSingleRowError")
}
_, err = conn.SelectRow("select * from (values ('Matthew'), ('Mark')) t")
if _, ok := err.(NotSingleRowError); !ok {
if _, ok := err.(pgx.NotSingleRowError); !ok {
t.Error("Multiple matching rows should have returned NotSingleRowError")
}
}
func TestConnectionSelectValue(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
test := func(sql string, expected interface{}, arguments ...interface{}) {
v, err := conn.SelectValue(sql, arguments...)
@ -289,23 +276,23 @@ func TestConnectionSelectValue(t *testing.T) {
test("select 1.23::float8", float64(1.23))
_, err := conn.SelectValue("select 'Jack' as name where 1=2")
if _, ok := err.(NotSingleRowError); !ok {
if _, ok := err.(pgx.NotSingleRowError); !ok {
t.Error("No matching row should have returned NoRowsFoundError")
}
_, err = conn.SelectValue("select * from (values ('Matthew'), ('Mark')) t")
if _, ok := err.(NotSingleRowError); !ok {
if _, ok := err.(pgx.NotSingleRowError); !ok {
t.Error("Multiple matching rows should have returned NotSingleRowError")
}
_, err = conn.SelectValue("select 'Matthew', 'Mark'")
if _, ok := err.(UnexpectedColumnCountError); !ok {
if _, ok := err.(pgx.UnexpectedColumnCountError); !ok {
t.Error("Multiple columns should have returned UnexpectedColumnCountError")
}
}
func TestSelectValues(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
test := func(sql string, expected []interface{}, arguments ...interface{}) {
values, err := conn.SelectValues(sql, arguments...)
@ -331,13 +318,13 @@ func TestSelectValues(t *testing.T) {
test("select * from (values (1::int4), (2::int4), (null), (3::int4)) t", []interface{}{int32(1), int32(2), nil, int32(3)})
_, err := conn.SelectValues("select 'Matthew', 'Mark'")
if _, ok := err.(UnexpectedColumnCountError); !ok {
if _, ok := err.(pgx.UnexpectedColumnCountError); !ok {
t.Error("Multiple columns should have returned UnexpectedColumnCountError")
}
}
func TestPrepare(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -411,7 +398,7 @@ func TestPrepare(t *testing.T) {
}
func TestPrepareFailure(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -427,7 +414,7 @@ func TestPrepareFailure(t *testing.T) {
}
func TestTransaction(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
@ -538,7 +525,7 @@ func TestTransaction(t *testing.T) {
}
func TestTransactionIso(t *testing.T) {
conn, err := Connect(*defaultConnectionParameters)
conn, err := pgx.Connect(*defaultConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}

View File

@ -1,16 +1,17 @@
package pgx
package pgx_test
import (
"github.com/JackC/pgx"
"testing"
)
func TestDataRowReaderReadValue(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
test := func(sql string, expected interface{}) {
var v interface{}
onDataRow := func(r *DataRowReader) error {
onDataRow := func(r *pgx.DataRowReader) error {
v = r.ReadValue()
return nil
}

View File

@ -1,16 +1,34 @@
package pgx
package pgx_test
import (
"github.com/JackC/pgx"
)
type test interface {
Fatalf(format string, args ...interface{})
}
func mustPrepare(t test, conn *Connection, name, sql string) {
var SharedConnection *pgx.Connection
func GetSharedConnection() (c *pgx.Connection) {
if SharedConnection == nil {
var err error
SharedConnection, err = pgx.Connect(*defaultConnectionParameters)
if err != nil {
panic("Unable to establish connection")
}
}
return SharedConnection
}
func mustPrepare(t test, conn *pgx.Connection, name, sql string) {
if err := conn.Prepare(name, sql); err != nil {
t.Fatalf("Could not prepare %v: %v", name, err)
}
}
func mustExecute(t test, conn *Connection, sql string, arguments ...interface{}) (commandTag string) {
func mustExecute(t test, conn *pgx.Connection, sql string, arguments ...interface{}) (commandTag string) {
var err error
if commandTag, err = conn.Execute(sql, arguments...); err != nil {
t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err)
@ -18,7 +36,7 @@ func mustExecute(t test, conn *Connection, sql string, arguments ...interface{})
return
}
func mustSelectRow(t test, conn *Connection, sql string, arguments ...interface{}) (row map[string]interface{}) {
func mustSelectRow(t test, conn *pgx.Connection, sql string, arguments ...interface{}) (row map[string]interface{}) {
var err error
if row, err = conn.SelectRow(sql, arguments...); err != nil {
t.Fatalf("SelectRow unexpectedly failed with %v: %v", sql, err)
@ -26,7 +44,7 @@ func mustSelectRow(t test, conn *Connection, sql string, arguments ...interface{
return
}
func mustSelectRows(t test, conn *Connection, sql string, arguments ...interface{}) (rows []map[string]interface{}) {
func mustSelectRows(t test, conn *pgx.Connection, sql string, arguments ...interface{}) (rows []map[string]interface{}) {
var err error
if rows, err = conn.SelectRows(sql, arguments...); err != nil {
t.Fatalf("SelectRows unexpected failed with %v: %v", sql, err)
@ -34,7 +52,7 @@ func mustSelectRows(t test, conn *Connection, sql string, arguments ...interface
return
}
func mustSelectValue(t test, conn *Connection, sql string, arguments ...interface{}) (value interface{}) {
func mustSelectValue(t test, conn *pgx.Connection, sql string, arguments ...interface{}) (value interface{}) {
var err error
if value, err = conn.SelectValue(sql, arguments...); err != nil {
t.Fatalf("SelectValue unexpectedly failed with %v: %v", sql, err)

View File

@ -1,11 +1,11 @@
package pgx
package pgx_test
import (
"testing"
)
func TestQuoteString(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
if conn.QuoteString("test") != "'test'" {
t.Error("Failed to quote string")
@ -17,7 +17,7 @@ func TestQuoteString(t *testing.T) {
}
func TestSanitizeSql(t *testing.T) {
conn := getSharedConnection()
conn := GetSharedConnection()
if conn.SanitizeSql("select $1", "Jack's") != "select 'Jack''s'" {
t.Error("Failed to sanitize string")