From c672c0d59595f729130088a5b8104600ff2a062a Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sun, 30 Dec 2018 21:52:33 -0600 Subject: [PATCH] Use environment variables for test configuration --- .gitignore | 1 - .travis.yml | 6 +- README.md | 22 +++++-- batch_test.go | 25 ++++---- bench-tmp_test.go | 5 +- bench_test.go | 29 ++++----- conn_config_test.go.example | 20 ------ conn_config_test.go.travis | 29 --------- conn_pool_test.go | 15 ++--- conn_test.go | 121 +++++++++++++++++------------------- copy_from_test.go | 22 +++---- copy_to_test.go | 7 ++- example_custom_type_test.go | 3 +- example_json_test.go | 3 +- helper_test.go | 16 +++++ large_objects_test.go | 5 +- query_test.go | 81 ++++++++++++------------ replication_test.go | 37 ++++++----- travis/before_script.bash | 2 - tx_test.go | 17 ++--- v4.md | 1 + values_test.go | 31 ++++----- 22 files changed, 245 insertions(+), 253 deletions(-) delete mode 100644 conn_config_test.go.example delete mode 100644 conn_config_test.go.travis diff --git a/.gitignore b/.gitignore index 0ff00800..39175a96 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,4 @@ _testmain.go *.exe -conn_config_test.go .envrc diff --git a/.travis.yml b/.travis.yml index 060fac8a..950792d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,9 +17,9 @@ env: - PGX_TEST_MD5_PASSWORD_CONN_STRING=postgres://pgx_md5:secret@127.0.0.1/pgx_test - PGX_TEST_PLAIN_PASSWORD_CONN_STRING=postgres://pgx_pw:secret@127.0.0.1/pgx_test matrix: - - CRATEVERSION=2.1 - - PGVERSION=10 - - PGVERSION=9.6 + - CRATEVERSION=2.1 PGX_TEST_CRATEDB_CONN_STRING="host=127.0.0.1 port=6543 user=pgx database=pgx_test" + - PGVERSION=10 PGX_TEST_REPLICATION_CONN_STRING="host=127.0.0.1 port=6543 user=pgx_replication password=secret database=pgx_test" + - PGVERSION=9.6 PGX_TEST_REPLICATION_CONN_STRING="host=127.0.0.1 port=6543 user=pgx_replication password=secret database=pgx_test" - PGVERSION=9.5 - PGVERSION=9.4 - PGVERSION=9.3 diff --git a/README.md b/README.md index d6499ba4..7ba9eaf8 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,8 @@ 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. +pgx uses environment variables to configure the test database connections. Consider using [direnv](https://github.com/direnv/direnv) to simplify this. + ### Normal Test Environment To setup the normal test environment, first install these dependencies: @@ -102,12 +104,14 @@ Connect to database pgx_test and run: create extension hstore; create domain uint64 as numeric(20,0); -Next open conn_config_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. +Run the tests with environment variable PGX_TEST_DATABASE set to your test database. + + PGX_TEXT_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./... ### Connection and Authentication Test Environment +Additional tests are available for specific connection types (e.g. TCP, Unix domain sockets, no password, plain password, MD5 password, etc). + Complete the normal test environment setup and also do the following. Run the following SQL: @@ -129,6 +133,14 @@ If you are developing on Windows with TCP connections: host pgx_test pgx_pw 127.0.0.1/32 password host pgx_test pgx_md5 127.0.0.1/32 md5 +Each different test connection type uses a different connection string in an environment variable. + + export PGX_TEST_UNIX_SOCKET_CONN_STRING="host=/var/run/postgresql database=pgx_test" + export PGX_TEST_TCP_CONN_STRING="host=127.0.0.1 user=pgx_md5 password=secret database=pgx_test" + export PGX_TEST_TLS_CONN_STRING="host=127.0.0.1 user=pgx_md5 password=secret database=pgx_test sslmode=require" + export PGX_TEST_MD5_PASSWORD_CONN_STRING="host=127.0.0.1 user=pgx_md5 password=secret database=pgx_test" + export PGX_TEST_PLAIN_PASSWORD_CONN_STRING="host=127.0.0.1 user=pgx_pw password=secret database=pgx_test" + ### Replication Test Environment Add a replication user: @@ -145,7 +157,9 @@ Change the following settings in your postgresql.conf: max_wal_senders=5 max_replication_slots=5 -Set `replicationConnConfig` appropriately in `conn_config_test.go`. +Set the replication environment variable. + + export PGX_TEST_REPLICATION_CONN_STRING="host=127.0.0.1 user=pgx_replication password=secret database=pgx_test" ## Version Policy diff --git a/batch_test.go b/batch_test.go index 61bbe357..98772f62 100644 --- a/batch_test.go +++ b/batch_test.go @@ -2,6 +2,7 @@ package pgx_test import ( "context" + "os" "testing" "github.com/jackc/pgx" @@ -11,7 +12,7 @@ import ( func TestConnBeginBatch(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := `create temporary table ledger( @@ -152,7 +153,7 @@ func TestConnBeginBatch(t *testing.T) { func TestConnBeginBatchWithPreparedStatement(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) _, err := conn.Prepare("ps1", "select n from generate_series(0,$1::int) n") @@ -208,7 +209,7 @@ func TestConnBeginBatchWithPreparedStatement(t *testing.T) { func TestConnBeginBatchContextCancelBeforeExecResults(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) sql := `create temporary table ledger( id serial primary key, @@ -251,7 +252,7 @@ func TestConnBeginBatchContextCancelBeforeExecResults(t *testing.T) { func TestConnBeginBatchContextCancelBeforeQueryResults(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) batch := conn.BeginBatch() batch.Queue("select pg_sleep(2)", @@ -287,7 +288,7 @@ func TestConnBeginBatchContextCancelBeforeQueryResults(t *testing.T) { func TestConnBeginBatchContextCancelBeforeFinish(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) batch := conn.BeginBatch() batch.Queue("select pg_sleep(2)", @@ -323,7 +324,7 @@ func TestConnBeginBatchContextCancelBeforeFinish(t *testing.T) { func TestConnBeginBatchCloseRowsPartiallyRead(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) batch := conn.BeginBatch() @@ -394,7 +395,7 @@ func TestConnBeginBatchCloseRowsPartiallyRead(t *testing.T) { func TestConnBeginBatchQueryError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) batch := conn.BeginBatch() @@ -446,7 +447,7 @@ func TestConnBeginBatchQueryError(t *testing.T) { func TestConnBeginBatchQuerySyntaxError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) batch := conn.BeginBatch() @@ -480,7 +481,7 @@ func TestConnBeginBatchQuerySyntaxError(t *testing.T) { func TestConnBeginBatchQueryRowInsert(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := `create temporary table ledger( @@ -529,7 +530,7 @@ func TestConnBeginBatchQueryRowInsert(t *testing.T) { func TestConnBeginBatchQueryPartialReadInsert(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := `create temporary table ledger( @@ -578,7 +579,7 @@ func TestConnBeginBatchQueryPartialReadInsert(t *testing.T) { func TestTxBeginBatch(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := `create temporary table ledger1( @@ -663,7 +664,7 @@ func TestTxBeginBatch(t *testing.T) { func TestTxBeginBatchRollback(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := `create temporary table ledger1( diff --git a/bench-tmp_test.go b/bench-tmp_test.go index a8e3f7db..9e36bedc 100644 --- a/bench-tmp_test.go +++ b/bench-tmp_test.go @@ -1,11 +1,12 @@ package pgx_test import ( + "os" "testing" ) func BenchmarkPgtypeInt4ParseBinary(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnectString(b, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(b, conn) _, err := conn.Prepare("selectBinary", "select n::int4 from generate_series(1, 100) n") @@ -36,7 +37,7 @@ func BenchmarkPgtypeInt4ParseBinary(b *testing.B) { } func BenchmarkPgtypeInt4EncodeBinary(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnectString(b, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(b, conn) _, err := conn.Prepare("encodeBinary", "select $1::int4, $2::int4, $3::int4, $4::int4, $5::int4, $6::int4, $7::int4") diff --git a/bench_test.go b/bench_test.go index 7f82891e..3785a325 100644 --- a/bench_test.go +++ b/bench_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "os" "strings" "testing" "time" @@ -13,7 +14,7 @@ import ( ) func BenchmarkConnPool(b *testing.B) { - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 5} pool, err := pgx.NewConnPool(config) if err != nil { b.Fatalf("Unable to create connection pool: %v", err) @@ -31,7 +32,7 @@ func BenchmarkConnPool(b *testing.B) { } func BenchmarkConnPoolQueryRow(b *testing.B) { - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 5} pool, err := pgx.NewConnPool(config) if err != nil { b.Fatalf("Unable to create connection pool: %v", err) @@ -52,7 +53,7 @@ func BenchmarkConnPoolQueryRow(b *testing.B) { } func BenchmarkPointerPointerWithNullValues(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) _, err := conn.Prepare("selectNulls", "select 1::int4, 'johnsmith', null::text, null::text, null::text, null::date, null::timestamptz") @@ -112,7 +113,7 @@ func BenchmarkPointerPointerWithNullValues(b *testing.B) { } func BenchmarkPointerPointerWithPresentValues(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) _, err := conn.Prepare("selectNulls", "select 1::int4, 'johnsmith', 'johnsmith@example.com', 'John Smith', 'male', '1970-01-01'::date, '2015-01-01 00:00:00'::timestamptz") @@ -172,7 +173,7 @@ func BenchmarkPointerPointerWithPresentValues(b *testing.B) { } func BenchmarkSelectWithoutLogging(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) benchmarkSelectWithLog(b, conn) @@ -183,7 +184,7 @@ type discardLogger struct{} func (dl discardLogger) Log(level pgx.LogLevel, msg string, data map[string]interface{}) {} func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) var logger discardLogger @@ -194,7 +195,7 @@ func BenchmarkSelectWithLoggingTraceDiscard(b *testing.B) { } func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) var logger discardLogger @@ -205,7 +206,7 @@ func BenchmarkSelectWithLoggingDebugWithDiscard(b *testing.B) { } func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) var logger discardLogger @@ -216,7 +217,7 @@ func BenchmarkSelectWithLoggingInfoWithDiscard(b *testing.B) { } func BenchmarkSelectWithLoggingErrorWithDiscard(b *testing.B) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) var logger discardLogger @@ -373,7 +374,7 @@ func newBenchmarkWriteTableCopyFromSrc(count int) pgx.CopyFromSource { } func benchmarkWriteNRowsViaInsert(b *testing.B, n int) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) mustExec(b, conn, benchmarkWriteTableCreateSQL) @@ -484,7 +485,7 @@ func multiInsert(conn *pgx.Conn, tableName string, columnNames []string, rowSrc } func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) mustExec(b, conn, benchmarkWriteTableCreateSQL) @@ -520,7 +521,7 @@ func benchmarkWriteNRowsViaMultiInsert(b *testing.B, n int) { } func benchmarkWriteNRowsViaCopy(b *testing.B, n int) { - conn := mustConnect(b, *defaultConnConfig) + conn := mustConnect(b, mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))) defer closeConn(b, conn) mustExec(b, conn, benchmarkWriteTableCreateSQL) @@ -612,7 +613,7 @@ func BenchmarkWrite10000RowsViaCopy(b *testing.B) { } func BenchmarkMultipleQueriesNonBatch(b *testing.B) { - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 5} pool, err := pgx.NewConnPool(config) if err != nil { b.Fatalf("Unable to create connection pool: %v", err) @@ -647,7 +648,7 @@ func BenchmarkMultipleQueriesNonBatch(b *testing.B) { } func BenchmarkMultipleQueriesBatch(b *testing.B) { - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 5} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 5} pool, err := pgx.NewConnPool(config) if err != nil { b.Fatalf("Unable to create connection pool: %v", err) diff --git a/conn_config_test.go.example b/conn_config_test.go.example deleted file mode 100644 index bbe14438..00000000 --- a/conn_config_test.go.example +++ /dev/null @@ -1,20 +0,0 @@ -package pgx_test - -import ( - // "crypto/tls" - // "crypto/x509" - // "fmt" - // "go/build" - // "io/ioutil" - // "path" - - "github.com/jackc/pgx" -) - -var defaultConnConfig *pgx.ConnConfig = &pgx.ConnConfig{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 replicationConnConfig *pgx.ConnConfig = nil -var cratedbConnConfig *pgx.ConnConfig = nil - -// var replicationConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_replication", Password: "secret", Database: "pgx_test"} diff --git a/conn_config_test.go.travis b/conn_config_test.go.travis deleted file mode 100644 index d67f6887..00000000 --- a/conn_config_test.go.travis +++ /dev/null @@ -1,29 +0,0 @@ -package pgx_test - -import ( - "crypto/tls" - "github.com/jackc/pgx" - "os" - "strconv" -) - -var defaultConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"} -var replicationConnConfig *pgx.ConnConfig = nil -var cratedbConnConfig *pgx.ConnConfig = nil - -func init() { - pgVersion := os.Getenv("PGVERSION") - - if len(pgVersion) > 0 { - v, err := strconv.ParseFloat(pgVersion, 64) - if err == nil && v >= 9.6 { - replicationConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_replication", Password: "secret", Database: "pgx_test"} - } - } - - crateVersion := os.Getenv("CRATEVERSION") - if crateVersion != "" { - cratedbConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", Port: 6543, User: "pgx", Password: "", Database: "pgx_test"} - } -} - diff --git a/conn_pool_test.go b/conn_pool_test.go index a7eebdd4..5331fbd4 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net" + "os" "sync" "testing" "time" @@ -14,7 +15,7 @@ import ( ) func createConnPool(t *testing.T, maxConnections int) *pgx.ConnPool { - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: maxConnections} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: maxConnections} pool, err := pgx.NewConnPool(config) if err != nil { t.Fatalf("Unable to create connection pool: %v", err) @@ -54,7 +55,7 @@ func TestNewConnPool(t *testing.T) { return nil } - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 2, AfterConnect: afterConnect} pool, err := pgx.NewConnPool(config) if err != nil { t.Fatal("Unable to establish connection pool") @@ -73,7 +74,7 @@ func TestNewConnPool(t *testing.T) { return errAfterConnect } - config = pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig, MaxConnections: 2, AfterConnect: afterConnect} + config = pgx.ConnPoolConfig{ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 2, AfterConnect: afterConnect} pool, err = pgx.NewConnPool(config) if err != errAfterConnect { t.Errorf("Expected errAfterConnect but received unexpected: %v", err) @@ -83,7 +84,7 @@ func TestNewConnPool(t *testing.T) { func TestNewConnPoolDefaultsTo5MaxConnections(t *testing.T) { t.Parallel() - config := pgx.ConnPoolConfig{ConnConfig: *defaultConnConfig} + config := pgx.ConnPoolConfig{ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))} pool, err := pgx.NewConnPool(config) if err != nil { t.Fatal("Unable to establish connection pool") @@ -179,7 +180,7 @@ func TestPoolNonBlockingConnections(t *testing.T) { maxConnections := 3 config := pgx.ConnPoolConfig{ - ConnConfig: *defaultConnConfig, + ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: maxConnections, } config.ConnConfig.Config.DialFunc = testDialer @@ -228,7 +229,7 @@ func TestAcquireTimeoutSanity(t *testing.T) { t.Parallel() config := pgx.ConnPoolConfig{ - ConnConfig: *defaultConnConfig, + ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 1, } @@ -260,7 +261,7 @@ func TestPoolWithAcquireTimeoutSet(t *testing.T) { connAllocTimeout := 2 * time.Second config := pgx.ConnPoolConfig{ - ConnConfig: *defaultConnConfig, + ConnConfig: mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")), MaxConnections: 1, AcquireTimeout: connAllocTimeout, } diff --git a/conn_test.go b/conn_test.go index 4b4c8562..5b70a9ac 100644 --- a/conn_test.go +++ b/conn_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "context" "fmt" + "os" "strconv" "strings" "sync" @@ -11,19 +12,20 @@ import ( "github.com/jackc/pgx" "github.com/jackc/pgx/pgtype" + "github.com/stretchr/testify/require" ) func TestCrateDBConnect(t *testing.T) { t.Parallel() - if cratedbConnConfig == nil { - t.Skip("Skipping due to undefined cratedbConnConfig") + connString := os.Getenv("PGX_TEST_CRATEDB_CONN_STRING") + if connString == "" { + t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_CRATEDB_CONN_STRING") } - conn, err := pgx.ConnectConfig(context.Background(), cratedbConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } + conn, err := pgx.Connect(context.Background(), connString) + require.Nil(t, err) + defer closeConn(t, conn) var result int err = conn.QueryRow("select 1 +1").Scan(&result) @@ -33,17 +35,14 @@ func TestCrateDBConnect(t *testing.T) { if result != 2 { t.Errorf("bad result: %d", result) } - - err = conn.Close() - if err != nil { - t.Fatal("Unable to close connection") - } } func TestConnect(t *testing.T) { t.Parallel() - conn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) + config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")) + + conn, err := pgx.ConnectConfig(context.Background(), &config) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } @@ -61,8 +60,8 @@ func TestConnect(t *testing.T) { if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } - if currentDB != defaultConnConfig.Config.Database { - t.Errorf("Did not connect to specified database (%v)", defaultConnConfig.Config.Database) + if currentDB != config.Config.Database { + t.Errorf("Did not connect to specified database (%v)", config.Config.Database) } var user string @@ -70,8 +69,8 @@ func TestConnect(t *testing.T) { if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } - if user != defaultConnConfig.Config.User { - t.Errorf("Did not connect as specified user (%v)", defaultConnConfig.Config.User) + if user != config.Config.User { + t.Errorf("Did not connect as specified user (%v)", config.Config.User) } err = conn.Close() @@ -83,7 +82,7 @@ func TestConnect(t *testing.T) { func TestConnectWithPreferSimpleProtocol(t *testing.T) { t.Parallel() - connConfig := *defaultConnConfig + connConfig := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")) connConfig.PreferSimpleProtocol = true conn := mustConnect(t, connConfig) @@ -108,7 +107,7 @@ func TestConnectWithPreferSimpleProtocol(t *testing.T) { func TestExec(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if results := mustExec(t, conn, "create temporary table foo(id integer primary key);"); results != "CREATE TABLE" { @@ -143,7 +142,7 @@ func TestExec(t *testing.T) { func TestExecFailure(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if _, err := conn.Exec("selct;"); err == nil { @@ -166,7 +165,7 @@ func TestExecFailure(t *testing.T) { func TestExecFailureWithArguments(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if _, err := conn.Exec("selct $1;", 1); err == nil { @@ -180,7 +179,7 @@ func TestExecFailureWithArguments(t *testing.T) { func TestExecExContextWithoutCancelation(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -201,7 +200,7 @@ func TestExecExContextWithoutCancelation(t *testing.T) { func TestExecExContextFailureWithoutCancelation(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -227,7 +226,7 @@ func TestExecExContextFailureWithoutCancelation(t *testing.T) { func TestExecExContextFailureWithoutCancelationWithArguments(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -244,7 +243,7 @@ func TestExecExContextFailureWithoutCancelationWithArguments(t *testing.T) { func TestExecExContextCancelationCancelsQuery(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -267,7 +266,7 @@ func TestExecExContextCancelationCancelsQuery(t *testing.T) { func TestExecFailureCloseBefore(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) closeConn(t, conn) if _, err := conn.Exec("select 1"); err == nil { @@ -281,7 +280,7 @@ func TestExecFailureCloseBefore(t *testing.T) { func TestExecExExtendedProtocol(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -314,7 +313,7 @@ func TestExecExExtendedProtocol(t *testing.T) { func TestExecExSimpleProtocol(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -351,7 +350,7 @@ func TestExecExSimpleProtocol(t *testing.T) { func TestConnExecExSuppliedCorrectParameterOIDs(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "create temporary table foo(name varchar primary key);") @@ -376,7 +375,7 @@ func TestConnExecExSuppliedCorrectParameterOIDs(t *testing.T) { func TestConnExecExSuppliedIncorrectParameterOIDs(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "create temporary table foo(name varchar primary key);") @@ -398,7 +397,7 @@ func TestConnExecExSuppliedIncorrectParameterOIDs(t *testing.T) { func TestConnExecExIncorrectParameterOIDsAfterAnotherQuery(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "create temporary table foo(name varchar primary key);") @@ -429,7 +428,7 @@ func TestConnExecExIncorrectParameterOIDsAfterAnotherQuery(t *testing.T) { func TestExecExFailureCloseBefore(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) closeConn(t, conn) if _, err := conn.ExecEx(context.Background(), "select 1", nil); err == nil { @@ -443,7 +442,7 @@ func TestExecExFailureCloseBefore(t *testing.T) { func TestPrepare(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) _, err := conn.Prepare("test", "select $1::varchar") @@ -495,7 +494,7 @@ func TestPrepare(t *testing.T) { func TestPrepareBadSQLFailure(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if _, err := conn.Prepare("badSQL", "select foo"); err == nil { @@ -508,7 +507,7 @@ func TestPrepareBadSQLFailure(t *testing.T) { func TestPrepareQueryManyParameters(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -578,7 +577,7 @@ func TestPrepareQueryManyParameters(t *testing.T) { func TestPrepareIdempotency(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) for i := 0; i < 2; i++ { @@ -608,7 +607,7 @@ func TestPrepareIdempotency(t *testing.T) { func TestPrepareEx(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) _, err := conn.PrepareEx(context.Background(), "test", "select $1", &pgx.PrepareExOptions{ParameterOIDs: []pgtype.OID{pgtype.TextOID}}) @@ -636,14 +635,14 @@ func TestPrepareEx(t *testing.T) { func TestListenNotify(t *testing.T) { t.Parallel() - listener := mustConnect(t, *defaultConnConfig) + listener := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, listener) if err := listener.Listen("chat"); err != nil { t.Fatalf("Unable to start listening: %v", err) } - notifier := mustConnect(t, *defaultConnConfig) + notifier := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, notifier) mustExec(t, notifier, "notify chat") @@ -700,14 +699,14 @@ func TestListenNotify(t *testing.T) { func TestUnlistenSpecificChannel(t *testing.T) { t.Parallel() - listener := mustConnect(t, *defaultConnConfig) + listener := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, listener) if err := listener.Listen("unlisten_test"); err != nil { t.Fatalf("Unable to start listening: %v", err) } - notifier := mustConnect(t, *defaultConnConfig) + notifier := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, notifier) mustExec(t, notifier, "notify unlisten_test") @@ -747,7 +746,7 @@ func TestListenNotifyWhileBusyIsSafe(t *testing.T) { listenerDone := make(chan bool) go func() { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) defer func() { listenerDone <- true @@ -790,7 +789,7 @@ func TestListenNotifyWhileBusyIsSafe(t *testing.T) { }() go func() { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) for i := 0; i < 100000; i++ { @@ -805,7 +804,7 @@ func TestListenNotifyWhileBusyIsSafe(t *testing.T) { func TestListenNotifySelfNotification(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if err := conn.Listen("self"); err != nil { @@ -848,7 +847,7 @@ func TestListenNotifySelfNotification(t *testing.T) { func TestListenUnlistenSpecialCharacters(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) chanName := "special characters !@#{$%^&*()}" @@ -864,7 +863,7 @@ func TestListenUnlistenSpecialCharacters(t *testing.T) { func TestFatalRxError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var wg sync.WaitGroup @@ -881,10 +880,7 @@ func TestFatalRxError(t *testing.T) { } }() - otherConn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } + otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer otherConn.Close() if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.PID()); err != nil { @@ -904,16 +900,13 @@ func TestFatalTxError(t *testing.T) { // Run timing sensitive test many times for i := 0; i < 50; i++ { func() { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) - otherConn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) - if err != nil { - t.Fatalf("Unable to establish connection: %v", err) - } + otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer otherConn.Close() - _, err = otherConn.Exec("select pg_terminate_backend($1)", conn.PID()) + _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.PID()) if err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -958,7 +951,7 @@ func TestCommandTag(t *testing.T) { func TestInsertBoolArray(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if results := mustExec(t, conn, "create temporary table foo(spice bool[]);"); results != "CREATE TABLE" { @@ -974,7 +967,7 @@ func TestInsertBoolArray(t *testing.T) { func TestInsertTimestampArray(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) if results := mustExec(t, conn, "create temporary table foo(spice timestamp[]);"); results != "CREATE TABLE" { @@ -990,7 +983,7 @@ func TestInsertTimestampArray(t *testing.T) { func TestCatchSimultaneousConnectionQueries(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows1, err := conn.Query("select generate_series(1,$1)", 10) @@ -1008,7 +1001,7 @@ func TestCatchSimultaneousConnectionQueries(t *testing.T) { func TestCatchSimultaneousConnectionQueryAndExec(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows, err := conn.Query("select generate_series(1,$1)", 10) @@ -1040,7 +1033,7 @@ func (l *testLogger) Log(level pgx.LogLevel, msg string, data map[string]interfa func TestSetLogger(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) l1 := &testLogger{} @@ -1075,7 +1068,7 @@ func TestSetLogger(t *testing.T) { func TestSetLogLevel(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) logger := &testLogger{} @@ -1152,7 +1145,7 @@ func TestConnOnNotice(t *testing.T) { var msg string - connConfig := *defaultConnConfig + connConfig := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")) connConfig.OnNotice = func(c *pgx.Conn, notice *pgx.Notice) { msg = notice.Message } @@ -1175,7 +1168,7 @@ end$$;`) } func TestConnInitConnInfo(t *testing.T) { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // spot check that the standard postgres type names aren't qualified @@ -1203,7 +1196,7 @@ func TestConnInitConnInfo(t *testing.T) { } func TestDomainType(t *testing.T) { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) dt, ok := conn.ConnInfo.DataTypeForName("uint64") diff --git a/copy_from_test.go b/copy_from_test.go index df9503dc..9481c476 100644 --- a/copy_from_test.go +++ b/copy_from_test.go @@ -18,7 +18,7 @@ import ( func TestConnCopyFromSmall(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -109,7 +109,7 @@ func TestConnCopyFromSmall(t *testing.T) { func TestConnCopyFromLarge(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -202,7 +202,7 @@ func TestConnCopyFromLarge(t *testing.T) { func TestConnCopyFromJSON(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) for _, typeName := range []string{"json", "jsonb"} { @@ -313,7 +313,7 @@ func (cfs *clientFailSource) Err() error { func TestConnCopyFromFailServerSideMidway(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -424,7 +424,7 @@ func (fs *failSource) Err() error { func TestConnCopyFromFailServerSideMidwayAbortsWithoutWaiting(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -478,7 +478,7 @@ func TestConnCopyFromFailServerSideMidwayAbortsWithoutWaiting(t *testing.T) { func TestConnCopyFromCopyFromSourceErrorMidway(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -538,7 +538,7 @@ func (cfs *clientFinalErrSource) Err() error { func TestConnCopyFromCopyFromSourceErrorEnd(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -596,7 +596,7 @@ func (cfs *nextPanicSource) Err() error { func TestConnCopyFromCopyFromSourceNextPanic(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -627,7 +627,7 @@ func TestConnCopyFromCopyFromSourceNextPanic(t *testing.T) { func TestConnCopyFromReaderQueryError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) inputReader := strings.NewReader("") @@ -652,7 +652,7 @@ func TestConnCopyFromReaderQueryError(t *testing.T) { func TestConnCopyFromReaderNoTableError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) inputReader := strings.NewReader("") @@ -677,7 +677,7 @@ func TestConnCopyFromReaderNoTableError(t *testing.T) { func TestConnCopyFromGzipReader(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( diff --git a/copy_to_test.go b/copy_to_test.go index cff837cb..1c2f8a6c 100644 --- a/copy_to_test.go +++ b/copy_to_test.go @@ -2,6 +2,7 @@ package pgx_test import ( "bytes" + "os" "testing" "github.com/jackc/pgx" @@ -10,7 +11,7 @@ import ( func TestConnCopyToWriterSmall(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -50,7 +51,7 @@ func TestConnCopyToWriterSmall(t *testing.T) { func TestConnCopyToWriterLarge(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, `create temporary table foo( @@ -92,7 +93,7 @@ func TestConnCopyToWriterLarge(t *testing.T) { func TestConnCopyToWriterQueryError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) outputWriter := bytes.NewBuffer(make([]byte, 0)) diff --git a/example_custom_type_test.go b/example_custom_type_test.go index 11417618..2eb36e6e 100644 --- a/example_custom_type_test.go +++ b/example_custom_type_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "context" "fmt" + "os" "regexp" "strconv" @@ -73,7 +74,7 @@ func (src *Point) String() string { } func Example_CustomType() { - conn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) if err != nil { fmt.Printf("Unable to establish connection: %v", err) return diff --git a/example_json_test.go b/example_json_test.go index c1670949..5b0623ef 100644 --- a/example_json_test.go +++ b/example_json_test.go @@ -3,12 +3,13 @@ package pgx_test import ( "context" "fmt" + "os" "github.com/jackc/pgx" ) func Example_JSON() { - conn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) if err != nil { fmt.Printf("Unable to establish connection: %v", err) return diff --git a/helper_test.go b/helper_test.go index f967d7a1..e83e5ed6 100644 --- a/helper_test.go +++ b/helper_test.go @@ -5,8 +5,24 @@ import ( "testing" "github.com/jackc/pgx" + "github.com/jackc/pgx/pgconn" + "github.com/stretchr/testify/require" ) +func mustConnectString(t testing.TB, connString string) *pgx.Conn { + conn, err := pgx.Connect(context.Background(), connString) + if err != nil { + t.Fatalf("Unable to establish connection: %v", err) + } + return conn +} + +func mustParseConfig(t testing.TB, connString string) pgx.ConnConfig { + config, err := pgconn.ParseConfig(connString) + require.Nil(t, err) + return pgx.ConnConfig{Config: *config} +} + func mustConnect(t testing.TB, config pgx.ConnConfig) *pgx.Conn { conn, err := pgx.ConnectConfig(context.Background(), &config) if err != nil { diff --git a/large_objects_test.go b/large_objects_test.go index 1fdd7627..5192ac4f 100644 --- a/large_objects_test.go +++ b/large_objects_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "context" "io" + "os" "testing" "github.com/jackc/pgx" @@ -11,7 +12,7 @@ import ( func TestLargeObjects(t *testing.T) { t.Parallel() - conn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) if err != nil { t.Fatal(err) } @@ -124,7 +125,7 @@ func TestLargeObjects(t *testing.T) { func TestLargeObjectsMultipleTransactions(t *testing.T) { t.Parallel() - conn, err := pgx.ConnectConfig(context.Background(), defaultConnConfig) + conn, err := pgx.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) if err != nil { t.Fatal(err) } diff --git a/query_test.go b/query_test.go index 06b7b8b7..c617f109 100644 --- a/query_test.go +++ b/query_test.go @@ -5,6 +5,7 @@ import ( "context" "database/sql" "fmt" + "os" "reflect" "strings" "testing" @@ -14,14 +15,14 @@ import ( "github.com/jackc/pgx" "github.com/jackc/pgx/pgtype" satori "github.com/jackc/pgx/pgtype/ext/satori-uuid" - "github.com/satori/go.uuid" + uuid "github.com/satori/go.uuid" "github.com/shopspring/decimal" ) func TestConnQueryScan(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var sum, rowCount int32 @@ -54,7 +55,7 @@ func TestConnQueryScan(t *testing.T) { func TestConnQueryScanWithManyColumns(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) columnCount := 1000 @@ -106,7 +107,7 @@ func TestConnQueryScanWithManyColumns(t *testing.T) { func TestConnQueryValues(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var rowCount int32 @@ -160,7 +161,7 @@ func TestConnQueryValues(t *testing.T) { func TestConnQueryValuesWithMultipleComplexColumnsOfSameType(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) expected0 := &pgtype.Int8Array{ @@ -222,7 +223,7 @@ func TestConnQueryValuesWithMultipleComplexColumnsOfSameType(t *testing.T) { func TestRowsScanDoesNotAllowScanningBinaryFormatValuesIntoString(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var s string @@ -239,7 +240,7 @@ func TestRowsScanDoesNotAllowScanningBinaryFormatValuesIntoString(t *testing.T) func TestConnQueryCloseEarly(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // Immediately close query without reading any rows @@ -276,7 +277,7 @@ func TestConnQueryCloseEarly(t *testing.T) { func TestConnQueryCloseEarlyWithErrorOnWire(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows, err := conn.Query("select 1/(10-n) from generate_series(1,10) n") @@ -295,7 +296,7 @@ func TestConnQueryCloseEarlyWithErrorOnWire(t *testing.T) { func TestConnQueryReadWrongTypeError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // Read a single value incorrectly @@ -331,7 +332,7 @@ func TestConnQueryReadWrongTypeError(t *testing.T) { func TestConnQueryReadTooManyValues(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // Read too many values @@ -362,7 +363,7 @@ func TestConnQueryReadTooManyValues(t *testing.T) { func TestConnQueryScanIgnoreColumn(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows, err := conn.Query("select 1::int8, 2::int8, 3::int8") @@ -396,7 +397,7 @@ func TestConnQueryScanIgnoreColumn(t *testing.T) { func TestConnQueryErrorWhileReturningRows(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) for i := 0; i < 100; i++ { @@ -427,7 +428,7 @@ func TestConnQueryErrorWhileReturningRows(t *testing.T) { func TestQueryEncodeError(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows, err := conn.Query("select $1::integer", "wrong") @@ -452,7 +453,7 @@ func TestQueryEncodeError(t *testing.T) { func TestQueryRowCoreTypes(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type allTypes struct { @@ -509,7 +510,7 @@ func TestQueryRowCoreTypes(t *testing.T) { func TestQueryRowCoreIntegerEncoding(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type allTypes struct { @@ -624,7 +625,7 @@ func TestQueryRowCoreIntegerEncoding(t *testing.T) { func TestQueryRowCoreIntegerDecoding(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type allTypes struct { @@ -799,7 +800,7 @@ func TestQueryRowCoreIntegerDecoding(t *testing.T) { func TestQueryRowCoreByteSlice(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -832,7 +833,7 @@ func TestQueryRowCoreByteSlice(t *testing.T) { func TestQueryRowUnknownType(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // Clear existing type mappings @@ -868,7 +869,7 @@ func TestQueryRowUnknownType(t *testing.T) { func TestQueryRowErrors(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type allTypes struct { @@ -909,7 +910,7 @@ func TestQueryRowErrors(t *testing.T) { func TestQueryRowExErrorsWrongParameterOIDs(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) sql := ` @@ -946,7 +947,7 @@ func TestQueryRowExErrorsWrongParameterOIDs(t *testing.T) { func TestQueryRowNoResults(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var n int32 @@ -959,7 +960,7 @@ func TestQueryRowNoResults(t *testing.T) { } func TestReadingValueAfterEmptyArray(t *testing.T) { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var a []string @@ -979,7 +980,7 @@ func TestReadingValueAfterEmptyArray(t *testing.T) { } func TestReadingNullByteArray(t *testing.T) { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var a []byte @@ -994,7 +995,7 @@ func TestReadingNullByteArray(t *testing.T) { } func TestReadingNullByteArrays(t *testing.T) { - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) rows, err := conn.Query("select null::text union all select null::text") @@ -1023,7 +1024,7 @@ func TestReadingNullByteArrays(t *testing.T) { func TestConnQueryDatabaseSQLScanner(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var num decimal.Decimal @@ -1050,7 +1051,7 @@ func TestConnQueryDatabaseSQLScanner(t *testing.T) { func TestConnQueryDatabaseSQLDriverValuer(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) expected, err := decimal.NewFromString("1234.567") @@ -1075,7 +1076,7 @@ func TestConnQueryDatabaseSQLDriverValuer(t *testing.T) { func TestConnQueryDatabaseSQLDriverValuerWithAutoGeneratedPointerReceiver(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "create temporary table t(n numeric)") @@ -1095,7 +1096,7 @@ func TestConnQueryDatabaseSQLDriverValuerWithAutoGeneratedPointerReceiver(t *tes func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) conn.ConnInfo.RegisterDataType(pgtype.DataType{ @@ -1125,7 +1126,7 @@ func TestConnQueryDatabaseSQLDriverValuerWithBinaryPgTypeThatAcceptsSameType(t * func TestConnQueryDatabaseSQLNullX(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type row struct { @@ -1182,7 +1183,7 @@ func TestConnQueryDatabaseSQLNullX(t *testing.T) { func TestQueryExContextSuccess(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1222,7 +1223,7 @@ func TestQueryExContextSuccess(t *testing.T) { func TestQueryExContextErrorWhileReceivingRows(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1259,7 +1260,7 @@ func TestQueryExContextErrorWhileReceivingRows(t *testing.T) { func TestQueryExContextCancelationCancelsQuery(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1290,7 +1291,7 @@ func TestQueryExContextCancelationCancelsQuery(t *testing.T) { func TestQueryRowExContextSuccess(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1314,7 +1315,7 @@ func TestQueryRowExContextSuccess(t *testing.T) { func TestQueryRowExContextErrorWhileReceivingRow(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1332,7 +1333,7 @@ func TestQueryRowExContextErrorWhileReceivingRow(t *testing.T) { func TestQueryRowExContextCancelationCancelsQuery(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) ctx, cancelFunc := context.WithCancel(context.Background()) @@ -1356,7 +1357,7 @@ func TestQueryRowExContextCancelationCancelsQuery(t *testing.T) { func TestConnQueryRowExSingleRoundTrip(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var result int32 @@ -1382,7 +1383,7 @@ func TestConnQueryRowExSingleRoundTrip(t *testing.T) { func TestConnSimpleProtocol(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) // Test all supported low-level types @@ -1579,7 +1580,7 @@ func TestConnSimpleProtocol(t *testing.T) { func TestConnSimpleProtocolRefusesNonUTF8ClientEncoding(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "set client_encoding to 'SQL_ASCII'") @@ -1601,7 +1602,7 @@ func TestConnSimpleProtocolRefusesNonUTF8ClientEncoding(t *testing.T) { func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) mustExec(t, conn, "set standard_conforming_strings to off") @@ -1623,7 +1624,7 @@ func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) { func TestQueryExCloseBefore(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) closeConn(t, conn) if _, err := conn.QueryEx(context.Background(), "select 1", nil); err == nil { diff --git a/replication_test.go b/replication_test.go index d06d73cd..0bd9a5d2 100644 --- a/replication_test.go +++ b/replication_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "context" "fmt" + "os" "reflect" "strconv" "strings" @@ -41,11 +42,12 @@ func getConfirmedFlushLsnFor(t *testing.T, conn *pgx.Conn, slot string) string { func TestSimpleReplicationConnection(t *testing.T) { var err error - if replicationConnConfig == nil { - t.Skip("Skipping due to undefined replicationConnConfig") + connString := os.Getenv("PGX_TEST_REPLICATION_CONN_STRING") + if connString == "" { + t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_REPLICATION_CONN_STRING") } - conn := mustConnect(t, *replicationConnConfig) + conn := mustConnectString(t, connString) defer func() { // Ensure replication slot is destroyed, but don't check for errors as it // should have already been destroyed. @@ -53,7 +55,8 @@ func TestSimpleReplicationConnection(t *testing.T) { closeConn(t, conn) }() - replicationConn := mustReplicationConnect(t, *replicationConnConfig) + replicationConnConfig := mustParseConfig(t, connString) + replicationConn := mustReplicationConnect(t, replicationConnConfig) defer closeReplicationConn(t, replicationConn) var cp string @@ -164,7 +167,7 @@ func TestSimpleReplicationConnection(t *testing.T) { closeReplicationConn(t, replicationConn) - replicationConn2 := mustReplicationConnect(t, *replicationConnConfig) + replicationConn2 := mustReplicationConnect(t, replicationConnConfig) defer closeReplicationConn(t, replicationConn2) err = replicationConn2.DropReplicationSlot("pgx_test") @@ -179,11 +182,13 @@ func TestSimpleReplicationConnection(t *testing.T) { } func TestReplicationConn_DropReplicationSlot(t *testing.T) { - if replicationConnConfig == nil { - t.Skip("Skipping due to undefined replicationConnConfig") + connString := os.Getenv("PGX_TEST_REPLICATION_CONN_STRING") + if connString == "" { + t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_REPLICATION_CONN_STRING") } - replicationConn := mustReplicationConnect(t, *replicationConnConfig) + replicationConnConfig := mustParseConfig(t, connString) + replicationConn := mustReplicationConnect(t, replicationConnConfig) defer closeReplicationConn(t, replicationConn) var cp string @@ -224,11 +229,13 @@ func TestReplicationConn_DropReplicationSlot(t *testing.T) { } func TestIdentifySystem(t *testing.T) { - if replicationConnConfig == nil { - t.Skip("Skipping due to undefined replicationConnConfig") + connString := os.Getenv("PGX_TEST_REPLICATION_CONN_STRING") + if connString == "" { + t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_REPLICATION_CONN_STRING") } - replicationConn2 := mustReplicationConnect(t, *replicationConnConfig) + replicationConnConfig := mustParseConfig(t, connString) + replicationConn2 := mustReplicationConnect(t, replicationConnConfig) defer closeReplicationConn(t, replicationConn2) r, err := replicationConn2.IdentifySystem() @@ -276,11 +283,13 @@ func getCurrentTimeline(t *testing.T, rc *pgx.ReplicationConn) int { } func TestGetTimelineHistory(t *testing.T) { - if replicationConnConfig == nil { - t.Skip("Skipping due to undefined replicationConnConfig") + connString := os.Getenv("PGX_TEST_REPLICATION_CONN_STRING") + if connString == "" { + t.Skipf("Skipping due to missing environment variable %v", "PGX_TEST_REPLICATION_CONN_STRING") } - replicationConn := mustReplicationConnect(t, *replicationConnConfig) + replicationConnConfig := mustParseConfig(t, connString) + replicationConn := mustReplicationConnect(t, replicationConnConfig) defer closeReplicationConn(t, replicationConn) timeline := getCurrentTimeline(t, replicationConn) diff --git a/travis/before_script.bash b/travis/before_script.bash index e9c0f865..bcf748a1 100755 --- a/travis/before_script.bash +++ b/travis/before_script.bash @@ -1,8 +1,6 @@ #!/usr/bin/env bash set -eux -mv conn_config_test.go.travis conn_config_test.go - if [ "${PGVERSION-}" != "" ] then # The tricky test user, below, has to actually exist so that it can be used in a test diff --git a/tx_test.go b/tx_test.go index 633f2177..133f1d3b 100644 --- a/tx_test.go +++ b/tx_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "context" "fmt" + "os" "testing" "time" @@ -15,7 +16,7 @@ import ( func TestTransactionSuccessfulCommit(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) createSql := ` @@ -57,7 +58,7 @@ func TestTransactionSuccessfulCommit(t *testing.T) { func TestTxCommitWhenTxBroken(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) createSql := ` @@ -149,7 +150,7 @@ func TestTxCommitSerializationFailure(t *testing.T) { func TestTransactionSuccessfulRollback(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) createSql := ` @@ -191,7 +192,7 @@ func TestTransactionSuccessfulRollback(t *testing.T) { func TestBeginExIsoLevels(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) isoLevels := []pgx.TxIsoLevel{pgx.Serializable, pgx.RepeatableRead, pgx.ReadCommitted, pgx.ReadUncommitted} @@ -217,7 +218,7 @@ func TestBeginExIsoLevels(t *testing.T) { func TestBeginExReadOnly(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tx, err := conn.BeginEx(context.Background(), &pgx.TxOptions{AccessMode: pgx.ReadOnly}) @@ -336,7 +337,7 @@ func TestTxCommitExCancel(t *testing.T) { func TestTxStatus(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tx, err := conn.Begin() @@ -360,7 +361,7 @@ func TestTxStatus(t *testing.T) { func TestTxStatusErrorInTransactions(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tx, err := conn.Begin() @@ -407,7 +408,7 @@ func TestTxStatusErrorInTransactions(t *testing.T) { func TestTxErr(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tx, err := conn.Begin() diff --git a/v4.md b/v4.md index 8cc8b752..65c1a2cc 100644 --- a/v4.md +++ b/v4.md @@ -35,6 +35,7 @@ Minor Potential Changes: ## Changes `pgconn.PgConn` now contains core PostgreSQL connection functionality. +Test configuration now done with environment variables instead of `.gitignore`'d locally modified `conn_config_test.go` file. ### Incompatible Changes diff --git a/values_test.go b/values_test.go index ddaf5468..fba38281 100644 --- a/values_test.go +++ b/values_test.go @@ -3,6 +3,7 @@ package pgx_test import ( "bytes" "net" + "os" "reflect" "testing" "time" @@ -13,7 +14,7 @@ import ( func TestDateTranscode(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) dates := []time.Time{ @@ -53,7 +54,7 @@ func TestDateTranscode(t *testing.T) { func TestTimestampTzTranscode(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) inputTime := time.Date(2013, 1, 2, 3, 4, 5, 6000, time.Local) @@ -74,7 +75,7 @@ func TestTimestampTzTranscode(t *testing.T) { func TestJSONAndJSONBTranscode(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) for _, typename := range []string{"json", "jsonb"} { @@ -229,7 +230,7 @@ func mustParseCIDR(t *testing.T, s string) *net.IPNet { func TestStringToNotTextTypeTranscode(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) input := "01086ee0-4963-4e35-9116-30c173a8d0bd" @@ -255,7 +256,7 @@ func TestStringToNotTextTypeTranscode(t *testing.T) { func TestInetCIDRTranscodeIPNet(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -304,7 +305,7 @@ func TestInetCIDRTranscodeIPNet(t *testing.T) { func TestInetCIDRTranscodeIP(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -364,7 +365,7 @@ func TestInetCIDRTranscodeIP(t *testing.T) { func TestInetCIDRArrayTranscodeIPNet(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -423,7 +424,7 @@ func TestInetCIDRArrayTranscodeIPNet(t *testing.T) { func TestInetCIDRArrayTranscodeIP(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -504,7 +505,7 @@ func TestInetCIDRArrayTranscodeIP(t *testing.T) { func TestInetCIDRTranscodeWithJustIP(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -546,7 +547,7 @@ func TestInetCIDRTranscodeWithJustIP(t *testing.T) { func TestArrayDecoding(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct { @@ -660,7 +661,7 @@ func TestArrayDecoding(t *testing.T) { func TestEmptyArrayDecoding(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) var val []string @@ -708,7 +709,7 @@ func TestEmptyArrayDecoding(t *testing.T) { func TestPointerPointer(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type allTypes struct { @@ -786,7 +787,7 @@ func TestPointerPointer(t *testing.T) { func TestPointerPointerNonZero(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) f := "foo" @@ -804,7 +805,7 @@ func TestPointerPointerNonZero(t *testing.T) { func TestEncodeTypeRename(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) type _int int @@ -908,7 +909,7 @@ func TestEncodeTypeRename(t *testing.T) { func TestRowDecode(t *testing.T) { t.Parallel() - conn := mustConnect(t, *defaultConnConfig) + conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE")) defer closeConn(t, conn) tests := []struct {