forked from johnnadratowski/golang-neo4j-bolt-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_test.go
167 lines (134 loc) · 4.14 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
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
160
161
162
163
164
165
166
167
package golangNeo4jBoltDriver
import (
"github.com/johnnadratowski/golang-neo4j-bolt-driver/structures/graph"
"io"
"testing"
)
func TestBoltTx_Commit(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltTx_Commit", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
tx, err := conn.Begin()
if err != nil {
t.Fatalf("An error occurred beginning transaction: %s", err)
}
stmt, err := conn.PrepareNeo(`CREATE (f:FOO {a: "1"})-[b:TO]->(c:BAR)<-[d:FROM]-(e:BAZ) RETURN f, b, c, d, e`)
if err != nil {
t.Fatalf("An error occurred preparing statement: %s", err)
}
result, err := stmt.ExecNeo(nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
if num, err := result.RowsAffected(); num != 5 {
t.Fatalf("Expected 5 rows affected: %#v err: %#v", result.Metadata(), err)
}
err = tx.Commit()
if err != nil {
t.Fatalf("An error occurred committing transaction: %s", err)
}
err = stmt.Close()
if err != nil {
t.Fatalf("An error occurred closing statement")
}
stmt, err = conn.PrepareNeo(`MATCH (f:FOO {a: "1"})-[b:TO]->(c:BAR)<-[d:FROM]-(e:BAZ) RETURN f, b, c, d, e`)
if err != nil {
t.Fatalf("An error occurred preparing statement: %s", err)
}
rows, err := stmt.QueryNeo(nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
output, _, err := rows.NextNeo()
if err != nil {
t.Fatalf("An error occurred getting next row: %s", err)
}
if output[0].(graph.Node).Labels[0] != "FOO" {
t.Fatalf("Unexpected return data: %s", err)
}
if output[1].(graph.Relationship).Type != "TO" {
t.Fatalf("Unexpected return data: %s", err)
}
if output[2].(graph.Node).Labels[0] != "BAR" {
t.Fatalf("Unexpected return data: %s", err)
}
if output[3].(graph.Relationship).Type != "FROM" {
t.Fatalf("Unexpected return data: %s", err)
}
if output[4].(graph.Node).Labels[0] != "BAZ" {
t.Fatalf("Unexpected return data: %s", err)
}
// Closing in middle of record stream
stmt.Close()
stmt, err = conn.PrepareNeo(`MATCH (f:FOO)-[b:TO]->(c:BAR)<-[d:FROM]-(e:BAZ) DELETE f, b, c, d, e`)
if err != nil {
t.Fatalf("An error occurred preparing delete statement: %s", err)
}
_, err = stmt.ExecNeo(nil)
if err != nil {
t.Fatalf("An error occurred on delete query to Neo: %s", err)
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing connection: %s", err)
}
}
func TestBoltTx_Rollback(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltTx_Rollback", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
tx, err := conn.Begin()
if err != nil {
t.Fatalf("An error occurred beginning transaction: %s", err)
}
stmt, err := conn.PrepareNeo(`CREATE (f:FOO {a: "1"})-[b:TO]->(c:BAR)<-[d:FROM]-(e:BAZ) RETURN f, b, c, d, e`)
if err != nil {
t.Fatalf("An error occurred preparing statement: %s", err)
}
result, err := stmt.ExecNeo(nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
if num, err := result.RowsAffected(); num != 5 {
t.Fatalf("Expected 5 rows affected: %#v err: %#v", result.Metadata(), err)
}
err = tx.Rollback()
if err != nil {
t.Fatalf("An error occurred committing transaction: %s", err)
}
err = stmt.Close()
if err != nil {
t.Fatalf("An error occurred closing statement")
}
stmt, err = conn.PrepareNeo(`MATCH (f:FOO {a: "1"})-[b:TO]->(c:BAR)<-[d:FROM]-(e:BAZ) RETURN f, b, c, d, e`)
if err != nil {
t.Fatalf("An error occurred preparing statement: %s", err)
}
rows, err := stmt.QueryNeo(nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
output, _, err := rows.NextNeo()
if err != io.EOF {
t.Fatalf("Unexpected error returned from getting next rows: %s", err)
}
if len(output) != 0 {
t.Fatalf("Unexpected return data: %s", err)
}
err = stmt.Close()
if err != nil {
t.Fatalf("An error occurred closing statement")
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing connection: %s", err)
}
}