-
Notifications
You must be signed in to change notification settings - Fork 60
/
transaction_test.go
86 lines (61 loc) · 1.9 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
package mgm_test
import (
"github.com/kamva/mgm/v3"
"github.com/kamva/mgm/v3/internal/util"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"testing"
)
// Note: to run Transaction tests, the MongoDB daemon must run as replica set, not as a standalone daemon.
// To convert it [see this](https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/)
func TestTransactionCommit(t *testing.T) {
setupDefConnection()
resetCollection()
d := &Doc{Name: "check", Age: 10}
err := mgm.Transaction(func(session mongo.Session, sc mongo.SessionContext) error {
err := mgm.Coll(d).CreateWithCtx(sc, d)
if err != nil {
return err
}
return session.CommitTransaction(sc)
})
util.AssertErrIsNil(t, err)
count, err := mgm.Coll(d).CountDocuments(mgm.Ctx(), bson.M{})
util.AssertErrIsNil(t, err)
require.Equal(t, int64(1), count)
}
func TestTransactionAbort(t *testing.T) {
setupDefConnection()
resetCollection()
//seed()
d := &Doc{Name: "check", Age: 10}
err := mgm.Transaction(func(session mongo.Session, sc mongo.SessionContext) error {
err := mgm.Coll(d).CreateWithCtx(sc, d)
if err != nil {
return err
}
return session.AbortTransaction(sc)
})
util.AssertErrIsNil(t, err)
count, err := mgm.Coll(d).CountDocuments(mgm.Ctx(), bson.M{})
util.AssertErrIsNil(t, err)
require.Equal(t, int64(0), count)
}
func TestTransactionWithCtx(t *testing.T) {
setupDefConnection()
resetCollection()
//seed()
d := &Doc{Name: "check", Age: 10}
err := mgm.TransactionWithCtx(mgm.Ctx(), func(session mongo.Session, sc mongo.SessionContext) error {
err := mgm.Coll(d).CreateWithCtx(sc, d)
if err != nil {
return err
}
return session.AbortTransaction(sc)
})
util.AssertErrIsNil(t, err)
count, err := mgm.Coll(d).CountDocuments(mgm.Ctx(), bson.M{})
util.AssertErrIsNil(t, err)
require.Equal(t, int64(0), count)
}