mirror of
https://github.com/jackc/pgx.git
synced 2025-04-28 22:10:31 +00:00
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.
This commit is contained in:
parent
a98db507b7
commit
e99a9b2306
2
.gitignore
vendored
2
.gitignore
vendored
@ -20,3 +20,5 @@ _cgo_export.*
|
|||||||
_testmain.go
|
_testmain.go
|
||||||
|
|
||||||
*.exe
|
*.exe
|
||||||
|
|
||||||
|
connection_settings_test.go
|
||||||
|
47
README.md
47
README.md
@ -1,25 +1,50 @@
|
|||||||
pgx
|
# pgx
|
||||||
===
|
|
||||||
|
|
||||||
Experimental PostgreSQL client library for Go
|
Experimental PostgreSQL client library for Go
|
||||||
|
|
||||||
Usage
|
## Usage
|
||||||
=====
|
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
Development
|
## Development
|
||||||
===========
|
|
||||||
|
|
||||||
Testing
|
## Testing
|
||||||
-------
|
|
||||||
|
|
||||||
To setup the test environment run the test_setup.sql script as a user that can
|
Pgx supports multiple connection and authentication types. Setting up a test
|
||||||
create users and databases. To successfully run the connection tests for various
|
environment that can test all of them can be cumbersome. In particular,
|
||||||
means of authentication you must include the following in your pg_hba.conf.
|
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_none trust
|
||||||
local pgx_test pgx_pw password
|
local pgx_test pgx_pw password
|
||||||
local pgx_test pgx_md5 md5
|
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
|
||||||
|
@ -6,8 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func createConnectionPool(maxConnections int) *ConnectionPool {
|
func createConnectionPool(maxConnections int) *ConnectionPool {
|
||||||
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
|
pool, err := NewConnectionPool(*defaultConnectionParameters, maxConnections)
|
||||||
pool, err := NewConnectionPool(connectionOptions, maxConnections)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Unable to create connection pool")
|
panic("Unable to create connection pool")
|
||||||
}
|
}
|
||||||
@ -15,8 +14,7 @@ func createConnectionPool(maxConnections int) *ConnectionPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNewConnectionPool(t *testing.T) {
|
func TestNewConnectionPool(t *testing.T) {
|
||||||
connectionOptions := ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "pgx_none", Database: "pgx_test"}
|
pool, err := NewConnectionPool(*defaultConnectionParameters, 5)
|
||||||
pool, err := NewConnectionPool(connectionOptions, 5)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection pool")
|
t.Fatal("Unable to establish connection pool")
|
||||||
}
|
}
|
||||||
|
18
connection_settings_test.go.example
Normal file
18
connection_settings_test.go.example
Normal 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"}
|
@ -10,7 +10,7 @@ var SharedConnection *Connection
|
|||||||
func getSharedConnection() (c *Connection) {
|
func getSharedConnection() (c *Connection) {
|
||||||
if SharedConnection == nil {
|
if SharedConnection == nil {
|
||||||
var err error
|
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 {
|
if err != nil {
|
||||||
panic("Unable to establish connection")
|
panic("Unable to establish connection")
|
||||||
}
|
}
|
||||||
@ -20,9 +20,9 @@ func getSharedConnection() (c *Connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
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 {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection")
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, present := conn.runtimeParams["server_version"]; !present {
|
if _, present := conn.runtimeParams["server_version"]; !present {
|
||||||
@ -39,13 +39,29 @@ func TestConnect(t *testing.T) {
|
|||||||
|
|
||||||
var rows []map[string]interface{}
|
var rows []map[string]interface{}
|
||||||
rows, err = conn.SelectRows("select current_database()")
|
rows, err = conn.SelectRows("select current_database()")
|
||||||
if err != nil || rows[0]["current_database"] != "pgx_test" {
|
if err != nil || rows[0]["current_database"] != defaultConnectionParameters.Database {
|
||||||
t.Error("Did not connect to specified database (pgx_text)")
|
t.Errorf("Did not connect to specified database (%v)", defaultConnectionParameters.Database)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err = conn.SelectRows("select current_user")
|
rows, err = conn.SelectRows("select current_user")
|
||||||
if err != nil || rows[0]["current_user"] != "pgx_none" {
|
if err != nil || rows[0]["current_user"] != defaultConnectionParameters.User {
|
||||||
t.Error("Did not connect as specified user (pgx_none)")
|
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()
|
err = conn.Close()
|
||||||
@ -55,7 +71,11 @@ func TestConnect(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithTcp(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 {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -67,15 +87,26 @@ func TestConnectWithTcp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithInvalidUser(t *testing.T) {
|
func TestConnectWithInvalidUser(t *testing.T) {
|
||||||
_, err := Connect(ConnectionParameters{Socket: "/private/tmp/.s.PGSQL.5432", User: "invalid_user", Database: "pgx_test"})
|
if invalidUserConnectionParameters == nil {
|
||||||
pgErr := err.(PgError)
|
return
|
||||||
if pgErr.Code != "28000" {
|
}
|
||||||
t.Fatal("Did not receive expected error when connecting with invalid user")
|
|
||||||
|
_, 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) {
|
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 {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -87,7 +118,11 @@ func TestConnectWithPlainTextPassword(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithMD5Password(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 {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection: " + err.Error())
|
t.Fatal("Unable to establish connection: " + err.Error())
|
||||||
}
|
}
|
||||||
@ -288,9 +323,9 @@ func TestSelectValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPrepare(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 {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection")
|
t.Fatalf("Unable to establish connection: %v", err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user