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