This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
mysql_test.go
242 lines (204 loc) · 4.49 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
package tests
import (
"database/sql"
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
. "github.com/go-xorm/tests"
"github.com/go-xorm/xorm"
)
func connStr(db string) string {
conn := "root:@"
if ConnectionPort != "" {
conn += "tcp(127.0.0.1:" + ConnectionPort + ")"
}
return conn + "/" + db + "?charset=utf8"
}
/*
CREATE DATABASE IF NOT EXISTS xorm_test CHARACTER SET
utf8 COLLATE utf8_general_ci;
*/
func TestMysql(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", connStr("xorm_test"))
if err != nil {
t.Error(err)
return
}
defer engine.Close()
engine.ShowSQL(ShowTestSql)
BaseTestAll(engine, t)
BaseTestAllSnakeMapper(engine, t)
BaseTestAll2(engine, t)
BaseTestAll3(engine, t)
}
func TestMysqlSameMapper(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", connStr("xorm_test1"))
if err != nil {
t.Error(err)
return
}
defer engine.Close()
engine.ShowSQL(ShowTestSql)
engine.SetMapper(core.SameMapper{})
BaseTestAll(engine, t)
BaseTestAllSameMapper(engine, t)
BaseTestAll2(engine, t)
BaseTestAll3(engine, t)
}
func TestMysqlGonicMapper(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", connStr("xorm_test1"))
if err != nil {
t.Error(err)
return
}
defer engine.Close()
engine.ShowSQL(ShowTestSql)
engine.SetMapper(core.GonicMapper{})
BaseTestAll(engine, t)
//BaseTestAllSameMapper(engine, t)
BaseTestAll2(engine, t)
BaseTestAll3(engine, t)
}
func TestMysqlWithCache(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", connStr("xorm_test2"))
if err != nil {
t.Error(err)
return
}
defer engine.Close()
engine.SetDefaultCacher(NewCacher())
engine.ShowSQL(ShowTestSql)
BaseTestAll(engine, t)
BaseTestAllSnakeMapper(engine, t)
BaseTestAll2(engine, t)
}
func TestMysqlWithCacheSameMapper(t *testing.T) {
err := mysqlDdlImport()
if err != nil {
t.Error(err)
return
}
engine, err := xorm.NewEngine("mysql", connStr("xorm_test3"))
if err != nil {
t.Error(err)
return
}
defer engine.Close()
engine.SetMapper(core.SameMapper{})
engine.SetDefaultCacher(NewCacher())
engine.ShowSQL(ShowTestSql)
BaseTestAll(engine, t)
BaseTestAllSameMapper(engine, t)
BaseTestAll2(engine, t)
}
func newMysqlEngine() (*xorm.Engine, error) {
return xorm.NewEngine("mysql", connStr("xorm_test"))
}
func mysqlDdlImport() error {
engine, err := xorm.NewEngine("mysql", connStr(""))
if err != nil {
return err
}
engine.ShowSQL(ShowTestSql)
sqlResults, err := engine.ImportFile("../testdata/mysql_ddl.sql")
if err != nil {
return err
}
engine.Logger().Debug("sql results:", sqlResults)
engine.Close()
return nil
}
func newMysqlDriverDB() (*sql.DB, error) {
return sql.Open("mysql", connStr("xorm_test"))
}
func BenchmarkMysqlDriverInsert(t *testing.B) {
DoBenchDriver(newMysqlDriverDB, CreateTableMySql, DropTableMySql,
DoBenchDriverInsert, t)
}
func BenchmarkMysqlDriverFind(t *testing.B) {
DoBenchDriver(newMysqlDriverDB, CreateTableMySql, DropTableMySql,
DoBenchDriverFind, t)
}
func BenchmarkMysqlNoCacheInsert(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.ShowSQL(true)
DoBenchInsert(engine, t)
}
func BenchmarkMysqlNoCacheFind(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.ShowSQL(true)
DoBenchFind(engine, t)
}
func BenchmarkMysqlNoCacheFindPtr(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.ShowSQL(true)
DoBenchFindPtr(engine, t)
}
func BenchmarkMysqlCacheInsert(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.SetDefaultCacher(NewCacher())
engine.ShowSQL(true)
DoBenchInsert(engine, t)
}
func BenchmarkMysqlCacheFind(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.SetDefaultCacher(NewCacher())
engine.ShowSQL(true)
DoBenchFind(engine, t)
}
func BenchmarkMysqlCacheFindPtr(t *testing.B) {
engine, err := newMysqlEngine()
defer engine.Close()
if err != nil {
t.Error(err)
return
}
engine.SetDefaultCacher(NewCacher())
engine.ShowSQL(true)
DoBenchFindPtr(engine, t)
}