-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
dialect_mysql_test.go
246 lines (217 loc) · 6.42 KB
/
dialect_mysql_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package pop
import (
"io/ioutil"
"os"
"strings"
"testing"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/stretchr/testify/require"
)
func Test_MySQL_URL_As_Is(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "mysql://user:pass@(host:port)/dbase?opt=value",
}
err := cd.Finalize()
r.NoError(err)
m := &mysql{commonDialect{ConnectionDetails: cd}}
r.Equal("user:pass@(host:port)/dbase?opt=value", m.URL())
r.Equal("user:pass@(host:port)/?opt=value", m.urlWithoutDb())
r.Equal("user:pass@(host:port)/dbase?opt=value", m.MigrationURL())
}
func Test_MySQL_URL_Override_withURL(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
Database: "xx",
Host: "xx",
Port: "xx",
User: "xx",
Password: "xx",
URL: "mysql://user:pass@(host:port)/dbase?opt=value",
}
err := cd.Finalize()
r.NoError(err)
m := &mysql{commonDialect{ConnectionDetails: cd}}
r.Equal("user:pass@(host:port)/dbase?opt=value", m.URL())
r.Equal("user:pass@(host:port)/?opt=value", m.urlWithoutDb())
r.Equal("user:pass@(host:port)/dbase?opt=value", m.MigrationURL())
}
func Test_MySQL_URL_With_Values(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{
Database: "dbase",
Host: "host",
Port: "port",
User: "user",
Password: "pass",
Options: map[string]string{"opt": "value"},
}}}
r.Equal("user:pass@(host:port)/dbase?opt=value", m.URL())
r.Equal("user:pass@(host:port)/?opt=value", m.urlWithoutDb())
r.Equal("user:pass@(host:port)/dbase?opt=value", m.MigrationURL())
}
func Test_MySQL_URL_Without_User(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{
Password: "pass",
Database: "dbase",
}}}
// finalizerMySQL fills address part in real world.
// without user, password cannot live alone
r.Equal("(:)/dbase?", m.URL())
}
func Test_MySQL_URL_Without_Password(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{
User: "user",
Database: "dbase",
}}}
// finalizerMySQL fills address part in real world.
r.Equal("user@(:)/dbase?", m.URL())
}
func Test_MySQL_URL_urlParserMySQL_Standard(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "mysql://user:pass@(host:port)/database?collation=utf8¶m2=value2",
}
err := urlParserMySQL(cd)
r.NoError(err)
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)
// only collation is stored as options by urlParserMySQL()
r.Equal("utf8", cd.Options["collation"])
r.Equal("", cd.Options["param2"])
}
func Test_MySQL_URL_urlParserMySQL_With_Protocol(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "user:pass@tcp(host:port)/dbase",
}
err := urlParserMySQL(cd)
r.NoError(err)
r.Equal("user", cd.User)
r.Equal("pass", cd.Password)
r.Equal("host", cd.Host)
r.Equal("port", cd.Port)
r.Equal("dbase", cd.Database)
}
func Test_MySQL_URL_urlParserMySQL_Socket(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "unix(/tmp/socket)/dbase",
}
err := urlParserMySQL(cd)
r.NoError(err)
r.Equal("/tmp/socket", cd.Host)
r.Equal("socket", cd.Port)
// additional test without URL
cd.URL = ""
m := &mysql{commonDialect{ConnectionDetails: cd}}
r.True(strings.HasPrefix(m.URL(), "unix(/tmp/socket)/dbase?"))
r.True(strings.HasPrefix(m.urlWithoutDb(), "unix(/tmp/socket)/?"))
}
func Test_MySQL_URL_urlParserMySQL_Unsupported(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{
URL: "mysql://user:pass@host:port/dbase?opt=value",
}
err := urlParserMySQL(cd)
r.Error(err)
}
func Test_MySQL_Database_Open_Failure(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{}}}
err := m.CreateDB()
r.Error(err)
err = m.DropDB()
r.Error(err)
}
func Test_MySQL_FizzTranslator(t *testing.T) {
r := require.New(t)
cd := &ConnectionDetails{}
m := &mysql{commonDialect{ConnectionDetails: cd}}
ft := m.FizzTranslator()
r.IsType(&translators.MySQL{}, ft)
r.Implements((*fizz.Translator)(nil), ft)
}
func Test_MySQL_Finalizer_Default_CD(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{}}}
finalizerMySQL(m.ConnectionDetails)
r.Equal(hostMySQL, m.ConnectionDetails.Host)
r.Equal(portMySQL, m.ConnectionDetails.Port)
}
func Test_MySQL_Finalizer_Default_Options(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{}}}
finalizerMySQL(m.ConnectionDetails)
r.Contains(m.URL(), "multiStatements=true")
r.Contains(m.URL(), "parseTime=true")
r.Contains(m.URL(), "readTimeout=3s")
r.Contains(m.URL(), "collation=utf8mb4_general_ci")
}
func Test_MySQL_Finalizer_Preserve_User_Defined_Options(t *testing.T) {
r := require.New(t)
m := &mysql{commonDialect{ConnectionDetails: &ConnectionDetails{
Options: map[string]string{
"multiStatements": "false",
"parseTime": "false",
"readTimeout": "1h",
"collation": "utf8",
},
}}}
finalizerMySQL(m.ConnectionDetails)
r.Contains(m.URL(), "multiStatements=false")
r.Contains(m.URL(), "parseTime=false")
r.Contains(m.URL(), "readTimeout=1h")
r.Contains(m.URL(), "collation=utf8")
}
func (s *MySQLSuite) Test_MySQL_DDL_Operations() {
r := s.Require()
origDatabase := PDB.Dialect.Details().Database
PDB.Dialect.Details().Database = "pop_test_mysql_extra"
defer func() {
PDB.Dialect.Details().Database = origDatabase
}()
PDB.Dialect.DropDB() // clean up
err := PDB.Dialect.CreateDB()
r.NoError(err)
err = PDB.Dialect.CreateDB()
r.Error(err)
err = PDB.Dialect.DropDB()
r.NoError(err)
err = PDB.Dialect.DropDB()
r.Error(err)
}
func (s *MySQLSuite) Test_MySQL_DDL_Schema() {
r := s.Require()
f, err := ioutil.TempFile(os.TempDir(), "pop_test_mysql_dump")
r.NoError(err)
defer func() {
f.Close()
os.Remove(f.Name())
}()
// do it against "pop_test"
err = PDB.Dialect.DumpSchema(f)
r.NoError(err)
_, err = f.Seek(0, 0)
r.NoError(err)
err = PDB.Dialect.LoadSchema(f)
r.NoError(err)
origDatabase := PDB.Dialect.Details().Database
PDB.Dialect.Details().Database = "pop_test_not_exist"
defer func() {
PDB.Dialect.Details().Database = origDatabase
}()
// do it against "pop_test_not_exist"
_, err = f.Seek(0, 0)
r.NoError(err)
err = PDB.Dialect.LoadSchema(f)
r.Error(err)
err = PDB.Dialect.DumpSchema(f)
r.Error(err)
}