Skip to content

Commit

Permalink
chore: update e2e test to use pgxpool (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon authored May 7, 2024
1 parent 276b9b7 commit 5554a9f
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions e2e_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"time"

"cloud.google.com/go/cloudsqlconn"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"

Expand Down Expand Up @@ -59,7 +59,7 @@ func requirePostgresVars(t *testing.T) {
}
}

func TestPostgresPgxConnect(t *testing.T) {
func TestPostgresPgxPoolConnect(t *testing.T) {
if testing.Short() {
t.Skip("skipping Postgres integration tests")
}
Expand All @@ -71,25 +71,26 @@ func TestPostgresPgxConnect(t *testing.T) {
if err != nil {
t.Fatalf("failed to init Dialer: %v", err)
}
defer d.Close()

dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
config, err := pgx.ParseConfig(dsn)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}

config.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, postgresConnName)
}

conn, connErr := pgx.ConnectConfig(ctx, config)
if connErr != nil {
t.Fatalf("failed to connect: %s", connErr)
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
defer conn.Close(ctx)
defer pool.Close()

var now time.Time
err = conn.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
if err != nil {
t.Fatalf("QueryRow failed: %s", err)
}
Expand All @@ -106,7 +107,7 @@ func TestPostgresConnectWithIAMUser(t *testing.T) {

// password is intentionally blank
dsn := fmt.Sprintf("user=%s password=\"\" dbname=%s sslmode=disable", postgresUserIAM, postgresDB)
config, err := pgx.ParseConfig(dsn)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}
Expand All @@ -115,18 +116,19 @@ func TestPostgresConnectWithIAMUser(t *testing.T) {
t.Fatalf("failed to initiate Dialer: %v", err)
}
defer d.Close()
config.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {

config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, postgresConnName)
}

conn, connErr := pgx.ConnectConfig(ctx, config)
if connErr != nil {
t.Fatalf("failed to connect: %s", connErr)
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
defer conn.Close(ctx)
defer pool.Close()

var now time.Time
err = conn.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
if err != nil {
t.Fatalf("QueryRow failed: %s", err)
}
Expand All @@ -143,7 +145,7 @@ func TestPostgresConnectWithLazyRefresh(t *testing.T) {

// password is intentionally blank
dsn := fmt.Sprintf("user=%s password=\"\" dbname=%s sslmode=disable", postgresUserIAM, postgresDB)
config, err := pgx.ParseConfig(dsn)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}
Expand All @@ -156,18 +158,18 @@ func TestPostgresConnectWithLazyRefresh(t *testing.T) {
t.Fatalf("failed to initiate Dialer: %v", err)
}
defer d.Close()
config.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, postgresConnName)
}

conn, connErr := pgx.ConnectConfig(ctx, config)
if connErr != nil {
t.Fatalf("failed to connect: %s", connErr)
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
defer conn.Close(ctx)
defer pool.Close()

var now time.Time
err = conn.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
if err != nil {
t.Fatalf("QueryRow failed: %s", err)
}
Expand Down Expand Up @@ -360,25 +362,26 @@ func TestPostgresAuthentication(t *testing.T) {
if err != nil {
t.Fatalf("failed to init Dialer: %v", err)
}
defer d.Close()

dsn := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", postgresUser, postgresPass, postgresDB)
config, err := pgx.ParseConfig(dsn)
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
t.Fatalf("failed to parse pgx config: %v", err)
}

config.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
config.ConnConfig.DialFunc = func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return d.Dial(ctx, postgresConnName)
}

conn, connErr := pgx.ConnectConfig(ctx, config)
if connErr != nil {
t.Fatalf("failed to connect: %s", connErr)
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
t.Fatalf("failed to create pool: %s", err)
}
defer conn.Close(ctx)
defer pool.Close()

var now time.Time
err = conn.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
err = pool.QueryRow(context.Background(), "SELECT NOW()").Scan(&now)
if err != nil {
t.Fatalf("QueryRow failed: %s", err)
}
Expand Down

0 comments on commit 5554a9f

Please sign in to comment.