// ©AngelaMos | 2026 // postgres.go package testutil import ( "context" "database/sql" "testing" "time" _ "github.com/jackc/pgx/v5/stdlib" "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" "github.com/testcontainers/testcontainers-go/wait" "github.com/CarterPerez-dev/cybersecurity-projects/canary-token-generator/backend/internal/core" ) func NewTestDB(t *testing.T) *sql.DB { t.Helper() ctx := context.Background() pgContainer, err := tcpostgres.Run(ctx, "postgres:18-alpine", tcpostgres.WithDatabase("canary_test"), tcpostgres.WithUsername("test"), tcpostgres.WithPassword("test"), testcontainers.WithWaitStrategy( wait.ForLog("database system is ready to accept connections"). WithOccurrence(2). WithStartupTimeout(60*time.Second), ), ) require.NoError(t, err) t.Cleanup(func() { _ = pgContainer.Terminate(context.Background()) }) connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") require.NoError(t, err) db, err := sql.Open("pgx", connStr) require.NoError(t, err) t.Cleanup(func() { _ = db.Close() }) require.NoError(t, db.Ping()) require.NoError(t, core.RunMigrations(db)) return db }