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
34 changes: 34 additions & 0 deletions example_helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package testdb_test

import (
"context"
"fmt"
"testing"
)

// exampleT stands in for the *testing.T a runnable example does not have;
// see postgres/example_helpers_test.go for the full story. The examples
// here close their databases by hand, but a real stand-in keeps them honest
// about what they call on t.
type exampleT struct {
testing.TB
cleanups []func()
}

func newExampleT() *exampleT { return &exampleT{} }

func (t *exampleT) Helper() {}
func (t *exampleT) Cleanup(f func()) { t.cleanups = append(t.cleanups, f) }
func (t *exampleT) Context() context.Context { return context.Background() }
func (t *exampleT) Failed() bool { return false }
func (t *exampleT) Logf(string, ...any) {}

func (t *exampleT) Errorf(format string, args ...any) { fmt.Printf("error: "+format+"\n", args...) }
func (t *exampleT) Fatalf(format string, args ...any) { panic(fmt.Sprintf(format, args...)) }
func (t *exampleT) Fatal(args ...any) { panic(fmt.Sprint(args...)) }

func (t *exampleT) done() {
for i := len(t.cleanups) - 1; i >= 0; i-- {
t.cleanups[i]()
}
}
10 changes: 6 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testdb_test
import (
"context"
"fmt"
"testing"

"github.com/bashhack/testdb"
"github.com/bashhack/testdb/postgres"
Expand All @@ -12,7 +11,8 @@ import (
// Example_new demonstrates using testdb.New with a custom initializer
// for full control over database initialization.
func Example_new() {
t := &testing.T{}
t := newExampleT()
defer t.done()

provider := &postgres.PostgresProvider{}
initializer := &postgres.PoolInitializer{}
Expand Down Expand Up @@ -48,7 +48,8 @@ func Example_new() {
// Example_dsnOnly demonstrates using testdb to get just a DSN
// without automatic connection initialization.
func Example_dsnOnly() {
t := &testing.T{}
t := newExampleT()
defer t.done()

provider := &postgres.PostgresProvider{}

Expand Down Expand Up @@ -78,7 +79,8 @@ func Example_dsnOnly() {
// Example_customInitializer shows implementing a custom initializer
// for integration with ORMs or custom database types.
func Example_customInitializer() {
t := &testing.T{}
t := newExampleT()
defer t.done()

provider := &postgres.PostgresProvider{}
initializer := &exampleInitializer{}
Expand Down
Loading
Loading