forked from coocood/qbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_test.go
157 lines (129 loc) · 3.95 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
package qbs
import (
"fmt"
"github.com/coocood/assrt"
_ "github.com/coocood/mysql"
"testing"
"time"
)
const (
mysqlDrvformat = "%v@/%v?charset=utf8&parseTime=true&loc=Local"
mysqlDriver = "mysql"
)
var mysqlSyntax = dialectSyntax{
NewMysql(),
"CREATE TABLE IF NOT EXISTS `without_pk` ( `first` longtext, `last` longtext, `amount` int )",
"CREATE TABLE `with_pk` ( `primary` bigint PRIMARY KEY AUTO_INCREMENT, `first` longtext, `last` longtext, `amount` int )",
"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 `c` 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() {
Register(mysqlDriver, fmt.Sprintf(mysqlDrvformat, testDbName, testDbUser), testDbName, NewMysql())
}
func TestMysqlSqlType(t *testing.T) {
assert := assrt.NewAssert(t)
d := NewMysql()
assert.Equal("boolean", d.sqlType(true, 0))
var indirect interface{} = true
assert.Equal("boolean", d.sqlType(indirect, 0))
assert.Equal("int", d.sqlType(uint32(2), 0))
assert.Equal("bigint", d.sqlType(int64(1), 0))
assert.Equal("double", d.sqlType(1.8, 0))
assert.Equal("longblob", d.sqlType([]byte("asdf"), 0))
assert.Equal("longtext", d.sqlType("astring", 0))
assert.Equal("longtext", d.sqlType("a", 65536))
assert.Equal("varchar(128)", d.sqlType("b", 128))
assert.Equal("timestamp", d.sqlType(time.Now(), 0))
}
func TestMysqlTransaction(t *testing.T) {
mg, q := setupMysqlDb()
doTestTransaction(t, mg, q)
}
func TestMysqlSaveAndDelete(t *testing.T) {
mg, q := setupMysqlDb()
doTestSaveAndDelete(t, mg, q)
}
func TestMysqlSaveAgain(t *testing.T) {
mg, q := setupMysqlDb()
doTestSaveAgain(t, mg, q)
}
func TestMysqlForeignKey(t *testing.T) {
mg, q := setupMysqlDb()
doTestForeignKey(t, mg, q)
}
func TestMysqlFind(t *testing.T) {
registerMysqlTest()
doTestFind(t)
}
func TestMysqlCreateTable(t *testing.T) {
mg, _ := setupMysqlDb()
doTestCreateTable(t, mg)
}
func TestMysqlUpdate(t *testing.T) {
mg, q := setupMysqlDb()
doTestUpdate(t, mg, q)
}
func TestMysqlValidation(t *testing.T) {
mg, q := setupMysqlDb()
doTestValidation(t, mg, q)
}
func TestMysqlBoolType(t *testing.T) {
mg, q := setupMysqlDb()
doTestBoolType(t, mg, q)
}
func TestMysqlStringPk(t *testing.T) {
mg, q := setupMysqlDb()
doTestStringPk(t, mg, q)
}
func TestMysqlCount(t *testing.T) {
mg, q := setupMysqlDb()
doTestCount(t, mg, q)
}
func TestMysqlQueryMap(t *testing.T) {
mg, q := setupMysqlDb()
doTestQueryMap(t, mg, q)
}
func TestMysqlAddColumnSQL(t *testing.T) {
doTestAddColumSQL(t, mysqlSyntax)
}
func TestMysqlCreateTableSQL(t *testing.T) {
doTestCreateTableSQL(t, mysqlSyntax)
}
func TestMysqlCreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(t, mysqlSyntax)
}
func TestMysqlInsertSQL(t *testing.T) {
doTestInsertSQL(t, mysqlSyntax)
}
func TestMysqlUpdateSQL(t *testing.T) {
doTestUpdateSQL(t, mysqlSyntax)
}
func TestMysqlDeleteSQL(t *testing.T) {
doTestDeleteSQL(t, mysqlSyntax)
}
func TestMysqlSelectionSQL(t *testing.T) {
doTestSelectionSQL(t, mysqlSyntax)
}
func TestMysqlQuerySQL(t *testing.T) {
doTestQuerySQL(t, mysqlSyntax)
}
func TestMysqlDropTableSQL(t *testing.T) {
doTestDropTableSQL(t, mysqlSyntax)
}
func BenchmarkMysqlFind(b *testing.B) {
registerMysqlTest()
doBenchmarkFind(b)
}