Skip to content

Instantly share code, notes, and snippets.

@Eun
Last active February 21, 2024 10:53
Show Gist options
  • Save Eun/221e9cb9190b56578dbb752e5e2a6b2a to your computer and use it in GitHub Desktop.
Save Eun/221e9cb9190b56578dbb752e5e2a6b2a to your computer and use it in GitHub Desktop.
testcontainers-go with postgres
// usage:
// testDB := testhelpers.NewTestDatabase(t)
// defer testDB.Close(t)
// println(testDB.ConnectionString(t))
package testhelpers
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type TestDatabase struct {
instance testcontainers.Container
}
func NewTestDatabase(t *testing.T) *TestDatabase {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
req := testcontainers.ContainerRequest{
Image: "postgres:12",
ExposedPorts: []string{"5432/tcp"},
AutoRemove: true,
Env: map[string]string{
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "postgres",
"POSTGRES_DB": "postgres",
},
WaitingFor: wait.ForListeningPort("5432/tcp"),
}
postgres, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
return &TestDatabase{
instance: postgres,
}
}
func (db *TestDatabase) Port(t *testing.T) int {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
p, err := db.instance.MappedPort(ctx, "5432")
require.NoError(t, err)
return p.Int()
}
func (db *TestDatabase) ConnectionString(t *testing.T) string {
return fmt.Sprintf("postgres://postgres:postgres@127.0.0.1:%d/postgres", db.Port(t))
}
func (db *TestDatabase) Close(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
require.NoError(t, db.instance.Terminate(ctx))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment