-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn_test.go
117 lines (89 loc) · 2.54 KB
/
txn_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package txn
import (
"context"
"errors"
"testing"
)
// MockAdapter implements the Adapter interface for testing
type MockAdapter struct {
beginError error
commitError error
rollbackError error
}
func (ma *MockAdapter) Begin(ctx context.Context) error { return ma.beginError }
func (ma *MockAdapter) Commit(ctx context.Context) error { return ma.commitError }
func (ma *MockAdapter) Rollback(ctx context.Context) error { return ma.rollbackError }
func (ma *MockAdapter) End(ctx context.Context) {}
func TestNew(t *testing.T) {
tx := New()
if tx == nil {
t.Fatal("New returned nil")
}
}
func TestRegister(t *testing.T) {
tx := New()
adapter := &MockAdapter{}
tx.Register(adapter)
if len(tx.(*txn).adapters) != 1 || tx.(*txn).adapters[0] != adapter {
t.Fatal("Register failed to add adapter")
}
}
func TestBegin_Success(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{})
tx.Register(&MockAdapter{})
err := tx.Begin(context.Background())
if err != nil {
t.Fatalf("Begin failed unexpectedly: %v", err)
}
}
func TestBegin_Failure(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{beginError: errors.New("begin error")})
err := tx.Begin(context.Background())
if err == nil {
t.Fatal("Expected Begin to fail, but it succeeded")
}
}
func TestCommit_Success(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{})
tx.Register(&MockAdapter{})
err := tx.Commit(context.Background())
if err != nil {
t.Fatalf("Commit failed unexpectedly: %v", err)
}
}
func TestCommit_Failure(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{commitError: errors.New("commit error")})
err := tx.Commit(context.Background())
if err == nil {
t.Fatal("Expected Commit to fail, but it succeeded")
}
}
func TestRollback_Success(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{})
tx.Register(&MockAdapter{})
err := tx.Rollback(context.Background())
if err != nil {
t.Fatalf("Rollback failed unexpectedly: %v", err)
}
}
func TestRollback_Failure(t *testing.T) {
tx := New()
tx.Register(&MockAdapter{rollbackError: errors.New("rollback error")})
err := tx.Rollback(context.Background())
if err == nil {
t.Fatal("Expected Rollback to fail, but it succeeded")
}
}
func TestCancel(t *testing.T) {
tx := New()
adapter := &MockAdapter{} // Create a new instance to capture the rollback call
tx.Register(adapter)
tx.Cancel(context.Background())
// Ideally, you'd assert that the adapter's Rollback method was called here.
// Since it's a mock, you can add a flag to MockAdapter and check if it's set after Cancel.
}