forked from coocood/qbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3_test.go
159 lines (131 loc) · 4.03 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
package qbs
import (
"database/sql"
"github.com/coocood/assrt"
"testing"
"time"
// _ "github.com/mattn/go-sqlite3"
)
const (
sqlite3Driver = "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 `c` text",
"CREATE UNIQUE INDEX `iname` ON `itable` (`a`, `b`, `c`)",
"CREATE INDEX `iname2` ON `itable2` (`d`, `e`)",
}
func openSqlite3Db() (*sql.DB, error) {
return sql.Open(sqlite3Driver, "/tmp/foo.db")
}
func registerSqlite3Test() {
// os.Remove("/tmp/foo.db")
Register(sqlite3Driver, "/tmp/foo.db", testDbName, NewSqlite3())
}
func setupSqlite3Db() (*Migration, *Qbs) {
registerSqlite3Test()
mg, _ := GetMigration()
q, _ := GetQbs()
return mg, q
}
func TestSqlite3SqlType(t *testing.T) {
assert := assrt.NewAssert(t)
d := NewSqlite3()
assert.Equal("integer", d.sqlType(true, 0))
var indirect interface{} = true
assert.Equal("integer", d.sqlType(indirect, 0))
assert.Equal("integer", d.sqlType(uint32(2), 0))
assert.Equal("integer", d.sqlType(int64(1), 0))
assert.Equal("real", d.sqlType(1.8, 0))
assert.Equal("text", d.sqlType([]byte("asdf"), 0))
assert.Equal("text", d.sqlType("astring", 0))
assert.Equal("text", d.sqlType("a", 65536))
assert.Equal("text", d.sqlType("b", 128))
assert.Equal("text", d.sqlType(time.Now(), 0))
}
func TestSqlite3Transaction(t *testing.T) {
mg, q := setupSqlite3Db()
doTestTransaction(t, mg, q)
}
func TestSqlite3SaveAndDelete(t *testing.T) {
mg, q := setupSqlite3Db()
doTestSaveAndDelete(t, mg, q)
}
func TestSqlite3SaveAgain(t *testing.T) {
mg, q := setupSqlite3Db()
doTestSaveAgain(t, mg, q)
}
func TestSqlite3ForeignKey(t *testing.T) {
mg, q := setupSqlite3Db()
doTestForeignKey(t, mg, q)
}
func TestSqlite3Find(t *testing.T) {
registerSqlite3Test()
doTestFind(t)
}
func TestSqlite3CreateTable(t *testing.T) {
mg, _ := setupSqlite3Db()
doTestCreateTable(t, mg)
}
func TestSqlite3Update(t *testing.T) {
mg, q := setupSqlite3Db()
doTestUpdate(t, mg, q)
}
func TestSqlite3Validation(t *testing.T) {
mg, q := setupSqlite3Db()
doTestValidation(t, mg, q)
}
func TestSqlite3BoolType(t *testing.T) {
mg, q := setupSqlite3Db()
doTestBoolType(t, mg, q)
}
func TestSqlite3StringPk(t *testing.T) {
mg, q := setupSqlite3Db()
doTestStringPk(t, mg, q)
}
func TestSqlite3Count(t *testing.T) {
mg, q := setupSqlite3Db()
doTestCount(t, mg, q)
}
func TestSqlite3QueryMap(t *testing.T) {
mg, q := setupSqlite3Db()
doTestQueryMap(t, mg, q)
}
func TestSqlite3AddColumnSQL(t *testing.T) {
doTestAddColumSQL(t, sqlite3Syntax)
}
func TestSqlite3CreateTableSQL(t *testing.T) {
doTestCreateTableSQL(t, sqlite3Syntax)
}
func TestSqlite3CreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(t, sqlite3Syntax)
}
func TestSqlite3InsertSQL(t *testing.T) {
doTestInsertSQL(t, sqlite3Syntax)
}
func TestSqlite3UpdateSQL(t *testing.T) {
doTestUpdateSQL(t, sqlite3Syntax)
}
func TestSqlite3DeleteSQL(t *testing.T) {
doTestDeleteSQL(t, sqlite3Syntax)
}
func TestSqlite3SelectionSQL(t *testing.T) {
doTestSelectionSQL(t, sqlite3Syntax)
}
func TestSqlite3QuerySQL(t *testing.T) {
doTestQuerySQL(t, sqlite3Syntax)
}
func TestSqlite3DropTableSQL(t *testing.T) {
doTestDropTableSQL(t, sqlite3Syntax)
}
func BenchmarkSqlite3Find(b *testing.B) {
doBenchmarkFind(b)
}