| 265 | } |
| 266 | |
| 267 | func TestOptions_ToURL(t *testing.T) { |
| 268 | tests := []struct { |
| 269 | name string |
| 270 | opts *Options |
| 271 | expected string |
| 272 | }{ |
| 273 | {"Empty", &Options{Database: "postgres"}, "postgres://localhost:5432/postgres?sslmode=disable"}, |
| 274 | {"User", &Options{Database: "postgres", User: "postgres"}, "postgres://postgres@localhost:5432/postgres?sslmode=disable"}, |
| 275 | {"UserPass", &Options{Database: "postgres", User: "postgres", Password: "password"}, "postgres://postgres:password@localhost:5432/postgres?sslmode=disable"}, |
| 276 | {"UserPassAddr", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234"}, "postgres://postgres:password@somewhere:1234/postgres?sslmode=disable"}, |
| 277 | {"UserPassAddrAppl", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234", ApplicationName: "test"}, "postgres://postgres:password@somewhere:1234/postgres?application_name=test&sslmode=disable"}, |
| 278 | {"UserPassAddrApplTimeout", &Options{Database: "postgres", User: "postgres", Password: "password", Addr: "somewhere:1234", ApplicationName: "test", DialTimeout: time.Second}, "postgres://postgres:password@somewhere:1234/postgres?application_name=test&connect_timeout=1&sslmode=disable"}, |
| 279 | } |
| 280 | for _, tt := range tests { |
| 281 | t.Run(tt.name, func(t *testing.T) { |
| 282 | assert := assert.New(t) |
| 283 | |
| 284 | actual := tt.opts.ToURL() |
| 285 | assert.Equal(tt.expected, actual) |
| 286 | |
| 287 | _, err := ParseURL(actual) |
| 288 | assert.NoError(err) |
| 289 | }) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | func TestOptions_ToURL_reparsable(t *testing.T) { |
| 294 | assert := assert.New(t) |