Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Options for a `*sql.DB` are chained after `With()`:
db := s.With().ReadOnly().Timeout(5 * time.Second).DB()
```

`Connector` is given the test the session was opened for and the connector pgsandpit made for the `*sql.DB`, and returns the connector to open it with, as `sql.OpenDB` takes one. For example, a connector that wraps it can hook into the queries:
`Connector` is given the connector pgsandpit made for the `*sql.DB` and returns the connector to open it with, as `sql.OpenDB` takes one. For example, a connector that wraps it can hook into the queries:

```go
db := s.With().Connector(func(t *testing.T, c driver.Connector) driver.Connector {
db := s.With().Connector(func(c driver.Connector) driver.Connector {
return &MyConnector{c}
}).DB()
```
Expand Down
6 changes: 3 additions & 3 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestConnector(t *testing.T) {
t.Run("wrap the connector to hook into queries", func(t *testing.T) {
s := sandpit.Session(t)
hook := &hookConnector{}
db := s.With().Connector(func(_ *testing.T, c driver.Connector) driver.Connector {
db := s.With().Connector(func(c driver.Connector) driver.Connector {
hook.Connector = c
return hook
}).DB()
Expand All @@ -80,7 +80,7 @@ func TestConnector(t *testing.T) {

cfg, err := pgx.ParseConfig(dsn())
require.NoError(t, err)
db := s.With().Connector(func(*testing.T, driver.Connector) driver.Connector {
db := s.With().Connector(func(driver.Connector) driver.Connector {
return stdlib.GetConnector(*cfg)
}).DB()

Expand All @@ -92,7 +92,7 @@ func TestConnector(t *testing.T) {
s := sandpit.Session(t)
b := s.With()
called := false
_ = b.Connector(func(_ *testing.T, c driver.Connector) driver.Connector {
_ = b.Connector(func(c driver.Connector) driver.Connector {
called = true
return c
})
Expand Down
11 changes: 5 additions & 6 deletions db_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type DBBuilder struct {
s *Session
readOnly bool
timeout time.Duration
connector func(*testing.T, driver.Connector) driver.Connector
connector func(driver.Connector) driver.Connector
conn func(*testing.T, *pgx.Conn)
}

Expand All @@ -44,9 +44,8 @@ func (b *DBBuilder) Timeout(d time.Duration) *DBBuilder {
// Connector sets a function that is given the connector pgsandpit made for the
// *sql.DB and returns the connector the *sql.DB is opened with, as by
// sql.OpenDB. If the session was built with SessionBuilder.Connector, f is
// given the connector that function returned. t is the test the session was
// opened for.
func (b *DBBuilder) Connector(f func(t *testing.T, c driver.Connector) driver.Connector) *DBBuilder {
// given the connector that function returned.
func (b *DBBuilder) Connector(f func(driver.Connector) driver.Connector) *DBBuilder {
c := *b
c.connector = f
return &c
Expand Down Expand Up @@ -75,10 +74,10 @@ func (b *DBBuilder) DB() *sql.DB {
}
var c driver.Connector = &connector{b: *b}
if b.s.connector != nil {
c = b.s.connector(b.s.t, c)
c = b.s.connector(c)
}
if b.connector != nil {
c = b.connector(b.s.t, c)
c = b.connector(c)
}
db := sql.OpenDB(c)
// Registered after the session's cleanup, so it runs before it.
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Session struct {
// opened through, and whether statements interleave with transactions.
readOnly bool
timeout time.Duration
connector func(*testing.T, driver.Connector) driver.Connector
connector func(driver.Connector) driver.Connector
interleave bool

// sem is held while a statement runs. Without interleave, an application
Expand Down
8 changes: 4 additions & 4 deletions session_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type SessionBuilder struct {
sp *Sandpit
readOnly bool
timeout time.Duration
connector func(*testing.T, driver.Connector) driver.Connector
connector func(driver.Connector) driver.Connector
conn func(*testing.T, *pgx.Conn)
cache bool
interleave bool
Expand All @@ -50,9 +50,9 @@ func (b *SessionBuilder) Timeout(d time.Duration) *SessionBuilder {

// Connector sets a function that is given the connector pgsandpit made for each
// *sql.DB of the session and returns the connector the *sql.DB is opened with.
// It is called each time a *sql.DB is made, with the test the session was opened
// for as t. A function set with DBBuilder.Connector is given what f returns.
func (b *SessionBuilder) Connector(f func(t *testing.T, c driver.Connector) driver.Connector) *SessionBuilder {
// It is called each time a *sql.DB is made. A function set with
// DBBuilder.Connector is given what f returns.
func (b *SessionBuilder) Connector(f func(driver.Connector) driver.Connector) *SessionBuilder {
c := *b
c.connector = f
return &c
Expand Down
23 changes: 9 additions & 14 deletions session_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestSessionBuilderConnector(t *testing.T) {

t.Run("is applied to every *sql.DB", func(t *testing.T) {
var hooks []*hookConnector
s := sandpit.With().Connector(func(_ *testing.T, c driver.Connector) driver.Connector {
s := sandpit.With().Connector(func(c driver.Connector) driver.Connector {
h := &hookConnector{Connector: c}
hooks = append(hooks, h)
return h
Expand All @@ -113,13 +113,13 @@ func TestSessionBuilderConnector(t *testing.T) {

t.Run("is wrapped by DBBuilder.Connector", func(t *testing.T) {
sessionHook := &hookConnector{}
s := sandpit.With().Connector(func(_ *testing.T, c driver.Connector) driver.Connector {
s := sandpit.With().Connector(func(c driver.Connector) driver.Connector {
sessionHook.Connector = c
return sessionHook
}).Session(t)

dbHook := &hookConnector{}
_, err := s.With().Connector(func(t *testing.T, c driver.Connector) driver.Connector {
_, err := s.With().Connector(func(c driver.Connector) driver.Connector {
// Given what the session's function returned.
assert.Same(t, sessionHook, c)
dbHook.Connector = c
Expand All @@ -134,7 +134,7 @@ func TestSessionBuilderConnector(t *testing.T) {
t.Run("does not change the builder", func(t *testing.T) {
b := sandpit.With()
called := false
_ = b.Connector(func(_ *testing.T, c driver.Connector) driver.Connector {
_ = b.Connector(func(c driver.Connector) driver.Connector {
called = true
return c
})
Expand Down Expand Up @@ -187,24 +187,19 @@ func TestSessionBuilderConn(t *testing.T) {
})
}

func TestBuilderFuncsAreGivenTheSessionsTest(t *testing.T) {
func TestBuilderConnIsGivenTheSessionsTest(t *testing.T) {
t.Parallel()

var got []*testing.T
conn := func(t *testing.T, _ *pgx.Conn) { got = append(got, t) }
connector := func(t *testing.T, c driver.Connector) driver.Connector {
got = append(got, t)
return c
}

s := sandpit.With().Conn(conn).Connector(connector).Session(t)
s := sandpit.With().Conn(conn).Session(t)
t.Run("from a subtest", func(*testing.T) {
s.With().Conn(conn).Connector(connector).DB()
s.With().Conn(conn).DB()
})

// SessionBuilder.Conn, DBBuilder.Conn, SessionBuilder.Connector and
// DBBuilder.Connector.
require.Len(t, got, 4)
// SessionBuilder.Conn and DBBuilder.Conn.
require.Len(t, got, 2)
for _, g := range got {
assert.Same(t, t, g)
}
Expand Down
Loading