Skip to content

Commit

Permalink
move test code
Browse files Browse the repository at this point in the history
  • Loading branch information
krehermann committed Sep 26, 2023
1 parent cfb1c1b commit 832e137
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
7 changes: 2 additions & 5 deletions core/internal/testutils/evmtest/evmtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/smartcontractkit/sqlx"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/test-go/testify/assert"
"golang.org/x/exp/slices"
"gopkg.in/guregu/null.v4"

Expand Down Expand Up @@ -373,8 +372,6 @@ func (r *RawSub[T]) TrySend(t T) {
}

func NewScopedDB(t testing.TB, cfg config.Database) *sqlx.DB {
// hack to scope to evm schema. the value "evm" will need to be dynamic to support multiple relayers
url, err := pg.SchemaScopedConnection(cfg.URL(), "evm")
assert.NoError(t, err, "failed to create evm scoped db")
return pgtest.NewSqlxDB(t, pgtest.WithURL(*url))
u := pg.SchemaScopedConnection(cfg.URL(), "evm")
return pgtest.NewSqlxDB(t, pg.WithURL(u))
}
12 changes: 2 additions & 10 deletions core/internal/testutils/pgtest/pgtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func uniqueConnection(t testing.TB) *url.URL {
return url
}

func NewSqlxDB(t testing.TB, opts ...ConnectionOpt) *sqlx.DB {
func NewSqlxDB(t testing.TB, opts ...pg.ConnectionOpt) *sqlx.DB {
testutils.SkipShortDB(t)

url := uniqueConnection(t)
for _, opt := range opts {
opt(url)
assert.NoError(t, opt(url))
}
db, err := sqlx.Open(string(dialects.TransactionWrappedPostgres), url.String())
require.NoError(t, err)
Expand All @@ -55,14 +55,6 @@ func NewSqlxDB(t testing.TB, opts ...ConnectionOpt) *sqlx.DB {
return db
}

type ConnectionOpt func(*url.URL)

func WithURL(override url.URL) ConnectionOpt {
return func(u *url.URL) {
u = &override
}
}

func MustExec(t *testing.T, db *sqlx.DB, stmt string, args ...interface{}) {
require.NoError(t, utils.JustError(db.Exec(stmt, args...)))
}
Expand Down
6 changes: 0 additions & 6 deletions core/internal/testutils/pgtest/txdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ type txDriver struct {
conns map[string]map[string]*conn // url -> (uuid -> db) so we can close per url
}

func cleanseURL(u *url.URL) {
q := u.Query()
q.Del("uuid")
u.RawQuery = q.Encode()

}
func (d *txDriver) Open(connection string) (driver.Conn, error) {
d.Lock()
defer d.Unlock()
Expand Down
25 changes: 11 additions & 14 deletions core/services/pg/locked_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package pg

import (
"context"
"errors"
"fmt"
"net/url"
"time"

"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/smartcontractkit/sqlx"

Expand Down Expand Up @@ -71,7 +72,7 @@ func (l *lockedDb) Open(ctx context.Context, opts ...ConnectionOpt) (err error)
l.db, err = openDB(l.appID, l.cfg)
if err != nil {
// l.db will be nil in case of error
return errors.Wrap(err, "failed to open db")
return fmt.Errorf("failed to open db: %w", err)
}
revert := func() {
// Let Open() return the actual error, while l.Close() error is just logged.
Expand Down Expand Up @@ -99,7 +100,7 @@ func (l *lockedDb) Open(ctx context.Context, opts ...ConnectionOpt) (err error)
l.leaseLock = NewLeaseLock(l.db, l.appID, l.lggr, cfg)
if err = l.leaseLock.TakeAndHold(ctx); err != nil {
defer revert()
return errors.Wrap(err, "failed to take initial lease on database")
return fmt.Errorf("failed to take initial lease on database: %w", err)
}
}

Expand Down Expand Up @@ -145,7 +146,7 @@ func openDB(appID uuid.UUID, cfg LockedDBConfig, opts ...ConnectionOpt) (db *sql
static.SetConsumerName(&uri, "App", &appID)
dialect := cfg.Dialect()
for _, opt := range opts {
opt(&uri)
err = errors.Join(err, opt(&uri))
}
db, err = NewConnection(uri.String(), dialect, cfg)
return
Expand All @@ -156,32 +157,28 @@ type ConnectionOpt func(*url.URL) error
// WithSchema scopes the current url connection string to the given schema
func WithSchema(schema string) ConnectionOpt {
return func(url *url.URL) (err error) {
url, err = SchemaScopedConnection(*url, schema)
*url = SchemaScopedConnection(*url, schema)
return
}
}

// WithURL sets the url connection string
func WithURL(override string) ConnectionOpt {
func WithURL(override url.URL) ConnectionOpt {
return func(url *url.URL) (err error) {
url, err = url.Parse(override)
(*url) = override
return
}
}

func SchemaScopedConnection(conn url.URL, schema string) (*url.URL, error) {
func SchemaScopedConnection(conn url.URL, schema string) url.URL {
// documentation on this options is limited; find by searching the official postgres docs
// https://www.postgresql.org/docs/16/app-psql.html

//TODO real parsing for connection query string
u := conn
opt := "options=-csearch_path="
u, err := url.Parse(conn.String())
if err != nil {
return nil, err
}
queryVals := u.Query()
queryVals.Add(opt, schema)

u.RawQuery = queryVals.Encode()
return u, nil
return u
}
14 changes: 14 additions & 0 deletions core/services/pg/locked_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package pg_test

import (
"context"
"net/url"
"testing"
"time"

"github.com/test-go/testify/assert"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2"
"github.com/smartcontractkit/chainlink/v2/core/logger"
Expand Down Expand Up @@ -102,3 +105,14 @@ func TestOpenUnlockedDB(t *testing.T) {
require.NoError(t, db1.Close())
require.NoError(t, db2.Close())
}

func TestConnectionOpt(t *testing.T) {
t.Parallel()

url := new(url.URL)
override, err := url.Parse("postgres://postgres:password@localhost:5432/chainlink_dev_test?sslmode=disable")
require.NoError(t, err)
pg.WithURL(*override)(url)

assert.EqualValues(t, override, url)
}

0 comments on commit 832e137

Please sign in to comment.