/
helpers_test.go
95 lines (82 loc) · 2.78 KB
/
helpers_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
package goDB
import "fmt"
import "encoding/gob"
import "sqlite3"
import "testing"
type TestDatabase sqlite3.Database
var FOO *sqlite3.Table
var BAR *sqlite3.Table
func init() {
FOO = &sqlite3.Table{"foo", "number INTEGER, text VARCHAR(20)"}
BAR = &sqlite3.Table{"bar", "number INTEGER, value BLOB"}
}
type TwoItems struct {
Number string
Text string
}
func (t *TwoItems) String() string {
return "[" + t.Number + " : " + t.Text + "]"
}
func fatalOnError(t *testing.T, e error, message string, parameters ...interface{}) {
if e != nil {
t.Fatalf("%v : %v", e, fmt.Sprintf(message, parameters...))
}
}
func fatalOnSuccess(t *testing.T, e error, message string, parameters ...interface{}) {
if e == nil {
t.Fatalf("%v : %v", e, fmt.Sprintf(message, parameters...))
}
}
func (db *TestDatabase) stepThroughRows(t *testing.T, table *sqlite3.Table) (c int) {
var e error
sql := fmt.Sprintf("SELECT * from %v;", table.Name)
c, e = (*sqlite3.Database)(db).Execute(sql, func(st *sqlite3.Statement, values ...interface{}) {
data := values[1]
switch data := data.(type) {
case *gob.Decoder:
blob := &TwoItems{}
data.Decode(blob)
t.Logf("BLOB => %v: %v, %v: %v\n", sqlite3.ResultColumn(0).Name(st), sqlite3.ResultColumn(0).Value(st), st.ColumnName(1), blob)
default:
t.Logf("TEXT => %v: %v, %v: %v\n", sqlite3.ResultColumn(0).Name(st), sqlite3.ResultColumn(0).Value(st), st.ColumnName(1), st.Column(1))
}
})
fatalOnError(t, e, "%v failed on step %v", sql, c)
if rows, _ := table.Rows((*sqlite3.Database)(db)); rows != c {
t.Fatalf("%v: %v rows expected, %v rows found", table.Name, rows, c)
}
return
}
func (db *TestDatabase) runQuery(t *testing.T, sql string, params ...interface{}) {
st, e := (*sqlite3.Database)(db).Prepare(sql, params...)
fatalOnError(t, e, st.SQLSource())
st.Step()
st.Finalize()
}
func (db *TestDatabase) createTestTables(t *testing.T, tables ...*sqlite3.Table) {
for _, table := range tables {
testdb := (*sqlite3.Database)(db)
table.Drop(testdb)
table.Create(testdb)
if c, _ := table.Rows(testdb); c != 0 {
t.Fatalf("%v already contains data", table.Name)
}
}
}
func (db *TestDatabase) populate(t *testing.T, table *sqlite3.Table) {
switch table.Name {
case "foo":
db.runQuery(t, "INSERT INTO foo values (1, 'this is a test')")
db.runQuery(t, "INSERT INTO foo values (?, ?)", 2, "holy moly")
if c, _ := table.Rows((*sqlite3.Database)(db)); c != 2 {
t.Fatal("Failed to populate %v", table.Name)
}
case "bar":
db.runQuery(t, "INSERT INTO bar values (1, 'this is a test')")
db.runQuery(t, "INSERT INTO bar values (?, ?)", 2, "holy moly")
db.runQuery(t, "INSERT INTO bar values (?, ?)", 3, TwoItems{"holy moly", "guacomole"})
if c, _ := table.Rows((*sqlite3.Database)(db)); c != 3 {
t.Fatal("Failed to populate %v", table.Name)
}
}
}