Skip to content

Commit a15c855

Browse files
authored
Merge pull request #2 from go-waitfor/copilot/fix-8454219b-2e64-44ba-8879-59366f15b715
Improve test coverage to 90% and fix URL scheme driver bug
2 parents a0e2e66 + 24a090f commit a15c855

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

postgres.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ func New(u *url.URL) (waitfor.Resource, error) {
3434
}
3535

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

3943
if err != nil {
4044
return err

postgres_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/go-waitfor/waitfor"
66
"github.com/go-waitfor/waitfor-postgres"
77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"net/url"
810
"testing"
911
"time"
1012
)
@@ -19,3 +21,73 @@ func TestUse(t *testing.T) {
1921

2022
assert.Error(t, err)
2123
}
24+
25+
func TestNew(t *testing.T) {
26+
t.Run("with valid URL", func(t *testing.T) {
27+
u, err := url.Parse("postgres://localhost:5432/testdb")
28+
require.NoError(t, err)
29+
30+
resource, err := postgres.New(u)
31+
assert.NoError(t, err)
32+
assert.NotNil(t, resource)
33+
})
34+
35+
t.Run("with nil URL", func(t *testing.T) {
36+
resource, err := postgres.New(nil)
37+
assert.Error(t, err)
38+
assert.Nil(t, resource)
39+
assert.Contains(t, err.Error(), "url")
40+
assert.Contains(t, err.Error(), "invalid argument")
41+
})
42+
}
43+
44+
func TestPostgres_Test(t *testing.T) {
45+
t.Run("with invalid postgres URL", func(t *testing.T) {
46+
u, err := url.Parse("postgres://nonexistent:5432/testdb")
47+
require.NoError(t, err)
48+
49+
resource, err := postgres.New(u)
50+
require.NoError(t, err)
51+
52+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
53+
defer cancel()
54+
55+
err = resource.Test(ctx)
56+
assert.Error(t, err)
57+
})
58+
59+
t.Run("with URL that causes connection failure", func(t *testing.T) {
60+
// Use a valid URL format but with invalid host
61+
u, err := url.Parse("postgres://user:password@invalid-host:5432/database")
62+
require.NoError(t, err)
63+
64+
resource, err := postgres.New(u)
65+
require.NoError(t, err)
66+
67+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
68+
defer cancel()
69+
70+
err = resource.Test(ctx)
71+
assert.Error(t, err)
72+
})
73+
74+
t.Run("with postgresql scheme (alternate scheme bug test)", func(t *testing.T) {
75+
// This tests a potential bug where using 'postgresql://' scheme
76+
// instead of 'postgres://' could cause sql.Open to fail
77+
u, err := url.Parse("postgresql://user:password@localhost:5432/database")
78+
require.NoError(t, err)
79+
80+
resource, err := postgres.New(u)
81+
require.NoError(t, err)
82+
83+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
84+
defer cancel()
85+
86+
// This should work (or at least fail with connection error, not driver error)
87+
err = resource.Test(ctx)
88+
// We expect a connection error, not a driver registration error
89+
assert.Error(t, err)
90+
// The error should not be about unknown driver
91+
assert.NotContains(t, err.Error(), "unknown driver")
92+
})
93+
}

0 commit comments

Comments
 (0)