-
Notifications
You must be signed in to change notification settings - Fork 139
/
transaction_test.go
128 lines (92 loc) · 2.46 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
package storm
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestTransaction(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Rollback()
require.Error(t, err)
err = db.Commit()
require.Error(t, err)
tx, err := db.Begin(true)
require.NoError(t, err)
ntx, ok := tx.(*node)
require.True(t, ok)
require.NotNil(t, ntx.tx)
err = tx.Init(&SimpleUser{})
require.NoError(t, err)
err = tx.Save(&User{ID: 10, Name: "John"})
require.NoError(t, err)
err = tx.Save(&User{ID: 20, Name: "John"})
require.NoError(t, err)
err = tx.Save(&User{ID: 30, Name: "Steve"})
require.NoError(t, err)
var user User
err = tx.One("ID", 10, &user)
require.NoError(t, err)
var users []User
err = tx.AllByIndex("Name", &users)
require.NoError(t, err)
require.Len(t, users, 3)
err = tx.All(&users)
require.NoError(t, err)
require.Len(t, users, 3)
err = tx.Find("Name", "Steve", &users)
require.NoError(t, err)
require.Len(t, users, 1)
err = tx.DeleteStruct(&user)
require.NoError(t, err)
err = tx.One("ID", 10, &user)
require.Error(t, err)
err = tx.Set("b1", "best friend's mail", "[email protected]")
require.NoError(t, err)
var str string
err = tx.Get("b1", "best friend's mail", &str)
require.NoError(t, err)
require.Equal(t, "[email protected]", str)
err = tx.Delete("b1", "best friend's mail")
require.NoError(t, err)
err = tx.Get("b1", "best friend's mail", &str)
require.Error(t, err)
err = tx.Commit()
require.NoError(t, err)
err = tx.Commit()
require.Error(t, err)
require.Equal(t, ErrNotInTransaction, err)
err = db.One("ID", 30, &user)
require.NoError(t, err)
require.Equal(t, 30, user.ID)
}
func TestTransactionRollback(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
tx, err := db.Begin(true)
require.NoError(t, err)
err = tx.Save(&User{ID: 10, Name: "John"})
require.NoError(t, err)
var user User
err = tx.One("ID", 10, &user)
require.NoError(t, err)
require.Equal(t, 10, user.ID)
err = tx.Rollback()
require.NoError(t, err)
err = db.One("ID", 10, &user)
require.Error(t, err)
}
func TestTransactionNotWritable(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
err := db.Save(&User{ID: 10, Name: "John"})
require.NoError(t, err)
tx, err := db.Begin(false)
require.NoError(t, err)
err = tx.Save(&User{ID: 20, Name: "John"})
require.Error(t, err)
var user User
err = tx.One("ID", 10, &user)
require.NoError(t, err)
err = tx.Rollback()
require.NoError(t, err)
}