Use pgx_test package for tests
This commit is contained in:
+34
-33
@@ -1,7 +1,8 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/JackC/pgx"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -15,7 +16,7 @@ var float4TextVsBinaryTestDataLoaded bool
|
|||||||
var float8TextVsBinaryTestDataLoaded bool
|
var float8TextVsBinaryTestDataLoaded bool
|
||||||
var boolTextVsBinaryTestDataLoaded bool
|
var boolTextVsBinaryTestDataLoaded bool
|
||||||
|
|
||||||
func createNarrowTestData(b *testing.B, conn *Connection) {
|
func createNarrowTestData(b *testing.B, conn *pgx.Connection) {
|
||||||
if narrowTestDataLoaded {
|
if narrowTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -46,23 +47,23 @@ func createNarrowTestData(b *testing.B, conn *Connection) {
|
|||||||
narrowTestDataLoaded = true
|
narrowTestDataLoaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeBinaryEncoders() (encoders map[Oid]func(*MessageReader, int32) interface{}) {
|
func removeBinaryEncoders() (encoders map[pgx.Oid]func(*pgx.MessageReader, int32) interface{}) {
|
||||||
encoders = make(map[Oid]func(*MessageReader, int32) interface{})
|
encoders = make(map[pgx.Oid]func(*pgx.MessageReader, int32) interface{})
|
||||||
for k, v := range ValueTranscoders {
|
for k, v := range pgx.ValueTranscoders {
|
||||||
encoders[k] = v.DecodeBinary
|
encoders[k] = v.DecodeBinary
|
||||||
ValueTranscoders[k].DecodeBinary = nil
|
pgx.ValueTranscoders[k].DecodeBinary = nil
|
||||||
}
|
}
|
||||||
return
|
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 {
|
for k, v := range encoders {
|
||||||
ValueTranscoders[k].DecodeBinary = v
|
pgx.ValueTranscoders[k].DecodeBinary = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowSimpleNarrow(b *testing.B) {
|
func BenchmarkSelectRowSimpleNarrow(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createNarrowTestData(b, conn)
|
createNarrowTestData(b, conn)
|
||||||
|
|
||||||
// Get random ids outside of timing
|
// Get random ids outside of timing
|
||||||
@@ -78,7 +79,7 @@ func BenchmarkSelectRowSimpleNarrow(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowPreparedNarrow(b *testing.B) {
|
func BenchmarkSelectRowPreparedNarrow(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createNarrowTestData(b, conn)
|
createNarrowTestData(b, conn)
|
||||||
|
|
||||||
// Get random ids outside of timing
|
// Get random ids outside of timing
|
||||||
@@ -94,7 +95,7 @@ func BenchmarkSelectRowPreparedNarrow(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowsSimpleNarrow(b *testing.B) {
|
func BenchmarkSelectRowsSimpleNarrow(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createNarrowTestData(b, conn)
|
createNarrowTestData(b, conn)
|
||||||
|
|
||||||
// Get random ids outside of timing
|
// Get random ids outside of timing
|
||||||
@@ -110,7 +111,7 @@ func BenchmarkSelectRowsSimpleNarrow(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowsPreparedNarrow(b *testing.B) {
|
func BenchmarkSelectRowsPreparedNarrow(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createNarrowTestData(b, conn)
|
createNarrowTestData(b, conn)
|
||||||
|
|
||||||
// Get random ids outside of timing
|
// 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 {
|
if testJoinsDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -197,7 +198,7 @@ func createJoinsTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowsSimpleJoins(b *testing.B) {
|
func BenchmarkSelectRowsSimpleJoins(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createJoinsTestData(b, conn)
|
createJoinsTestData(b, conn)
|
||||||
|
|
||||||
sql := `
|
sql := `
|
||||||
@@ -217,7 +218,7 @@ func BenchmarkSelectRowsSimpleJoins(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSelectRowsPreparedJoins(b *testing.B) {
|
func BenchmarkSelectRowsPreparedJoins(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createJoinsTestData(b, conn)
|
createJoinsTestData(b, conn)
|
||||||
|
|
||||||
b.ResetTimer()
|
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 {
|
if int2TextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -252,7 +253,7 @@ func createInt2TextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt2Text(b *testing.B) {
|
func BenchmarkInt2Text(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt2TextVsBinaryTestData(b, conn)
|
createInt2TextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -268,7 +269,7 @@ func BenchmarkInt2Text(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt2Binary(b *testing.B) {
|
func BenchmarkInt2Binary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt2TextVsBinaryTestData(b, conn)
|
createInt2TextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectInt16", "select * from t")
|
mustPrepare(b, conn, "selectInt16", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectInt16") }()
|
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 {
|
if int4TextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -305,7 +306,7 @@ func createInt4TextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt4Text(b *testing.B) {
|
func BenchmarkInt4Text(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt4TextVsBinaryTestData(b, conn)
|
createInt4TextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -321,7 +322,7 @@ func BenchmarkInt4Text(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt4Binary(b *testing.B) {
|
func BenchmarkInt4Binary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt4TextVsBinaryTestData(b, conn)
|
createInt4TextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectInt32", "select * from t")
|
mustPrepare(b, conn, "selectInt32", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectInt32") }()
|
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 {
|
if int8TextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -358,7 +359,7 @@ func createInt8TextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt8Text(b *testing.B) {
|
func BenchmarkInt8Text(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt8TextVsBinaryTestData(b, conn)
|
createInt8TextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -374,7 +375,7 @@ func BenchmarkInt8Text(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkInt8Binary(b *testing.B) {
|
func BenchmarkInt8Binary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createInt8TextVsBinaryTestData(b, conn)
|
createInt8TextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectInt64", "select * from t")
|
mustPrepare(b, conn, "selectInt64", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectInt64") }()
|
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 {
|
if float4TextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -411,7 +412,7 @@ func createFloat4TextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFloat4Text(b *testing.B) {
|
func BenchmarkFloat4Text(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createFloat4TextVsBinaryTestData(b, conn)
|
createFloat4TextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -427,7 +428,7 @@ func BenchmarkFloat4Text(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFloat4Binary(b *testing.B) {
|
func BenchmarkFloat4Binary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createFloat4TextVsBinaryTestData(b, conn)
|
createFloat4TextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectFloat32", "select * from t")
|
mustPrepare(b, conn, "selectFloat32", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectFloat32") }()
|
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 {
|
if float8TextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -464,7 +465,7 @@ func createFloat8TextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFloat8Text(b *testing.B) {
|
func BenchmarkFloat8Text(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createFloat8TextVsBinaryTestData(b, conn)
|
createFloat8TextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -480,7 +481,7 @@ func BenchmarkFloat8Text(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkFloat8Binary(b *testing.B) {
|
func BenchmarkFloat8Binary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createFloat8TextVsBinaryTestData(b, conn)
|
createFloat8TextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectFloat32", "select * from t")
|
mustPrepare(b, conn, "selectFloat32", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectFloat32") }()
|
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 {
|
if boolTextVsBinaryTestDataLoaded {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -517,7 +518,7 @@ func createBoolTextVsBinaryTestData(b *testing.B, conn *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkBoolText(b *testing.B) {
|
func BenchmarkBoolText(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createBoolTextVsBinaryTestData(b, conn)
|
createBoolTextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
encoders := removeBinaryEncoders()
|
encoders := removeBinaryEncoders()
|
||||||
@@ -533,7 +534,7 @@ func BenchmarkBoolText(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkBoolBinary(b *testing.B) {
|
func BenchmarkBoolBinary(b *testing.B) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
createBoolTextVsBinaryTestData(b, conn)
|
createBoolTextVsBinaryTestData(b, conn)
|
||||||
mustPrepare(b, conn, "selectBool", "select * from t")
|
mustPrepare(b, conn, "selectBool", "select * from t")
|
||||||
defer func() { conn.Deallocate("selectBool") }()
|
defer func() { conn.Deallocate("selectBool") }()
|
||||||
|
|||||||
+10
-10
@@ -30,11 +30,11 @@ type ConnectionParameters struct {
|
|||||||
type Connection struct {
|
type Connection struct {
|
||||||
conn net.Conn // the underlying TCP or unix domain socket connection
|
conn net.Conn // the underlying TCP or unix domain socket connection
|
||||||
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
|
buf *bytes.Buffer // work buffer to avoid constant alloc and dealloc
|
||||||
pid int32 // backend pid
|
Pid int32 // backend pid
|
||||||
secretKey int32 // key to use to send a cancel query message to the server
|
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
|
RuntimeParams map[string]string // parameters that have been reported by the server
|
||||||
parameters ConnectionParameters // parameters used when establishing this connection
|
parameters ConnectionParameters // parameters used when establishing this connection
|
||||||
txStatus byte
|
TxStatus byte
|
||||||
preparedStatements map[string]*preparedStatement
|
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.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)
|
c.preparedStatements = make(map[string]*preparedStatement)
|
||||||
|
|
||||||
msg := newStartupMessage()
|
msg := newStartupMessage()
|
||||||
@@ -498,7 +498,7 @@ func (c *Connection) transaction(isoLevel string, f func() bool) (committed bool
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if committed && c.txStatus == 'T' {
|
if committed && c.TxStatus == 'T' {
|
||||||
_, err = c.Execute("commit")
|
_, err = c.Execute("commit")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
committed = false
|
committed = false
|
||||||
@@ -593,7 +593,7 @@ func hexMD5(s string) string {
|
|||||||
func (c *Connection) rxParameterStatus(r *MessageReader) {
|
func (c *Connection) rxParameterStatus(r *MessageReader) {
|
||||||
key := r.ReadString()
|
key := r.ReadString()
|
||||||
value := r.ReadString()
|
value := r.ReadString()
|
||||||
c.runtimeParams[key] = value
|
c.RuntimeParams[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) rxErrorResponse(r *MessageReader) (err PgError) {
|
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) {
|
func (c *Connection) rxBackendKeyData(r *MessageReader) {
|
||||||
c.pid = r.ReadInt32()
|
c.Pid = r.ReadInt32()
|
||||||
c.secretKey = r.ReadInt32()
|
c.SecretKey = r.ReadInt32()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) rxReadyForQuery(r *MessageReader) {
|
func (c *Connection) rxReadyForQuery(r *MessageReader) {
|
||||||
c.txStatus = r.ReadByte()
|
c.TxStatus = r.ReadByte()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) rxRowDescription(r *MessageReader) (fields []FieldDescription) {
|
func (c *Connection) rxRowDescription(r *MessageReader) (fields []FieldDescription) {
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ func (p *ConnectionPool) Acquire() (c *Connection) {
|
|||||||
|
|
||||||
// Release gives up use of a connection.
|
// Release gives up use of a connection.
|
||||||
func (p *ConnectionPool) Release(c *Connection) {
|
func (p *ConnectionPool) Release(c *Connection) {
|
||||||
if c.txStatus != 'I' {
|
if c.TxStatus != 'I' {
|
||||||
c.Execute("rollback")
|
c.Execute("rollback")
|
||||||
}
|
}
|
||||||
p.connectionChannel <- c
|
p.connectionChannel <- c
|
||||||
|
|||||||
+15
-14
@@ -1,12 +1,13 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/JackC/pgx"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createConnectionPool(maxConnections int) *ConnectionPool {
|
func createConnectionPool(maxConnections int) *pgx.ConnectionPool {
|
||||||
pool, err := NewConnectionPool(*defaultConnectionParameters, maxConnections)
|
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, maxConnections)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Unable to create connection pool")
|
panic("Unable to create connection pool")
|
||||||
}
|
}
|
||||||
@@ -14,7 +15,7 @@ func createConnectionPool(maxConnections int) *ConnectionPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewConnectionPool(t *testing.T) {
|
func TestNewConnectionPool(t *testing.T) {
|
||||||
pool, err := NewConnectionPool(*defaultConnectionParameters, 5)
|
pool, err := pgx.NewConnectionPool(*defaultConnectionParameters, 5)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection pool")
|
t.Fatal("Unable to establish connection pool")
|
||||||
}
|
}
|
||||||
@@ -32,8 +33,8 @@ func TestPoolAcquireAndReleaseCycle(t *testing.T) {
|
|||||||
pool := createConnectionPool(maxConnections)
|
pool := createConnectionPool(maxConnections)
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
acquireAll := func() (connections []*Connection) {
|
acquireAll := func() (connections []*pgx.Connection) {
|
||||||
connections = make([]*Connection, maxConnections)
|
connections = make([]*pgx.Connection, maxConnections)
|
||||||
for i := 0; i < maxConnections; i++ {
|
for i := 0; i < maxConnections; i++ {
|
||||||
connections[i] = pool.Acquire()
|
connections[i] = pool.Acquire()
|
||||||
}
|
}
|
||||||
@@ -107,25 +108,25 @@ func TestPoolReleaseWithTransactions(t *testing.T) {
|
|||||||
if _, err = conn.Execute("select"); err == nil {
|
if _, err = conn.Execute("select"); err == nil {
|
||||||
t.Fatal("Did not receive expected error")
|
t.Fatal("Did not receive expected error")
|
||||||
}
|
}
|
||||||
if conn.txStatus != 'E' {
|
if conn.TxStatus != 'E' {
|
||||||
t.Fatalf("Expected txStatus to be 'E', instead it was '%c'", conn.txStatus)
|
t.Fatalf("Expected TxStatus to be 'E', instead it was '%c'", conn.TxStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.Release(conn)
|
pool.Release(conn)
|
||||||
|
|
||||||
if conn.txStatus != 'I' {
|
if conn.TxStatus != 'I' {
|
||||||
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.txStatus)
|
t.Fatalf("Expected release to rollback errored transaction, but it did not: '%c'", conn.TxStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn = pool.Acquire()
|
conn = pool.Acquire()
|
||||||
mustExecute(t, conn, "begin")
|
mustExecute(t, conn, "begin")
|
||||||
if conn.txStatus != 'T' {
|
if conn.TxStatus != 'T' {
|
||||||
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.txStatus)
|
t.Fatalf("Expected txStatus to be 'T', instead it was '%c'", conn.TxStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.Release(conn)
|
pool.Release(conn)
|
||||||
|
|
||||||
if conn.txStatus != 'I' {
|
if conn.TxStatus != 'I' {
|
||||||
t.Fatalf("Expected release to rollback uncommitted transaction, but it did not: '%c'", conn.txStatus)
|
t.Fatalf("Expected release to rollback uncommitted transaction, but it did not: '%c'", conn.TxStatus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
// To skip tests for specific connection / authentication types set that connection param to nil
|
||||||
var tcpConnectionParameters *ConnectionParameters = nil
|
var tcppgx.ConnectionParameters *pgx.ConnectionParameters = nil
|
||||||
var unixSocketConnectionParameters *ConnectionParameters = nil
|
var unixSocketpgx.ConnectionParameters *pgx.ConnectionParameters = nil
|
||||||
var md5ConnectionParameters *ConnectionParameters = nil
|
var md5pgx.ConnectionParameters *pgx.ConnectionParameters = nil
|
||||||
var plainPasswordConnectionParameters *ConnectionParameters = nil
|
var plainPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = nil
|
||||||
var noPasswordConnectionParameters *ConnectionParameters = nil
|
var noPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = nil
|
||||||
var invalidUserConnectionParameters *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 tcppgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.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 unixSocketpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.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 md5pgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.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 plainPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.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 noPasswordpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.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 invalidUserpgx.ConnectionParameters *pgx.ConnectionParameters = &pgx.ConnectionParameters{Host: "127.0.0.1", User: "invalid", Database: "pgx_test"}
|
||||||
|
|||||||
+31
-44
@@ -1,40 +1,27 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"github.com/JackC/pgx"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestConnect(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
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")
|
t.Error("Runtime parameters not stored")
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.pid == 0 {
|
if conn.Pid == 0 {
|
||||||
t.Error("Backend PID not stored")
|
t.Error("Backend PID not stored")
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.secretKey == 0 {
|
if conn.SecretKey == 0 {
|
||||||
t.Error("Backend secret key not stored")
|
t.Error("Backend secret key not stored")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +46,7 @@ func TestConnectWithUnixSocket(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := Connect(*unixSocketConnectionParameters)
|
conn, err := pgx.Connect(*unixSocketConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -75,7 +62,7 @@ func TestConnectWithTcp(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := Connect(*tcpConnectionParameters)
|
conn, err := pgx.Connect(*tcpConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -91,8 +78,8 @@ func TestConnectWithInvalidUser(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := Connect(*invalidUserConnectionParameters)
|
_, err := pgx.Connect(*invalidUserConnectionParameters)
|
||||||
pgErr, ok := err.(PgError)
|
pgErr, ok := err.(pgx.PgError)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err)
|
t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err)
|
||||||
}
|
}
|
||||||
@@ -106,7 +93,7 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := Connect(*plainPasswordConnectionParameters)
|
conn, err := pgx.Connect(*plainPasswordConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -122,7 +109,7 @@ func TestConnectWithMD5Password(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := Connect(*md5ConnectionParameters)
|
conn, err := pgx.Connect(*md5ConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@@ -134,7 +121,7 @@ func TestConnectWithMD5Password(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExecute(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" {
|
if results := mustExecute(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" {
|
||||||
t.Error("Unexpected results from Execute")
|
t.Error("Unexpected results from Execute")
|
||||||
@@ -161,7 +148,7 @@ func TestExecute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExecuteFailure(t *testing.T) {
|
func TestExecuteFailure(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -177,10 +164,10 @@ func TestExecuteFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectFunc(t *testing.T) {
|
func TestSelectFunc(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
var sum, rowCount int32
|
var sum, rowCount int32
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *pgx.DataRowReader) error {
|
||||||
rowCount++
|
rowCount++
|
||||||
sum += r.ReadValue().(int32)
|
sum += r.ReadValue().(int32)
|
||||||
return nil
|
return nil
|
||||||
@@ -199,7 +186,7 @@ func TestSelectFunc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectFuncFailure(t *testing.T) {
|
func TestSelectFuncFailure(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -216,7 +203,7 @@ func TestSelectFuncFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectRows(t *testing.T) {
|
func TestSelectRows(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
rows := mustSelectRows(t, conn, "select $1 as name, null as position", "Jack")
|
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) {
|
func TestSelectRow(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
row := mustSelectRow(t, conn, "select $1 as name, null as position", "Jack")
|
row := mustSelectRow(t, conn, "select $1 as name, null as position", "Jack")
|
||||||
if row["name"] != "Jack" {
|
if row["name"] != "Jack" {
|
||||||
@@ -254,18 +241,18 @@ func TestSelectRow(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err := conn.SelectRow("select 'Jack' as name where 1=2")
|
_, 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")
|
t.Error("No matching row should have returned NotSingleRowError")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = conn.SelectRow("select * from (values ('Matthew'), ('Mark')) t")
|
_, 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")
|
t.Error("Multiple matching rows should have returned NotSingleRowError")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectionSelectValue(t *testing.T) {
|
func TestConnectionSelectValue(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
test := func(sql string, expected interface{}, arguments ...interface{}) {
|
test := func(sql string, expected interface{}, arguments ...interface{}) {
|
||||||
v, err := conn.SelectValue(sql, arguments...)
|
v, err := conn.SelectValue(sql, arguments...)
|
||||||
@@ -289,23 +276,23 @@ func TestConnectionSelectValue(t *testing.T) {
|
|||||||
test("select 1.23::float8", float64(1.23))
|
test("select 1.23::float8", float64(1.23))
|
||||||
|
|
||||||
_, err := conn.SelectValue("select 'Jack' as name where 1=2")
|
_, 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")
|
t.Error("No matching row should have returned NoRowsFoundError")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = conn.SelectValue("select * from (values ('Matthew'), ('Mark')) t")
|
_, 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")
|
t.Error("Multiple matching rows should have returned NotSingleRowError")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = conn.SelectValue("select 'Matthew', 'Mark'")
|
_, 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")
|
t.Error("Multiple columns should have returned UnexpectedColumnCountError")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSelectValues(t *testing.T) {
|
func TestSelectValues(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
test := func(sql string, expected []interface{}, arguments ...interface{}) {
|
test := func(sql string, expected []interface{}, arguments ...interface{}) {
|
||||||
values, err := conn.SelectValues(sql, arguments...)
|
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)})
|
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'")
|
_, 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")
|
t.Error("Multiple columns should have returned UnexpectedColumnCountError")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPrepare(t *testing.T) {
|
func TestPrepare(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -411,7 +398,7 @@ func TestPrepare(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrepareFailure(t *testing.T) {
|
func TestPrepareFailure(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -427,7 +414,7 @@ func TestPrepareFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTransaction(t *testing.T) {
|
func TestTransaction(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
@@ -538,7 +525,7 @@ func TestTransaction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTransactionIso(t *testing.T) {
|
func TestTransactionIso(t *testing.T) {
|
||||||
conn, err := Connect(*defaultConnectionParameters)
|
conn, err := pgx.Connect(*defaultConnectionParameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unable to establish connection: %v", err)
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/JackC/pgx"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDataRowReaderReadValue(t *testing.T) {
|
func TestDataRowReaderReadValue(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
test := func(sql string, expected interface{}) {
|
test := func(sql string, expected interface{}) {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
|
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *pgx.DataRowReader) error {
|
||||||
v = r.ReadValue()
|
v = r.ReadValue()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-6
@@ -1,16 +1,34 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/JackC/pgx"
|
||||||
|
)
|
||||||
|
|
||||||
type test interface {
|
type test interface {
|
||||||
Fatalf(format string, args ...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 {
|
if err := conn.Prepare(name, sql); err != nil {
|
||||||
t.Fatalf("Could not prepare %v: %v", name, err)
|
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
|
var err error
|
||||||
if commandTag, err = conn.Execute(sql, arguments...); err != nil {
|
if commandTag, err = conn.Execute(sql, arguments...); err != nil {
|
||||||
t.Fatalf("Execute unexpectedly failed with %v: %v", sql, err)
|
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
|
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
|
var err error
|
||||||
if row, err = conn.SelectRow(sql, arguments...); err != nil {
|
if row, err = conn.SelectRow(sql, arguments...); err != nil {
|
||||||
t.Fatalf("SelectRow unexpectedly failed with %v: %v", sql, err)
|
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
|
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
|
var err error
|
||||||
if rows, err = conn.SelectRows(sql, arguments...); err != nil {
|
if rows, err = conn.SelectRows(sql, arguments...); err != nil {
|
||||||
t.Fatalf("SelectRows unexpected failed with %v: %v", sql, err)
|
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
|
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
|
var err error
|
||||||
if value, err = conn.SelectValue(sql, arguments...); err != nil {
|
if value, err = conn.SelectValue(sql, arguments...); err != nil {
|
||||||
t.Fatalf("SelectValue unexpectedly failed with %v: %v", sql, err)
|
t.Fatalf("SelectValue unexpectedly failed with %v: %v", sql, err)
|
||||||
|
|||||||
+3
-3
@@ -1,11 +1,11 @@
|
|||||||
package pgx
|
package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestQuoteString(t *testing.T) {
|
func TestQuoteString(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
if conn.QuoteString("test") != "'test'" {
|
if conn.QuoteString("test") != "'test'" {
|
||||||
t.Error("Failed to quote string")
|
t.Error("Failed to quote string")
|
||||||
@@ -17,7 +17,7 @@ func TestQuoteString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSanitizeSql(t *testing.T) {
|
func TestSanitizeSql(t *testing.T) {
|
||||||
conn := getSharedConnection()
|
conn := GetSharedConnection()
|
||||||
|
|
||||||
if conn.SanitizeSql("select $1", "Jack's") != "select 'Jack''s'" {
|
if conn.SanitizeSql("select $1", "Jack's") != "select 'Jack''s'" {
|
||||||
t.Error("Failed to sanitize string")
|
t.Error("Failed to sanitize string")
|
||||||
|
|||||||
Reference in New Issue
Block a user