Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func New(u *url.URL) (waitfor.Resource, error) {
}

func (s *Postgres) Test(ctx context.Context) error {
db, err := sql.Open(s.url.Scheme, strings.TrimPrefix(s.url.String(), Scheme+"://"))
// Always use "postgres" as the driver name for sql.Open, regardless of the URL scheme
// Remove the scheme and "://" from the URL to get the connection string
connStr := strings.TrimPrefix(s.url.String(), s.url.Scheme+"://")

db, err := sql.Open(Scheme, connStr)

if err != nil {
return err
Expand Down
72 changes: 72 additions & 0 deletions postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/go-waitfor/waitfor"
"github.com/go-waitfor/waitfor-postgres"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
"time"
)
Expand All @@ -19,3 +21,73 @@ func TestUse(t *testing.T) {

assert.Error(t, err)
}

func TestNew(t *testing.T) {
t.Run("with valid URL", func(t *testing.T) {
u, err := url.Parse("postgres://localhost:5432/testdb")
require.NoError(t, err)

resource, err := postgres.New(u)
assert.NoError(t, err)
assert.NotNil(t, resource)
})

t.Run("with nil URL", func(t *testing.T) {
resource, err := postgres.New(nil)
assert.Error(t, err)
assert.Nil(t, resource)
assert.Contains(t, err.Error(), "url")
assert.Contains(t, err.Error(), "invalid argument")
})
}

func TestPostgres_Test(t *testing.T) {
t.Run("with invalid postgres URL", func(t *testing.T) {
u, err := url.Parse("postgres://nonexistent:5432/testdb")
require.NoError(t, err)

resource, err := postgres.New(u)
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

err = resource.Test(ctx)
assert.Error(t, err)
})

t.Run("with URL that causes connection failure", func(t *testing.T) {
// Use a valid URL format but with invalid host
u, err := url.Parse("postgres://user:password@invalid-host:5432/database")
require.NoError(t, err)

resource, err := postgres.New(u)
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

err = resource.Test(ctx)
assert.Error(t, err)
})

t.Run("with postgresql scheme (alternate scheme bug test)", func(t *testing.T) {
// This tests a potential bug where using 'postgresql://' scheme
// instead of 'postgres://' could cause sql.Open to fail
u, err := url.Parse("postgresql://user:password@localhost:5432/database")
require.NoError(t, err)

resource, err := postgres.New(u)
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

// This should work (or at least fail with connection error, not driver error)
err = resource.Test(ctx)
// We expect a connection error, not a driver registration error
assert.Error(t, err)
// The error should not be about unknown driver
assert.NotContains(t, err.Error(), "unknown driver")
})
}