forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_details_test.go
108 lines (90 loc) · 2.3 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
package pop_test
import (
"testing"
"github.com/markbates/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_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, "")
}