forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect_postgresql_test.go
110 lines (91 loc) · 2.43 KB
/
dialect_postgresql_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package pop
import (
"os"
"os/user"
"testing"
"github.com/stretchr/testify/require"
)
func Test_PostgreSQL_Connection_String(t *testing.T) {
r := require.New(t)
url := "host=host port=1234 dbname=database user=user password=pass"
cd := &ConnectionDetails{
Dialect: "postgres",
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("postgres", cd.Dialect)
r.Equal("host", cd.Host)
r.Equal("pass", cd.Password)
r.Equal("1234", cd.Port)
r.Equal("user", cd.User)
r.Equal("database", cd.Database)
}
func Test_PostgreSQL_Connection_String_Options(t *testing.T) {
r := require.New(t)
url := "host=host port=1234 dbname=database user=user password=pass sslmode=disable fallback_application_name=test_app connect_timeout=10 sslcert=/some/location sslkey=/some/other/location sslrootcert=/root/location"
cd := &ConnectionDetails{
Dialect: "postgres",
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("disable", cd.Options["sslmode"])
r.Equal("test_app", cd.Options["fallback_application_name"])
}
func Test_PostgreSQL_Connection_String_Without_User(t *testing.T) {
r := require.New(t)
url := "dbname=database"
cd := &ConnectionDetails{
Dialect: "postgres",
URL: url,
}
err := cd.Finalize()
r.NoError(err)
uc := os.Getenv("PGUSER")
if uc == "" {
c, err := user.Current()
if err == nil {
uc = c.Username
}
}
r.Equal(url, cd.URL)
r.Equal("postgres", cd.Dialect)
var foundHost bool
for _, host := range []string{
"/var/run/postgresql", // Debian
"/private/tmp", // OSX - homebrew
"/tmp", // standard PostgreSQL
"localhost", // Windows does not do sockets
} {
if cd.Host == host {
foundHost = true
break
}
}
r.True(foundHost, `Got host: "%s"`, cd.Host)
r.Equal(os.Getenv("PGPASSWORD"), cd.Password)
r.Equal(portPostgreSQL, cd.Port) // fallback
r.Equal(uc, cd.User)
r.Equal("database", cd.Database)
}
func Test_PostgreSQL_Connection_String_Failure(t *testing.T) {
r := require.New(t)
url := "abc"
cd := &ConnectionDetails{
Dialect: "postgres",
URL: url,
}
err := cd.Finalize()
r.Error(err)
r.Equal("postgres", cd.Dialect)
}
func Test_PostgreSQL_Quotable(t *testing.T) {
r := require.New(t)
p := postgresql{}
r.Equal(`"table_name"`, p.Quote("table_name"))
r.Equal(`"schema"."table_name"`, p.Quote("schema.table_name"))
r.Equal(`"schema"."table name"`, p.Quote(`"schema"."table name"`))
}