Files
query-orchestration/internal/test/database.go
T
Michael McGuinness 752fb2e2c0 Merged in feature/chc_1 (pull request #120)
Text Extraction Clean Up + Parallelization

* merged_Call

* directchildren

* bitofimprovements

* go

* parallel

* fixfullsuite

* pondforconcurrency

* comments

* muchdone

* bitsofclean

* stabilisedtests

* snappy

* threadpooltests

* childelements

* testspassed
2025-04-25 17:02:50 +00:00

85 lines
2.2 KiB
Go

package test
import (
"context"
"fmt"
"strconv"
"testing"
db "queryorchestration/internal/database"
"queryorchestration/internal/serviceconfig"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
type CreateDatabaseConfig struct {
NoMigrations bool
}
const (
dbAlias = "postgres"
dbPort = 5432
)
func CreateDB(t testing.TB, ctx context.Context, cfg serviceconfig.ConfigProvider, network string, dcfg *CreateDatabaseConfig) testcontainers.Container {
port, err := nat.NewPort("tcp", strconv.Itoa(dbPort))
require.NoError(t, err)
cfg.SetDBUser("pass")
cfg.SetDBSecret("postgres")
cfg.SetDBName(NormaliseAlias(fmt.Sprintf("queryorchestration_%s", t.Name())))
cfg.SetDBNoSSL(true)
req := testcontainers.ContainerRequest{
Image: "postgres:17.2-alpine3.21",
Name: "postgres_test_queryorchestration",
Env: map[string]string{
"POSTGRES_USER": cfg.GetDBUser(),
"POSTGRES_PASSWORD": cfg.GetDBSecret(),
},
Cmd: []string{"postgres", "-c", "max_connections=1000"},
ExposedPorts: []string{port.Port()},
WaitingFor: wait.ForAll(
wait.ForExposedPort(),
wait.ForListeningPort(port),
wait.ForLog("database system is ready to accept connections"),
wait.ForSQL(port, "postgres", func(host string, port nat.Port) string {
return fmt.Sprintf("postgres://%s:%s@%s:%d/postgres?sslmode=disable",
cfg.GetDBUser(), cfg.GetDBSecret(), host, port.Int())
}),
),
Networks: []string{network},
NetworkAliases: map[string][]string{
network: {dbAlias},
},
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Reuse: true,
})
require.NoError(t, err)
host, err := container.Host(ctx)
require.NoError(t, err)
mappedPort, err := container.MappedPort(ctx, port)
require.NoError(t, err)
cfg.SetDBHost(host)
cfg.SetDBPort(mappedPort.Int())
if !dcfg.NoMigrations {
err := db.RunMigrations(ctx, cfg)
require.NoError(t, err)
err = cfg.SetDBPool(ctx)
require.NoError(t, err)
}
return container
}