secret-santa/internal/testhelpers/containers.go

34 lines
886 B
Go

package testhelpers
import (
"context"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
type PostgresContainer struct {
*postgres.PostgresContainer
ConnectionString string
}
func CreatePostgresContainer(ctx context.Context) (*PostgresContainer, error) {
container, err := postgres.Run(ctx, "16",
postgres.WithUsername("test-user"),
postgres.WithPassword("test-password"),
postgres.WithDatabase("test-db"),
testcontainers.WithWaitStrategy(wait.ForLog("database system is ready to accept connections").WithOccurrence(2)),
)
if err != nil {
return nil, err
}
connString, err := container.ConnectionString(ctx)
if err != nil {
return nil, err
}
return &PostgresContainer{
PostgresContainer: container,
ConnectionString: connString,
}, nil
}