Add default connect_timeout option to connection string (#626)

* Add default connect_timeout option to connection string
* Add an extra test
This commit is contained in:
Dan Sosedoff
2022-12-20 16:58:54 -06:00
committed by GitHub
parent b31e7f1ea7
commit bcba666507
3 changed files with 43 additions and 0 deletions

View File

@@ -173,6 +173,28 @@ func TestBuildStringFromOptions(t *testing.T) {
assert.Equal(t, "postgres://foobar:password2@127.0.0.1:5432/foobar2?sslmode=disable", str)
})
t.Run("with connection timeout", func(t *testing.T) {
opts := command.Options{
URL: "postgres://user:pass@localhost:5432/dbname",
OpenTimeout: 30,
}
str, err := BuildStringFromOptions(opts)
assert.NoError(t, err)
assert.Equal(t, "postgres://user:pass@localhost:5432/dbname?connect_timeout=30&sslmode=disable", str)
opts = command.Options{
Host: "localhost",
Port: 5432,
User: "username",
DbName: "dbname",
OpenTimeout: 30,
}
str, err = BuildStringFromOptions(opts)
assert.NoError(t, err)
assert.Equal(t, "postgres://username:@localhost:5432/dbname?connect_timeout=30&sslmode=disable", str)
})
t.Run("invalid url", func(t *testing.T) {
opts := command.Options{}
examples := []string{
@@ -231,6 +253,14 @@ func TestFormatURL(t *testing.T) {
},
result: "postgres://username:password@localhost:5432/dbname?sslmode=disable",
},
{
name: "with timeout setting",
input: command.Options{
URL: "postgres://username@localhost:5432/dbname",
OpenTimeout: 30,
},
result: "postgres://username@localhost:5432/dbname?connect_timeout=30&sslmode=disable",
},
}
for _, ex := range examples {