-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction_test.go
159 lines (126 loc) · 3.76 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
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
package mgod_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/Lyearn/mgod"
"github.com/Lyearn/mgod/schema/schemaopt"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type TransactionSuite struct {
suite.Suite
*require.Assertions
}
type transactionTestUser struct {
Name string
EmailID string `bson:"emailId"`
}
func TestTransactionSuite(t *testing.T) {
s := new(TransactionSuite)
suite.Run(t, s)
}
func (s *TransactionSuite) SetupSuite() {
s.setupConnection()
}
func (s *TransactionSuite) SetupTest() {
s.Assertions = require.New(s.T())
}
func (s *TransactionSuite) setupConnection() {
cfg := &mgod.ConnectionConfig{Timeout: 5 * time.Second}
// Can use the `mlaunch` tool to start a local replica set using command `mlaunch --repl`.
uri := "mongodb://localhost:27017/?replicaSet=replset&authSource=admin"
err := mgod.ConfigureDefaultClient(cfg, options.Client().ApplyURI(uri))
if err != nil {
s.T().Fatal(err)
}
}
func (s *TransactionSuite) getModelForDB(dbName string) mgod.EntityMongoModel[transactionTestUser] {
schemaOpts := schemaopt.SchemaOptions{Timestamps: true}
opts := mgod.NewEntityMongoModelOptions(dbName, "users", &schemaOpts)
userModel, err := mgod.NewEntityMongoModel(transactionTestUser{}, *opts)
if err != nil {
s.T().Fatal(err)
}
return userModel
}
func (s *TransactionSuite) TestWithTransaction() {
userModel := s.getModelForDB("mgod1")
userDoc := transactionTestUser{Name: "Gopher", EmailID: "[email protected]"}
p, err := mgod.WithTransaction(context.Background(), func(sc mongo.SessionContext) (interface{}, error) {
_, err := userModel.InsertOne(sc, userDoc)
if err != nil {
return nil, err
}
userCount, err := userModel.CountDocuments(sc, bson.M{})
if err != nil {
return nil, err
}
_, err = userModel.DeleteOne(sc, bson.M{})
if err != nil {
return nil, err
}
return userCount, nil
})
userCount, ok := p.(int64)
s.NoError(err)
s.True(ok)
s.Equal(userCount, int64(1))
}
func (s *TransactionSuite) TestWithTransactionAbort() {
userModel := s.getModelForDB("mgod1")
userDoc := transactionTestUser{Name: "Gopher", EmailID: "[email protected]"}
abortErr := errors.New("dummy error to abort transaction")
_, err := mgod.WithTransaction(context.Background(), func(sc mongo.SessionContext) (interface{}, error) {
_, err := userModel.InsertOne(sc, userDoc)
if err != nil {
return nil, err
}
return nil, abortErr
})
s.EqualError(err, abortErr.Error())
userCount, err := userModel.CountDocuments(context.Background(), bson.M{})
s.NoError(err)
s.Equal(userCount, int64(0))
}
func (s *TransactionSuite) TestWithTransactionForMultiTenancy() {
userModelTenant1 := s.getModelForDB("mgod1")
userModelTenant2 := s.getModelForDB("mgod2")
userDoc := transactionTestUser{Name: "Gopher", EmailID: "[email protected]"}
p, err := mgod.WithTransaction(context.Background(), func(sc mongo.SessionContext) (interface{}, error) {
_, err := userModelTenant1.InsertOne(sc, userDoc)
if err != nil {
return nil, err
}
_, err = userModelTenant2.InsertOne(sc, userDoc)
if err != nil {
return nil, err
}
userCount1, err := userModelTenant1.CountDocuments(sc, bson.M{})
if err != nil {
return nil, err
}
userCount2, err := userModelTenant2.CountDocuments(sc, bson.M{})
if err != nil {
return nil, err
}
_, err = userModelTenant1.DeleteOne(sc, bson.M{})
if err != nil {
return nil, err
}
_, err = userModelTenant2.DeleteOne(sc, bson.M{})
if err != nil {
return nil, err
}
return fmt.Sprintf("%d%d", userCount1, userCount2), nil
})
userCountStr, ok := p.(string)
s.NoError(err)
s.True(ok)
s.Equal(userCountStr, "11")
}