-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_test.go
176 lines (140 loc) · 5.03 KB
/
mongo_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package txnmongo
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
)
func TestNew(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
adapter := New(mt.Client)
assert.NotNil(t, adapter)
assert.IsType(t, &mongoAdapter{}, adapter)
}
func TestBegin_Success(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
// Mock successful session start and transaction start
mt.AddMockResponses(mtest.CreateSuccessResponse(), mtest.CreateSuccessResponse())
b := true
ses, _ := mt.Client.StartSession(&options.SessionOptions{
Snapshot: &b,
CausalConsistency: &b,
})
adapter := &mongoAdapter{client: mt.Client, sess: ses}
err := adapter.Begin(context.Background())
assert.Nil(t, err)
assert.NotNil(t, adapter.sess)
}
func TestBegin_SessionStartError(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
// Mock session start failure
mt.AddMockResponses(mtest.CreateCommandErrorResponse(mtest.CommandError{}))
b := true
adapter := mongoAdapter{client: mt.Client, sesOption: &options.SessionOptions{
Snapshot: &b,
CausalConsistency: &b,
}}
err := adapter.Begin(context.Background())
assert.Error(t, err)
}
func TestBegin_TransactionStartError(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
// Mock transaction start failure
mt.AddMockResponses(mtest.CreateCommandErrorResponse(mtest.CommandError{}))
adapter := mongoAdapter{client: mt.Client, txOption: &options.TransactionOptions{
WriteConcern: writeconcern.Unacknowledged(),
}}
err := adapter.Begin(context.Background())
assert.Error(t, err)
}
func TestCommit_Success(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
mt.AddMockResponses(mtest.CreateSuccessResponse())
ses, _ := mt.Client.StartSession()
adapter := &mongoAdapter{
client: mt.Client,
sess: ses,
}
adapter.Begin(context.Background()) // Start transaction
err := adapter.Commit(context.Background())
assert.Nil(t, err)
}
func TestCommit_NoSession(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
adapter := &mongoAdapter{client: mt.Client} // No session set
err := adapter.Commit(context.Background())
assert.Nil(t, err)
}
func TestCommit_Error(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
ses, _ := mt.Client.StartSession()
adapter := &mongoAdapter{
client: mt.Client,
sess: ses,
}
mt.AddMockResponses(mtest.CreateWriteErrorsResponse(mtest.WriteError{})) // Simulate error
err := adapter.Commit(context.Background())
assert.Error(t, err)
}
func TestRollback_Success(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
mt.AddMockResponses(mtest.CreateSuccessResponse())
ses, _ := mt.Client.StartSession()
adapter := &mongoAdapter{
client: mt.Client,
sess: ses,
}
adapter.Begin(context.Background()) // Start transaction
err := adapter.Rollback(context.Background())
assert.Nil(t, err)
}
func TestRollback_NoSession(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
adapter := &mongoAdapter{client: mt.Client} // No session set
err := adapter.Rollback(context.Background())
assert.Nil(t, err)
}
func TestRollback_Error(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
ses, _ := mt.Client.StartSession()
adapter := &mongoAdapter{
client: mt.Client,
sess: ses,
}
mt.AddMockResponses(mtest.CreateWriteErrorsResponse(mtest.WriteError{})) // Simulate error
err := adapter.Rollback(context.Background())
assert.Error(t, err)
}
func TestEnd(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
// Mock session end
mt.AddMockResponses(mtest.CreateSuccessResponse())
ses, _ := mt.Client.StartSession()
adapter := &mongoAdapter{
client: mt.Client,
sess: ses,
}
adapter.End(context.Background())
// No assertions needed, as EndSession doesn't return an error in the mock
}
func TestEnd_NoSession(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
adapter := &mongoAdapter{client: mt.Client} // No session set
// Calling End with no session should not panic
assert.NotPanics(t, func() { adapter.End(context.Background()) })
}
func TestGetCurrent(t *testing.T) {
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock).ShareClient(true))
// Mock session end
mt.AddMockResponses(mtest.CreateSuccessResponse())
adapter := &mongoAdapter{
client: mt.Client,
}
adapter.Begin(context.Background())
assert.Equal(t, adapter.sessCtx, adapter.GetCurrent(context.Background()))
adapter.End(context.Background())
ctx := context.Background()
assert.Equal(t, ctx, adapter.GetCurrent(ctx))
}