-
Notifications
You must be signed in to change notification settings - Fork 72
/
conn_test.go
311 lines (254 loc) · 8.58 KB
/
conn_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package golangNeo4jBoltDriver
import (
"io"
"reflect"
"testing"
"github.com/johnnadratowski/golang-neo4j-bolt-driver/errors"
"github.com/johnnadratowski/golang-neo4j-bolt-driver/structures/messages"
)
func TestBoltConn_parseURL(t *testing.T) {
c := &boltConn{connStr: "http://foo:7687"}
_, err := c.parseURL()
if err == nil {
t.Fatal("Expected error from incorrect protocol")
}
c = &boltConn{connStr: "bolt://john@foo:7687"}
_, err = c.parseURL()
if err == nil {
t.Fatal("Expected error from missing password")
}
c = &boltConn{connStr: "bolt://john:password@foo:7687"}
_, err = c.parseURL()
if err != nil {
t.Fatal("Should not error on valid url")
}
if c.user != "john" {
t.Fatal("Expected user to be 'john'")
}
if c.password != "password" {
t.Fatal("Expected password to be 'password'")
}
c = &boltConn{connStr: "bolt://john:password@foo:7687?tls=true"}
_, err = c.parseURL()
if err != nil {
t.Fatal("Should not error on valid url")
}
if !c.useTLS {
t.Fatal("Expected to use TLS")
}
c = &boltConn{connStr: "bolt://john:password@foo:7687?tls=true&tls_no_verify=1&tls_ca_cert_file=ca&tls_cert_file=cert&tls_key_file=key"}
_, err = c.parseURL()
if err != nil {
t.Fatal("Should not error on valid url")
}
if !c.useTLS {
t.Fatal("Expected to use TLS")
}
if !c.tlsNoVerify {
t.Fatal("Expected to use TLS with no verification")
}
if c.caCertFile != "ca" {
t.Fatal("Expected ca cert file 'ca'")
}
if c.certFile != "cert" {
t.Fatal("Expected cert file 'cert'")
}
if c.keyFile != "key" {
t.Fatal("Expected key file 'key'")
}
}
func TestBoltConn_Close(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_Close", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
err = conn.Close()
if err != nil {
t.Fatalf("An error occurred closing conn: %s", err)
}
if !conn.(*boltConn).closed {
t.Error("Conn not closed at end of test")
}
}
func TestBoltConn_SelectOne(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_SelectOne", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
rows, err := conn.QueryNeo("RETURN 1;", nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
expectedMetadata := map[string]interface{}{
"fields": []interface{}{"1"},
}
resultingMetadata := make(map[string]interface{})
for key, value := range rows.Metadata() {
if key != "result_available_after" {
resultingMetadata[key] = value
}
}
if !reflect.DeepEqual(resultingMetadata, expectedMetadata) {
t.Fatalf("Unexpected success metadata. Expected %#v. Got: %#v", expectedMetadata, rows.Metadata())
} else if _, ok := rows.Metadata()["result_available_after"]; !ok {
t.Fatalf("Unexpected success metadata. Expected the key 'result_available_after'. Got: %#v", rows.Metadata())
}
output, _, err := rows.NextNeo()
if err != nil {
t.Fatalf("An error occurred getting next row: %s", err)
}
if output[0].(int64) != 1 {
t.Fatalf("Unexpected output. Expected 1. Got: %d", output)
}
_, metadata, err := rows.NextNeo()
expectedMetadata = map[string]interface{}{
"type": "r",
}
resultingMetadata = make(map[string]interface{})
for key, value := range metadata {
if key != "result_consumed_after" {
resultingMetadata[key] = value
}
}
if err != io.EOF {
t.Fatalf("Unexpected row closed output. Expected io.EOF. Got: %s", err)
} else if !reflect.DeepEqual(resultingMetadata, expectedMetadata) {
t.Fatalf("Metadata didn't match expected. Expected %#v. Got: %#v", expectedMetadata, metadata)
} else if _, ok := metadata["result_consumed_after"]; !ok {
t.Fatalf("Metadata didn't match expected. Expected the key 'result_consumed_after'. Got: %#v", rows.Metadata())
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing connection: %s", err)
}
}
func TestBoltConn_SelectAll(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_SelectAll", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
results, err := conn.ExecNeo("CREATE (f:NODE {a: 1}), (b:NODE {a: 2})", nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
affected, err := results.RowsAffected()
if err != nil {
t.Fatalf("An error occurred getting rows affected: %s", err)
}
if affected != int64(2) {
t.Fatalf("Incorrect number of rows affected: %d", affected)
}
data, rowMetadata, metadata, err := conn.QueryNeoAll("MATCH (n:NODE) RETURN n.a ORDER BY n.a", nil)
if data[0][0] != int64(1) {
t.Fatalf("Incorrect data returned for first row: %#v", data[0])
}
if data[1][0] != int64(2) {
t.Fatalf("Incorrect data returned for second row: %#v", data[1])
}
if rowMetadata["fields"].([]interface{})[0] != "n.a" {
t.Fatalf("Unexpected column metadata: %#v", rowMetadata)
}
if metadata["type"].(string) != "r" {
t.Fatalf("Unexpected request metadata: %#v", metadata)
}
results, err = conn.ExecNeo("MATCH (n:NODE) DELETE n", nil)
if err != nil {
t.Fatalf("An error occurred querying Neo: %s", err)
}
affected, err = results.RowsAffected()
if err != nil {
t.Fatalf("An error occurred getting rows affected: %s", err)
}
if affected != int64(2) {
t.Fatalf("Incorrect number of rows affected: %d", affected)
}
err = conn.Close()
if err != nil {
t.Fatalf("Error closing connection: %s", err)
}
}
func TestBoltConn_Ignored(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_Ignored", neo4jConnStr)
conn, _ := driver.OpenNeo(neo4jConnStr)
defer conn.Close()
// This will make two calls at once - Run and Pull All. The pull all should be ignored, which is what
// we're testing.
_, err := conn.ExecNeo("syntax error", map[string]interface{}{"foo": 1, "bar": 2.2})
if err == nil {
t.Fatal("Expected an error on syntax error.")
}
data, _, _, err := conn.QueryNeoAll("RETURN 1;", nil)
if err != nil {
t.Fatalf("Got error when running next query after a failure: %#v", err)
}
if data[0][0].(int64) != 1 {
t.Fatalf("Expected different data from output: %#v", data)
}
}
func TestBoltConn_IgnoredPipeline(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_IgnoredPipeline", neo4jConnStr)
conn, _ := driver.OpenNeo(neo4jConnStr)
defer conn.Close()
// This will make two calls at once - Run and Pull All. The pull all should be ignored, which is what
// we're testing.
_, err := conn.ExecPipeline([]string{"syntax error", "syntax error", "syntax error"}, nil)
if err == nil {
t.Fatal("Expected an error on syntax error.")
}
data, _, _, err := conn.QueryNeoAll("RETURN 1;", nil)
if err != nil {
t.Fatalf("Got error when running next query after a failure: %#v", err)
}
if data[0][0].(int64) != 1 {
t.Fatalf("Expected different data from output: %#v", data)
}
}
func TestBoltConn_FailureMessageError(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_FailureMessageError", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
defer conn.Close()
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
_, err = conn.ExecNeo("THIS IS A BAD QUERY AND SHOULD RETURN A FAILURE MESSAGE", nil)
if err == nil {
t.Fatal("This should have returned a failure message error, but got a nil error")
}
code := "Neo.ClientError.Statement.SyntaxError"
if err.(*errors.Error).InnerMost().(messages.FailureMessage).Metadata["code"] != code {
t.Fatalf("Expected error message code %s, but got %v", code, err.(*errors.Error).InnerMost().(messages.FailureMessage).Metadata["code"])
}
}
func TestBoltConn_CloseStatementOnError(t *testing.T) {
driver := NewDriver()
// Records session for testing
driver.(*boltDriver).recorder = newRecorder("TestBoltConn_CloseStatementOnError", neo4jConnStr)
conn, err := driver.OpenNeo(neo4jConnStr)
defer conn.Close()
if err != nil {
t.Fatalf("An error occurred opening conn: %s", err)
}
_, err = conn.QueryNeo("THIS IS A BAD QUERY AND SHOULD RETURN A FAILURE MESSAGE", nil)
if err == nil {
t.Fatal("This should have returned a failure message error, but got a nil error")
}
_, err = conn.QueryNeo("RETURN 1;", nil)
if err != nil {
t.Fatalf("Got error when running next query after a failure: %#v", err)
}
}