forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
hook_test.go
80 lines (64 loc) · 1.57 KB
/
hook_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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite_test
import (
"fmt"
"testing"
. "github.com/gwenn/gosqlite"
)
func commitHook(d interface{}) bool {
if t, ok := d.(*testing.T); ok {
t.Log("CMT")
} else {
fmt.Println(d)
}
return false
}
func rollbackHook(d interface{}) {
if t, ok := d.(*testing.T); ok {
t.Log("RBK")
} else {
fmt.Println(d)
}
}
func updateHook(d interface{}, a Action, dbName, tableName string, rowID int64) {
if t, ok := d.(*testing.T); ok {
t.Logf("UPD: %d, %s.%s.%d\n", a, dbName, tableName, rowID)
} else {
fmt.Printf("%s: %d, %s.%s.%d\n", d, a, dbName, tableName, rowID)
}
}
func TestNoHook(t *testing.T) {
db := open(t)
defer checkClose(db, t)
db.CommitHook(nil, nil)
db.RollbackHook(nil, nil)
db.UpdateHook(nil, nil)
}
func TestCommitHook(t *testing.T) {
skipIfCgoCheckActive(t)
db := open(t)
defer checkClose(db, t)
db.CommitHook(commitHook, t)
checkNoError(t, db.Begin(), "%s")
createTable(db, t)
checkNoError(t, db.Commit(), "%s")
}
func TestRollbackHook(t *testing.T) {
skipIfCgoCheckActive(t)
db := open(t)
defer checkClose(db, t)
db.RollbackHook(rollbackHook, t)
checkNoError(t, db.Begin(), "%s")
createTable(db, t)
checkNoError(t, db.Rollback(), "%s")
}
func TestUpdateHook(t *testing.T) {
skipIfCgoCheckActive(t)
db := open(t)
defer checkClose(db, t)
createTable(db, t)
db.UpdateHook(updateHook, t)
checkNoError(t, db.Exec("INSERT INTO test VALUES (1, 273.1, 0, 'data')"), "%s")
}