powered by ory/dockertest
.
Go package providing lifecycle management for PostgreSQL Docker instances.
Here is an article expanding on the usage of this package.
Leverage Docker to run unit and integration tests against a real PostgreSQL database.
package foo_test
import (
"testing"
"github.com/adrianbrad/psqldocker"
)
func TestXxx(t *testing.T) {
const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"
c, err := psqldocker.NewContainer(
"user",
"password",
"dbName",
psqldocker.WithContainerName("test"),
// initialize with a schema
psqldocker.WithSql(schema),
// you can add other options here
)
if err != nil {
t.Fatalf("cannot start new psql container: %s\n", err)
}
t.Cleanup(func() {
err = c.Close()
if err != nil {
t.Logf("err while closing conainter: %w", err)
}
})
t.Run("Subtest", func(t *testing.T) {
// execute your test logic here
})
}
package foo_test
import (
"log"
"testing"
"github.com/adrianbrad/psqldocker"
)
func TestMain(m *testing.M) {
const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"
c, err := psqldocker.NewContainer(
"user",
"password",
"dbName",
psqldocker.WithContainerName("test"),
// initialize with a schema
psqldocker.WithSql(schema),
// you can add other options here
)
if err != nil {
log.Fatalf("cannot start new psql container: %s\n", err)
}
defer func() {
err = c.Close()
if err != nil {
log.Printf("err while closing conainter: %w", err)
}
}()
m.Run()
}