forked from coocood/qbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_test.go
255 lines (216 loc) · 6.07 KB
/
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
247
248
249
250
251
252
253
254
255
package qbs
import (
_ "github.com/coocood/mysql"
"testing"
)
var mysqlSyntax = dialectSyntax{
NewMysql(),
"CREATE TABLE IF NOT EXISTS `without_pk` ( `first` longtext, `last` longtext, `amount` bigint )",
"CREATE TABLE `with_pk` ( `primary` bigint PRIMARY KEY AUTO_INCREMENT, `first` longtext, `last` longtext, `amount` bigint )",
"INSERT INTO `sql_gen_model` (`prim`, `first`, `last`, `amount`) VALUES (?, ?, ?, ?)",
"UPDATE `sql_gen_model` SET `first` = ?, `last` = ?, `amount` = ? WHERE `prim` = ?",
"DELETE FROM `sql_gen_model` WHERE `prim` = ?",
"SELECT `post`.`id`, `post`.`author_id`, `post`.`content`, `author`.`id` AS author___id, `author`.`name` AS author___name FROM `post` LEFT JOIN `user` AS `author` ON `post`.`author_id` = `author`.`id`",
"SELECT `name`, `grade`, `score` FROM `student` WHERE (grade IN (?, ?, ?)) AND ((score <= ?) OR (score >= ?)) ORDER BY `name`, `grade` DESC LIMIT ? OFFSET ?",
"DROP TABLE IF EXISTS `drop_table`",
"ALTER TABLE `a` ADD COLUMN `newc` varchar(100)",
"CREATE UNIQUE INDEX `iname` ON `itable` (`a`, `b`, `c`)",
"CREATE INDEX `iname2` ON `itable2` (`d`, `e`)",
}
func setupMysqlDb() (*Migration, *Qbs) {
registerMysqlTest()
mg, _ := GetMigration()
q, _ := GetQbs()
return mg, q
}
func registerMysqlTest() {
dsn := new(DataSourceName)
dsn.DbName = testDbName
dsn.Username = "root"
dsn.Dialect = NewMysql()
dsn.Append("parseTime", "true").Append("loc", "Local")
RegisterWithDataSourceName(dsn)
}
var mysqlSqlTypeResults []string = []string{
"boolean",
"int",
"int",
"int",
"int",
"int",
"int",
"bigint",
"bigint",
"bigint",
"bigint",
"double",
"double",
"varchar(128)",
"longtext",
"timestamp",
"longblob",
"bigint",
"int",
"boolean",
"double",
"timestamp",
"varchar(128)",
"longtext",
}
func TestMysqlSqlType(t *testing.T) {
assert := NewAssert(t)
d := NewMysql()
testModel := structPtrToModel(new(typeTestTable), false, nil)
for index, column := range testModel.fields {
if storedResult := mysqlSqlTypeResults[index]; storedResult != "-" {
result := d.sqlType(*column)
assert.Equal(storedResult, result)
}
}
}
func TestMysqlTransaction(t *testing.T) {
registerMysqlTest()
doTestTransaction(NewAssert(t))
}
func TestMysqlSaveAndDelete(t *testing.T) {
mg, q := setupMysqlDb()
doTestSaveAndDelete(NewAssert(t), mg, q)
}
func TestMysqlSaveAgain(t *testing.T) {
mg, q := setupMysqlDb()
doTestSaveAgain(NewAssert(t), mg, q)
}
func TestMysqlForeignKey(t *testing.T) {
registerMysqlTest()
doTestForeignKey(NewAssert(t))
}
func TestMysqlFind(t *testing.T) {
registerMysqlTest()
doTestFind(NewAssert(t))
}
func TestMysqlCreateTable(t *testing.T) {
mg, _ := setupMysqlDb()
doTestCreateTable(NewAssert(t), mg)
}
func TestMysqlUpdate(t *testing.T) {
mg, q := setupMysqlDb()
doTestUpdate(NewAssert(t), mg, q)
}
func TestMysqlValidation(t *testing.T) {
mg, q := setupMysqlDb()
doTestValidation(NewAssert(t), mg, q)
}
func TestMysqlBoolType(t *testing.T) {
mg, q := setupMysqlDb()
doTestBoolType(NewAssert(t), mg, q)
}
func TestMysqlStringPk(t *testing.T) {
mg, q := setupMysqlDb()
doTestStringPk(NewAssert(t), mg, q)
}
func TestMysqlCount(t *testing.T) {
registerMysqlTest()
doTestCount(NewAssert(t))
}
func TestMysqlQueryMap(t *testing.T) {
mg, q := setupMysqlDb()
doTestQueryMap(NewAssert(t), mg, q)
}
func TestMysqlBulkInsert(t *testing.T) {
registerMysqlTest()
doTestBulkInsert(NewAssert(t))
}
func TestMysqlQueryStruct(t *testing.T) {
registerMysqlTest()
doTestQueryStruct(NewAssert(t))
}
func TestMysqlCustomNameConvertion(t *testing.T) {
registerMysqlTest()
ColumnNameToFieldName = noConvert
FieldNameToColumnName = noConvert
TableNameToStructName = noConvert
StructNameToTableName = noConvert
doTestForeignKey(NewAssert(t))
ColumnNameToFieldName = snakeToUpperCamel
FieldNameToColumnName = toSnake
TableNameToStructName = snakeToUpperCamel
StructNameToTableName = toSnake
}
func TestMysqlConnectionLimit(t *testing.T) {
registerMysqlTest()
doTestConnectionLimit(NewAssert(t))
}
func TestMysqlIterate(t *testing.T) {
registerMysqlTest()
doTestIterate(NewAssert(t))
}
func TestMysqlAddColumnSQL(t *testing.T) {
doTestAddColumSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlCreateTableSQL(t *testing.T) {
doTestCreateTableSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlCreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlInsertSQL(t *testing.T) {
doTestInsertSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlUpdateSQL(t *testing.T) {
doTestUpdateSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlDeleteSQL(t *testing.T) {
doTestDeleteSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlSelectionSQL(t *testing.T) {
doTestSelectionSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlQuerySQL(t *testing.T) {
doTestQuerySQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlDropTableSQL(t *testing.T) {
doTestDropTableSQL(NewAssert(t), mysqlSyntax)
}
func TestMysqlDataSourceName(t *testing.T) {
dsn := new(DataSourceName)
dsn.DbName = "abc"
dsn.Username = "john"
dsn.Dialect = NewMysql()
assert := NewAssert(t)
assert.Equal("john@/abc", dsn)
dsn.Password = "123"
assert.Equal("john:123@/abc", dsn)
dsn.Host = "192.168.1.3"
assert.Equal("john:123@tcp(192.168.1.3)/abc", dsn)
dsn.UnixSocket = true
assert.Equal("john:123@unix(192.168.1.3)/abc", dsn)
dsn.Append("charset", "utf8")
dsn.Append("parseTime", "true")
assert.Equal("john:123@unix(192.168.1.3)/abc?charset=utf8&parseTime=true", dsn)
dsn.Port = "3336"
assert.Equal("john:123@unix(192.168.1.3:3336)/abc?charset=utf8&parseTime=true", dsn)
}
func TestMysqlSaveNullable(t *testing.T) {
mg, q := setupMysqlDb()
doTestSaveNullable(NewAssert(t), mg, q)
}
func BenchmarkMysqlFind(b *testing.B) {
registerMysqlTest()
doBenchmarkFind(b, b.N)
}
func BenchmarkMysqlQueryStruct(b *testing.B) {
registerMysqlTest()
doBenchmarkQueryStruct(b, b.N)
}
func BenchmarkMysqlDbQuery(b *testing.B) {
registerMysqlTest()
doBenchmarkDbQuery(b, b.N)
}
func BenchmarkMysqlStmtQuery(b *testing.B) {
registerMysqlTest()
doBenchmarkStmtQuery(b, b.N)
}
func BenchmarkMysqlTransaction(b *testing.B) {
registerMysqlTest()
doBenchmarkTransaction(b, b.N)
}