-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers_test.go
86 lines (76 loc) · 2.35 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
package sqlite3
import (
"fmt"
"encoding/gob"
"testing"
)
var FOO *Table
var BAR *Table
func init() {
FOO = &Table{ "foo", "number INTEGER, text VARCHAR(20)" }
BAR = &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 *Database) stepThroughRows(t *testing.T, table *Table, verbose... bool) (c int) {
var e error
sql := fmt.Sprintf("SELECT * from %v;", table.Name)
c, e = db.Execute(sql, func(st *Statement, values ...interface{}) {
data := values[1]
switch data := data.(type) {
case *gob.Decoder:
blob := &TwoItems{}
data.Decode(blob)
if len(verbose) > 0 && verbose[0] {
t.Logf("BLOB => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), blob)
}
default:
if len(verbose) > 0 && verbose[0] {
t.Logf("TEXT => %v: %v, %v: %v\n", ResultColumn(0).Name(st), ResultColumn(0).Value(st), st.ColumnName(1), st.Column(1))
}
}
})
fatalOnError(t, e, "%v failed on step %v", sql, c)
if rows, _ := table.Rows(db); rows != c {
t.Fatalf("%v: %v rows expected, %v rows found", table.Name, rows, c)
}
return
}
func (db *Database) runQuery(t *testing.T, sql string, params... interface{}) {
st, e := db.Prepare(sql, params...)
fatalOnError(t, e, st.SQLSource())
st.Step()
st.Finalize()
}
func (db *Database) populate(t *testing.T, table *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(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(db); c != 3 {
t.Fatal("Failed to populate %v", table.Name)
}
}
}