-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtransaction_test.go
83 lines (74 loc) · 1.47 KB
/
transaction_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
package sqlingo
import (
"context"
"errors"
"testing"
)
type mockTx struct {
isCommitted bool
isRolledBack bool
commitError error
}
func (m *mockTx) Commit() error {
if m.commitError != nil {
return m.commitError
}
m.isCommitted = true
return nil
}
func (m *mockTx) Rollback() error {
m.isRolledBack = true
return nil
}
func TestTransaction(t *testing.T) {
db := newMockDatabase()
err := db.BeginTx(nil, nil, func(tx Transaction) error {
if tx.GetDB() != db.GetDB() {
t.Error()
}
if tx.GetTx() == nil {
t.Error()
}
_, err := tx.Execute("<dummy>")
if err != nil {
t.Error(err)
}
return nil
})
if err != nil {
t.Error(err)
}
if !sharedMockConn.mockTx.isCommitted {
t.Error()
}
if sharedMockConn.mockTx.isRolledBack {
t.Error()
}
err = db.BeginTx(context.Background(), nil, func(tx Transaction) error {
return errors.New("error")
})
if err == nil {
t.Error("should get error here")
}
if sharedMockConn.mockTx.isCommitted {
t.Error()
}
if !sharedMockConn.mockTx.isRolledBack {
t.Error()
}
sharedMockConn.beginTxError = errors.New("error")
err = db.BeginTx(context.Background(), nil, func(tx Transaction) error {
return nil
})
if err == nil {
t.Error("should get error here")
}
sharedMockConn.beginTxError = nil
err = db.BeginTx(context.Background(), nil, func(tx Transaction) error {
sharedMockConn.mockTx.commitError = errors.New("error")
return nil
})
if err == nil {
t.Error("should get error here")
}
}