This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/test/helper.go
SoXX b5437ecc62
All checks were successful
Gitea Build Check / Build (push) Successful in 10m55s
tests: fixed tests
Signed-off-by: SoXX <soxx@fenpa.ws>
2024-06-28 14:16:28 +02:00

124 lines
2.8 KiB
Go

package test
import (
"context"
"database/sql"
"net/url"
"strconv"
"strings"
"time"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
migrate "github.com/rubenv/sql-migrate"
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
databaseName = "anthrove"
databaseUser = "anthrove"
databasePassword = "anthrove"
migrationSource = "../../pkg/database/migrations/"
)
func StartPostgresContainer(ctx context.Context) (*postgrescontainer.PostgresContainer, *gorm.DB, error) {
pgContainer, err := postgrescontainer.RunContainer(ctx,
testcontainers.WithImage("postgres:alpine"),
postgrescontainer.WithDatabase(databaseName),
postgrescontainer.WithUsername(databaseUser),
postgrescontainer.WithPassword(databasePassword),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).WithStartupTimeout(60*time.Second)),
)
if err != nil {
return nil, nil, err
}
connectionString, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
if err != nil {
return nil, nil, err
}
err = migrateDatabase(connectionString)
if err != nil {
return nil, nil, err
}
gormDB, err := getGormDB(connectionString)
if err != nil {
return nil, nil, err
}
return pgContainer, gormDB, nil
}
func migrateDatabase(connectionString string) error {
db, err := sql.Open("postgres", connectionString)
if err != nil {
return err
}
migrations := &migrate.FileMigrationSource{
Dir: migrationSource,
}
_, err = migrate.Exec(db, "postgres", migrations, migrate.Up)
if err != nil {
return err
}
return nil
}
func getGormDB(connectionString string) (*gorm.DB, error) {
return gorm.Open(postgres.Open(connectionString), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
}
func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgrescontainer.PostgresContainer) (*models.DatabaseConfig, error) {
var err error
connectionString, err := pgContainer.ConnectionString(ctx)
if err != nil {
return nil, err
}
connectionStringUrl, err := url.Parse(connectionString)
if err != nil {
return nil, err
}
split := strings.Split(connectionStringUrl.Host, ":")
host := split[0]
port, err := strconv.Atoi(split[1])
if err != nil {
return nil, err
}
database := strings.TrimPrefix(connectionStringUrl.Path, "/")
username := connectionStringUrl.User.Username()
password, _ := connectionStringUrl.User.Password()
return &models.DatabaseConfig{
Endpoint: host,
Username: username,
Password: password,
Database: database,
Port: port,
SSL: false,
Timezone: "Europe/Berlin",
Debug: true,
}, nil
}