-
Notifications
You must be signed in to change notification settings - Fork 101
/
sqlite3_test.go
284 lines (238 loc) · 6.52 KB
/
sqlite3_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package qbs
import (
"testing"
//"time"
_ "github.com/mattn/go-sqlite3"
)
var sqlite3Syntax = dialectSyntax{
NewSqlite3(),
"CREATE TABLE IF NOT EXISTS `without_pk` ( `first` text, `last` text, `amount` integer )",
"CREATE TABLE `with_pk` ( `primary` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `first` text, `last` text, `amount` integer )",
"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` text",
"CREATE UNIQUE INDEX `iname` ON `itable` (`a`, `b`, `c`)",
"CREATE INDEX `iname2` ON `itable2` (`d`, `e`)",
}
func registerSqlite3Test() {
RegisterSqlite3("/tmp/foo.db")
}
func setupSqlite3Db() (*Migration, *Qbs) {
registerSqlite3Test()
mg, _ := GetMigration()
q, _ := GetQbs()
return mg, q
}
var sqlite3SqlTypeResults []string = []string{
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"real",
"real",
"text",
"text",
"text",
"text",
"integer",
"integer",
"integer",
"real",
"text",
"text",
"text",
}
func TestSqlite3SqlType(t *testing.T) {
assert := NewAssert(t)
d := NewSqlite3()
testModel := structPtrToModel(new(typeTestTable), false, nil)
for index, column := range testModel.fields {
if storedResult := sqlite3SqlTypeResults[index]; storedResult != "-" {
result := d.sqlType(*column)
assert.Equal(storedResult, result)
}
}
/*for _, column := range testModel.fields {
result := d.sqlType(*column)
switch column.camelName {
case "Bool":
assert.Equal("integer", result)
case "Int8":
assert.Equal("integer", result)
case "Int16":
assert.Equal("integer", result)
case "Int32":
assert.Equal("integer", result)
case "UInt8":
assert.Equal("integer", result)
case "UInt16":
assert.Equal("integer", result)
case "UInt32":
assert.Equal("integer", result)
case "Int":
assert.Equal("integer", result)
case "UInt":
assert.Equal("integer", result)
case "Int64":
assert.Equal("integer", result)
case "UInt64":
assert.Equal("integer", result)
case "Float32":
assert.Equal("real", result)
case "Float64":
assert.Equal("real", result)
case "Varchar":
assert.Equal("text", result)
case "LongText":
assert.Equal("text", result)
case "Time":
assert.Equal("text", result)
case "Slice":
assert.Equal("text", result)
case "DerivedInt":
assert.Equal("integer", result)
case "DerivedInt16":
assert.Equal("integer", result)
case "DerivedBool":
assert.Equal("integer", result)
case "DerivedFloat":
assert.Equal("real", result)
case "DerivedTime":
assert.Equal("text", result)
}
}*/
}
func TestSqlite3Transaction(t *testing.T) {
registerSqlite3Test()
doTestTransaction(NewAssert(t))
}
func TestSqlite3SaveAndDelete(t *testing.T) {
mg, q := setupSqlite3Db()
doTestSaveAndDelete(NewAssert(t), mg, q)
}
func TestSqlite3SaveAgain(t *testing.T) {
mg, q := setupSqlite3Db()
doTestSaveAgain(NewAssert(t), mg, q)
}
func TestSqlite3ForeignKey(t *testing.T) {
registerSqlite3Test()
doTestForeignKey(NewAssert(t))
}
func TestSqlite3Find(t *testing.T) {
registerSqlite3Test()
doTestFind(NewAssert(t))
}
func TestSqlite3CreateTable(t *testing.T) {
mg, _ := setupSqlite3Db()
doTestCreateTable(NewAssert(t), mg)
}
func TestSqlite3Update(t *testing.T) {
mg, q := setupSqlite3Db()
doTestUpdate(NewAssert(t), mg, q)
}
func TestSqlite3Validation(t *testing.T) {
mg, q := setupSqlite3Db()
doTestValidation(NewAssert(t), mg, q)
}
func TestSqlite3BoolType(t *testing.T) {
mg, q := setupSqlite3Db()
doTestBoolType(NewAssert(t), mg, q)
}
func TestSqlite3StringPk(t *testing.T) {
mg, q := setupSqlite3Db()
doTestStringPk(NewAssert(t), mg, q)
}
func TestSqlite3Count(t *testing.T) {
registerSqlite3Test()
doTestCount(NewAssert(t))
}
func TestSqlite3QueryMap(t *testing.T) {
mg, q := setupSqlite3Db()
doTestQueryMap(NewAssert(t), mg, q)
}
func TestSqlite3BulkInsert(t *testing.T) {
registerSqlite3Test()
doTestBulkInsert(NewAssert(t))
}
func TestSqlite3QueryStruct(t *testing.T) {
registerSqlite3Test()
doTestQueryStruct(NewAssert(t))
}
func TestSqlite3CustomNameConvertion(t *testing.T) {
registerSqlite3Test()
ColumnNameToFieldName = noConvert
FieldNameToColumnName = noConvert
TableNameToStructName = noConvert
StructNameToTableName = noConvert
doTestForeignKey(NewAssert(t))
ColumnNameToFieldName = snakeToUpperCamel
FieldNameToColumnName = toSnake
TableNameToStructName = snakeToUpperCamel
StructNameToTableName = toSnake
}
func TestSqlite3ConnectionLimit(t *testing.T) {
registerSqlite3Test()
doTestConnectionLimit(NewAssert(t))
}
func TestSqlite3Iterate(t *testing.T) {
registerSqlite3Test()
doTestIterate(NewAssert(t))
}
func TestSqlite3AddColumnSQL(t *testing.T) {
doTestAddColumSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3CreateTableSQL(t *testing.T) {
doTestCreateTableSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3CreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3InsertSQL(t *testing.T) {
doTestInsertSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3UpdateSQL(t *testing.T) {
doTestUpdateSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3DeleteSQL(t *testing.T) {
doTestDeleteSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3SelectionSQL(t *testing.T) {
doTestSelectionSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3QuerySQL(t *testing.T) {
doTestQuerySQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3DropTableSQL(t *testing.T) {
doTestDropTableSQL(NewAssert(t), sqlite3Syntax)
}
func TestSqlite3SaveNullable(t *testing.T) {
mg, q := setupSqlite3Db()
doTestSaveNullable(NewAssert(t), mg, q)
}
func BenchmarkSqlite3Find(b *testing.B) {
registerSqlite3Test()
doBenchmarkFind(b, b.N)
}
func BenchmarkSqlite3DbQuery(b *testing.B) {
registerSqlite3Test()
doBenchmarkDbQuery(b, b.N)
}
func BenchmarkSqlite3StmtQuery(b *testing.B) {
registerSqlite3Test()
doBenchmarkStmtQuery(b, b.N)
}
func BenchmarkSqlite3Transaction(b *testing.B) {
registerSqlite3Test()
doBenchmarkTransaction(b, b.N)
}