diff --git a/README.md b/README.md index cf9acc1..e36680a 100644 --- a/README.md +++ b/README.md @@ -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() ``` diff --git a/connector_test.go b/connector_test.go index e0aa28f..27157a4 100644 --- a/connector_test.go +++ b/connector_test.go @@ -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() @@ -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() @@ -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 }) diff --git a/db_builder.go b/db_builder.go index 1274724..2d8fc20 100644 --- a/db_builder.go +++ b/db_builder.go @@ -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) } @@ -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 @@ -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. diff --git a/session.go b/session.go index dd79c84..305d901 100644 --- a/session.go +++ b/session.go @@ -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 diff --git a/session_builder.go b/session_builder.go index b3cb5b0..7b9bef1 100644 --- a/session_builder.go +++ b/session_builder.go @@ -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 @@ -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 diff --git a/session_builder_test.go b/session_builder_test.go index f923aec..6879328 100644 --- a/session_builder_test.go +++ b/session_builder_test.go @@ -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 @@ -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 @@ -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 }) @@ -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) }