-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx_test.go
61 lines (49 loc) · 1.59 KB
/
tx_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
package anystore
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/anyproto/any-store/anyenc"
)
func TestDb_WriteTx(t *testing.T) {
t.Run("err other instance", func(t *testing.T) {
fx := newFixture(t)
fx2 := newFixture(t)
tx, err := fx2.WriteTx(ctx)
require.NoError(t, err)
defer tx.Rollback()
_, err = fx.CreateCollection(tx.Context(), "test")
assert.ErrorIs(t, err, ErrTxOtherInstance)
})
t.Run("err tx been used", func(t *testing.T) {
fx := newFixture(t)
tx, err := fx.WriteTx(ctx)
require.NoError(t, err)
require.NoError(t, tx.Commit())
_, err = fx.CreateCollection(tx.Context(), "test")
assert.ErrorIs(t, err, ErrTxIsUsed)
})
t.Run("rollback + savepoint rollback", func(t *testing.T) {
fx := newFixture(t)
coll, err := fx.CreateCollection(ctx, "test")
require.NoError(t, err)
require.NoError(t, coll.EnsureIndex(ctx, IndexInfo{Fields: []string{"a"}, Unique: true}))
tx, err := fx.WriteTx(ctx)
require.NoError(t, err)
require.NoError(t, coll.Insert(tx.Context(),
anyenc.MustParseJson(`{"id":1,"a":1}`),
anyenc.MustParseJson(`{"id":2,"a":2}`),
anyenc.MustParseJson(`{"id":3,"a":3}`),
))
assertCollCountCtx(tx.Context(), t, coll, 3)
// this insert will be failed and should rollback to savepoint
require.Error(t, coll.Insert(tx.Context(),
anyenc.MustParseJson(`{"id":4,"a":4}`),
anyenc.MustParseJson(`{"id":5,"a":5}`),
anyenc.MustParseJson(`{"id":6,"a":1}`),
))
assertCollCountCtx(tx.Context(), t, coll, 3)
require.NoError(t, tx.Rollback())
assertCollCount(t, coll, 0)
})
}