forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_details_test.go
135 lines (113 loc) · 3.03 KB
/
connection_details_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package pop_test
import (
"testing"
"github.com/gobuffalo/pop"
"github.com/stretchr/testify/require"
)
func Test_ConnectionDetails_Finalize(t *testing.T) {
r := require.New(t)
cd := &pop.ConnectionDetails{
URL: "postgres://user:pass@host:port/database",
}
err := cd.Finalize()
r.NoError(err)
r.Equal(cd.Database, "database")
r.Equal(cd.Dialect, "postgres")
r.Equal(cd.Host, "host")
r.Equal(cd.Password, "pass")
r.Equal(cd.Port, "port")
r.Equal(cd.User, "user")
}
func Test_ConnectionDetails_Finalize_MySQL_DSN(t *testing.T) {
r := require.New(t)
url := "mysql://user:pass@(host:port)/database?param1=value1¶m2=value2"
cd := &pop.ConnectionDetails{
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("mysql", cd.Dialect)
r.Equal("user", cd.User)
r.Equal("pass", cd.Password)
r.Equal("host", cd.Host)
r.Equal("port", cd.Port)
r.Equal("database", cd.Database)
}
func Test_ConnectionDetails_Finalize_MySQL_DSN_collation(t *testing.T) {
r := require.New(t)
urls := []string{
"mysql://user:pass@(host:port)/database?collation=utf8mb4_general_ci",
"mysql://user:pass@(host:port)/database?collation=utf8mb4_general_ci&readTimeout=10s",
"mysql://user:pass@(host:port)/database?readTimeout=10s&collation=utf8mb4_general_ci",
}
for _, url := range urls {
cd := &pop.ConnectionDetails{
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("mysql", cd.Dialect)
r.Equal("user", cd.User)
r.Equal("pass", cd.Password)
r.Equal("host", cd.Host)
r.Equal("port", cd.Port)
r.Equal("database", cd.Database)
r.Equal("utf8mb4_general_ci", cd.Encoding)
}
}
func Test_ConnectionDetails_Finalize_MySQL_DSN_Protocol(t *testing.T) {
r := require.New(t)
url := "mysql://user:pass@tcp(host:port)/protocol"
cd := &pop.ConnectionDetails{
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("mysql", cd.Dialect)
r.Equal("user", cd.User)
r.Equal("pass", cd.Password)
r.Equal("host", cd.Host)
r.Equal("port", cd.Port)
r.Equal("protocol", cd.Database)
}
func Test_ConnectionDetails_Finalize_MySQL_DSN_Socket(t *testing.T) {
r := require.New(t)
url := "mysql://user:pass@unix(/path/to/socket)/socket"
cd := &pop.ConnectionDetails{
URL: url,
}
err := cd.Finalize()
r.NoError(err)
r.Equal(url, cd.URL)
r.Equal("mysql", cd.Dialect)
r.Equal("user", cd.User)
r.Equal("pass", cd.Password)
r.Equal("/path/to/socket", cd.Host)
r.Equal("socket", cd.Port)
r.Equal("socket", cd.Database)
}
func Test_ConnectionDetails_Finalize_UnknownDialect(t *testing.T) {
r := require.New(t)
cd := &pop.ConnectionDetails{
URL: "unknown://user:pass@host:port/database",
}
err := cd.Finalize()
r.Error(err)
}
func Test_ConnectionDetails_Finalize_SQLite(t *testing.T) {
r := require.New(t)
cd := &pop.ConnectionDetails{
URL: "sqlite3:///tmp/foo.db",
}
err := cd.Finalize()
r.NoError(err)
r.Equal(cd.Database, "/tmp/foo.db")
r.Equal(cd.Dialect, "sqlite3")
r.Equal(cd.Host, "")
r.Equal(cd.Password, "")
r.Equal(cd.Port, "")
r.Equal(cd.User, "")
}