-
Notifications
You must be signed in to change notification settings - Fork 41
/
postgres_test.go
88 lines (84 loc) · 1.97 KB
/
postgres_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ipam
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestDatasource(t *testing.T) {
tests := []struct {
name string
host string
port string
user string
password string
dbname string
sslmode SSLMode
want string
wantErr bool
}{
{
name: "basic, no escape",
host: "host",
port: "5432",
user: "user",
password: "password",
dbname: "dbname",
sslmode: SSLModeAllow,
want: "postgres://user:password@host:5432/dbname?sslmode=allow",
wantErr: false,
},
{
name: "username and password with escape chars",
host: "host",
port: "5432",
user: "us@r",
password: "pass:word",
dbname: "dbname",
sslmode: SSLModeAllow,
want: "postgres://us%40r:pass%3Aword@host:5432/dbname?sslmode=allow",
wantErr: false,
},
{
name: "username and password with very special characters",
user: "us@r",
password: "+S-@u]JBpWo^kduE7+(25zts",
dbname: "dbname",
sslmode: SSLModeAllow,
want: "postgres://us%40r:+S-%40u%5DJBpWo%5EkduE7+%2825zts@:/dbname?sslmode=allow",
wantErr: false,
},
{
name: "space allowed in dbname",
host: "host",
port: "5432",
user: "user",
password: "password",
dbname: "db name",
sslmode: SSLModeAllow,
want: "postgres://user:password@host:5432/db%20name?sslmode=allow",
wantErr: false,
},
{
name: "empty password",
host: "host",
port: "5432",
user: "user",
password: "",
dbname: "db name",
sslmode: SSLModeAllow,
want: "postgres://user:@host:5432/db%20name?sslmode=allow",
wantErr: false,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
got, err := dataSource(tc.host, tc.port, tc.user, tc.password, tc.dbname, tc.sslmode)
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.want, got)
})
}
}