Make connections configurable and skippable

It is a hassle to setup all potential connection and authentication
types. And it is impossible to test Unix domain sockets on Windows. Make
testing non-default connections optional.
pgx-vs-pq
Jack Christensen 2013-07-07 21:31:35 -05:00
parent a98db507b7
commit e99a9b2306
5 changed files with 109 additions and 31 deletions

2
.gitignore vendored
View File

@ -20,3 +20,5 @@ _cgo_export.*
_testmain.go
*.exe
connection_settings_test.go

View File

@ -1,25 +1,50 @@
pgx
===
# pgx
Experimental PostgreSQL client library for Go
Usage
=====
## Usage
TODO
Development
===========
## Development
Testing
-------
## Testing
To setup the test environment run the test_setup.sql script as a user that can
create users and databases. To successfully run the connection tests for various
means of authentication you must include the following in your pg_hba.conf.
Pgx supports multiple connection and authentication types. Setting up a test
environment that can test all of them can be cumbersome. In particular,
Windows cannot test Unix domain socket connections. Because of this pgx will
skip tests for connection types that are not configured.
### Normal Test Environment
To setup the normal test environment run the following SQL:
create user pgx_md5 password 'secret';
create database pgx_test;
Next open connection_settings_test.go.example and make a copy without the
.example. If your PostgreSQL server is accepting connections on 127.0.0.1,
then you are done.
### Connection and Authentication Test Environment
Comple the normal test environment setup and also do the following.
Run the following SQL:
create user pgx_none;
create user pgx_pw password 'secret';
Add the following to your pg_hba.conf:
If you are developing on Unix with domain socket connections:
local pgx_test pgx_none trust
local pgx_test pgx_pw password
local pgx_test pgx_md5 md5
If you are developing on Windows with TCP connections:
host pgx_test pgx_none 127.0.0.1/32 trust
host pgx_test pgx_pw 127.0.0.1/32 password
host pgx_test pgx_md5 127.0.0.1/32 md5

View File

@ -6,8 +6,7 @@ import (
)
func createConnectionPool(maxConnections int) *ConnectionPool {
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
pool, err := NewConnectionPool(connectionOptions, maxConnections)
pool, err := NewConnectionPool(*defaultConnectionParameters, maxConnections)
if err != nil {
panic("Unable to create connection pool")
}
@ -15,8 +14,7 @@ func createConnectionPool(maxConnections int) *ConnectionPool {
}
func TestNewConnectionPool(t *testing.T) {
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
pool, err := NewConnectionPool(connectionOptions, 5)
pool, err := NewConnectionPool(*defaultConnectionParameters, 5)
if err != nil {
t.Fatal("Unable to establish connection pool")
}

View File

@ -0,0 +1,18 @@
package pgx
var defaultConnectionParameters *ConnectionParameters = &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 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"}

View File

@ -10,7 +10,7 @@ var SharedConnection *Connection
func getSharedConnection() (c *Connection) {
if SharedConnection == nil {
var err error
SharedConnection, err = Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"})
SharedConnection, err = Connect(*defaultConnectionParameters)
if err != nil {
panic("Unable to establish connection")
}
@ -20,9 +20,9 @@ func getSharedConnection() (c *Connection) {
}
func TestConnect(t *testing.T) {
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"})
conn, err := Connect(*defaultConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection")
t.Fatalf("Unable to establish connection: %v", err)
}
if _, present := conn.runtimeParams["server_version"]; !present {
@ -39,13 +39,29 @@ func TestConnect(t *testing.T) {
var rows []map[string]interface{}
rows, err = conn.SelectRows("select current_database()")
if err != nil || rows[0]["current_database"] != "pgx_test" {
t.Error("Did not connect to specified database (pgx_text)")
if err != nil || rows[0]["current_database"] != defaultConnectionParameters.Database {
t.Errorf("Did not connect to specified database (%v)", defaultConnectionParameters.Database)
}
rows, err = conn.SelectRows("select current_user")
if err != nil || rows[0]["current_user"] != "pgx_none" {
t.Error("Did not connect as specified user (pgx_none)")
if err != nil || rows[0]["current_user"] != defaultConnectionParameters.User {
t.Errorf("Did not connect as specified user (%v)", defaultConnectionParameters.User)
}
err = conn.Close()
if err != nil {
t.Fatal("Unable to close connection")
}
}
func TestConnectWithUnixSocket(t *testing.T) {
if unixSocketConnectionParameters == nil {
return
}
conn, err := Connect(*unixSocketConnectionParameters)
if err != nil {
t.Fatalf("Unable to establish connection: %v", err)
}
err = conn.Close()
@ -55,7 +71,11 @@ func TestConnect(t *testing.T) {
}
func TestConnectWithTcp(t *testing.T) {
conn, err := Connect(ConnectionParameters{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"})
if tcpConnectionParameters == nil {
return
}
conn, err := Connect(*tcpConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -67,15 +87,26 @@ func TestConnectWithTcp(t *testing.T) {
}
func TestConnectWithInvalidUser(t *testing.T) {
_, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "invalid_user", Database: "pgx_test"})
pgErr := err.(PgError)
if pgErr.Code != "28000" {
t.Fatal("Did not receive expected error when connecting with invalid user")
if invalidUserConnectionParameters == nil {
return
}
_, err := Connect(*invalidUserConnectionParameters)
pgErr, ok := err.(PgError)
if !ok {
t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err)
}
if pgErr.Code != "28000" && pgErr.Code != "28P01" {
t.Fatalf("Expected to receive a PgError with code 28000 or 28P01, instead received: %v", pgErr)
}
}
func TestConnectWithPlainTextPassword(t *testing.T) {
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_pw", Password: "secret", Database: "pgx_test"})
if plainPasswordConnectionParameters == nil {
return
}
conn, err := Connect(*plainPasswordConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -87,7 +118,11 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
}
func TestConnectWithMD5Password(t *testing.T) {
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_md5", Password: "secret", Database: "pgx_test"})
if md5ConnectionParameters == nil {
return
}
conn, err := Connect(*md5ConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection: " + err.Error())
}
@ -288,9 +323,9 @@ func TestSelectValues(t *testing.T) {
}
func TestPrepare(t *testing.T) {
conn, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"})
conn, err := Connect(*defaultConnectionParameters)
if err != nil {
t.Fatal("Unable to establish connection")
t.Fatalf("Unable to establish connection: %v", err)
}
defer conn.Close()