Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

1.3 KB
Raw Blame
package testdb

import (
	"context"
	"net/url"
	"os"
	"testing"
	"time"

	"github.com/jackc/pgx/v5/pgxpool"
)

func TestCreateOverridesDatabaseQuery(t *testing.T) {
	dsn := os.Getenv("SECRET_TEST_DATABASE_URL")
	if dsn == "" {
		t.Skip("SECRET_TEST_DATABASE_URL is not set")
	}
	u, err := url.Parse(dsn)
	if err != nil {
		t.Fatal(err)
	}
	q := u.Query()
	q.Set("dbname", "postgres")
	u.RawQuery = q.Encode()
	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
	defer cancel()
	admin, err := pgxpool.New(ctx, u.String())
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(admin.Close)
	name, result := Create(t, ctx, admin, u.String(), false)
	cfg, err := pgxpool.ParseConfig(result)
	if err != nil {
		t.Fatal(err)
	}
	if cfg.ConnConfig.Database != name {
		t.Fatalf("database = %q, want %q", cfg.ConnConfig.Database, name)
	}
}

func TestDatabaseURLOverridesQuery(t *testing.T) {
	got, err := databaseURL("postgres://localhost/old?dbname=postgres&database=postgres&sslmode=disable", "isolated")
	if err != nil {
		t.Fatal(err)
	}
	u, err := url.Parse(got)
	if err != nil {
		t.Fatal(err)
	}
	if u.Path != "/isolated" || u.Query().Has("dbname") || u.Query().Has("database") || u.Query().Get("sslmode") != "disable" {
		t.Fatalf("unexpected test database URL: %q", got)
	}
}