secret-santa/internal/testhelpers/containers.go

35 lines
912 B
Go
Raw Normal View History

2024-11-17 23:35:59 +01:00
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) {
2024-11-24 11:29:35 +01:00
container, err := postgres.Run(ctx, "docker.io/postgres:16-alpine",
2024-11-17 23:35:59 +01:00
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
}