forked from freeeve/cq
-
Notifications
You must be signed in to change notification settings - Fork 11
/
driver_trans_test.go
152 lines (127 loc) · 3.23 KB
/
driver_trans_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
package cq
import (
"log"
"time"
. "gopkg.in/check.v1"
// _ "gopkg.in/cq.v1"
)
// This file is meant to hold integration tests where cq must be imported as _
// and is for testing transactions. Some of these are going to be long tests...
type TransactionSuite struct{}
var _ = Suite(&TransactionSuite{})
func clearTestRecords() {
db := testConn()
_, err := db.Exec("match (n:`TestRollback~~~~`) delete n")
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("match (n:`TestCommit~~~~`) delete n")
if err != nil {
log.Fatal(err)
}
}
func (s *TransactionSuite) SetUpTest(c *C) {
clearTestRecords()
}
func (s *TransactionSuite) TearDownTest(c *C) {
clearTestRecords()
}
func (s *TransactionSuite) TestTransactionRollback1(c *C) {
testTransactionRollbackN(c, 1)
}
func (s *TransactionSuite) TestTransactionRollback7(c *C) {
testTransactionRollbackN(c, 7)
}
func (s *TransactionSuite) TestTransactionRollback100(c *C) {
testTransactionRollbackN(c, 100)
}
func (s *TransactionSuite) TestTransactionRollback1000(c *C) {
testTransactionRollbackN(c, 1000)
}
func (s *TransactionSuite) TestTransactionRollback7777(c *C) {
testTransactionRollbackN(c, 7777)
}
func testTransactionRollbackN(c *C, n int) {
db := testConn()
tx, err := db.Begin()
c.Assert(err, IsNil)
for i := 0; i < n; i++ {
_, err := tx.Exec("create (:`TestRollback~~~~`)")
c.Assert(err, IsNil)
}
err = tx.Rollback()
c.Assert(err, IsNil)
rows, err := db.Query("match (n:`TestRollback~~~~`) return count(1)")
c.Assert(err, IsNil)
defer rows.Close()
rows.Next()
var count int
err = rows.Scan(&count)
c.Assert(err, IsNil)
if count > 0 {
c.Fatal("rollback doesn't work")
}
}
func (s *TransactionSuite) TestTransactionExecCommit1(c *C) {
testTransactionExecCommitN(c, 1, 0)
}
func (s *TransactionSuite) TestTransactionExecCommit77(c *C) {
testTransactionExecCommitN(c, 77, 0)
}
func (s *TransactionSuite) TestTransactionExecCommit100(c *C) {
testTransactionExecCommitN(c, 100, 0)
}
func (s *TransactionSuite) TestTransactionExecCommit1000(c *C) {
testTransactionExecCommitN(c, 1000, 0)
}
func (s *TransactionSuite) TestTransactionExecCommit7777(c *C) {
testTransactionExecCommitN(c, 7777, 0)
}
func (s *TransactionSuite) TestTransactionExecCommit1Rec2Secs(c *C) {
testTransactionExecCommitN(c, 1, 2*time.Second)
}
func testTransactionExecCommitN(c *C, n int, delay time.Duration) {
db := testConn()
tx, err := db.Begin()
c.Assert(err, IsNil)
for i := 0; i < n; i++ {
_, err := tx.Exec("create (:`TestCommit~~~~` {id:{0}})", i)
c.Assert(err, IsNil)
}
time.Sleep(delay)
err = tx.Commit()
c.Assert(err, IsNil)
rows, err := db.Query("match (n:`TestCommit~~~~`) return n.id order by n.id")
c.Assert(err, IsNil)
defer rows.Close()
var count int
var i int
for rows.Next() {
if i > n {
c.Fatal("i shouldn't be > n")
}
err = rows.Scan(&count)
c.Assert(err, IsNil)
i++
}
c.Assert(i, Equals, n)
}
func (s *TransactionSuite) TestTxBadCypherError(c *C) {
db := testConn()
tx, err := db.Begin()
c.Assert(err, IsNil)
for i := 0; i < 1000; i++ {
_, err := tx.Exec("create (:`TestCommit~~~~` {id:{0}})")
if i%100 != 99 {
c.Assert(err, IsNil)
} else {
if err == nil {
c.Fatal(err)
}
}
}
err = tx.Commit()
if err == nil {
c.Fatal(err)
}
}