44 lines
1016 B
Go
44 lines
1016 B
Go
//go:build integration
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// openTestStore returns a Store migrated into a fresh schema that is dropped
|
|
// when the test ends, so tests never see each other's rows.
|
|
func openTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
url := os.Getenv("DATABASE_URL")
|
|
if url == "" {
|
|
t.Skip("DATABASE_URL not set")
|
|
}
|
|
ctx := context.Background()
|
|
admin, err := pgx.Connect(ctx, url)
|
|
require.NoError(t, err)
|
|
schema := fmt.Sprintf("test_%d", time.Now().UnixNano())
|
|
_, err = admin.Exec(ctx, "CREATE SCHEMA "+schema)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() {
|
|
_, _ = admin.Exec(ctx, "DROP SCHEMA "+schema+" CASCADE")
|
|
_ = admin.Close(ctx)
|
|
})
|
|
|
|
cfg, err := pgxpool.ParseConfig(url)
|
|
require.NoError(t, err)
|
|
cfg.ConnConfig.RuntimeParams["search_path"] = schema
|
|
s, err := openWithConfig(ctx, cfg)
|
|
require.NoError(t, err)
|
|
t.Cleanup(s.Close)
|
|
return s
|
|
}
|