-
Notifications
You must be signed in to change notification settings - Fork 1
/
savepoint_test.go
58 lines (45 loc) · 1.41 KB
/
savepoint_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
package hermes_test
import (
"testing"
)
// Test using savepoints for partial rollbacks.
func TestSavepoint(t *testing.T) {
db := connect(t)
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
if _, err := tx.Exec("create table test_savepoint(name varchar(64) not null)"); err != nil {
t.Errorf("Unable to create test_savepoint table in database: %s", err)
}
if _, err := tx.Exec("insert into test_savepoint (name) values ('abc')"); err != nil {
t.Errorf("Unable to insert first record in test_savepoint: %s", err)
}
savepoint, err := tx.Savepoint()
if err != nil {
t.Errorf("Unable to create savepoint: %s", err)
}
if savepoint == "" {
t.Error("invalid savepoint ID")
}
if _, err := tx.Exec("insert into test_savepoint (name) values ('def')"); err != nil {
t.Errorf("Unable to insert first record in test_savepoint: %s", err)
}
var count int
if err := tx.Get(&count, "select count(1) from test_savepoint"); err != nil {
t.Errorf("Failed to get test_savepoint record count: %s", err)
}
if err := tx.RollbackTo(savepoint); err != nil {
t.Errorf("Unable to rollback to savepoint: %s", err)
}
if count != 2 {
t.Errorf("Expected two records; got %d", count)
}
if err := tx.Get(&count, "select count(1) from test_savepoint"); err != nil {
t.Errorf("Failed to get test_savepoint record count: %s", err)
}
if count != 1 {
t.Errorf("Expected one record; got %d", count)
}
}