-
Notifications
You must be signed in to change notification settings - Fork 55
/
cypher_test.go
392 lines (378 loc) · 8.64 KB
/
cypher_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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neoism
import (
"fmt"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
// 18.3.1. Send queries with parameters
func TestCypherParameters(t *testing.T) {
var db *Database
db = connectTest(t)
defer cleanup(t, db)
// defer cleanup(t, db)
// Create
nameIdx, _ := db.CreateLegacyNodeIndex("name_index", "", "")
defer nameIdx.Delete()
floatIdx, _ := db.CreateLegacyNodeIndex("float_index", "", "")
defer floatIdx.Delete()
numIdx, _ := db.CreateLegacyNodeIndex("num_index", "", "")
defer numIdx.Delete()
n0, _ := db.CreateNode(Props{"name": "I"})
nameIdx.Add(n0, "name", "I")
n1, _ := db.CreateNode(Props{"name": "you"})
n2, _ := db.CreateNode(Props{"name": "num", "num": 42})
numIdx.Add(n2, "num", 42)
n3, _ := db.CreateNode(Props{"name": "float", "float": 3.14})
floatIdx.Add(n3, "float", 3.14)
r0, _ := n0.Relate("knows", n1.Id(), nil)
r1, _ := n0.Relate("loves", n1.Id(), nil)
n0.Relate("understands", n2.Id(), nil)
//
// Query with string parameters and integer results
//
type resultStruct0 struct {
N int `json:"id(n)"`
R int `json:"id(r)"`
M int `json:"id(m)"`
}
result0 := []resultStruct0{}
cq := CypherQuery{
Statement: `
START n = node:name_index(name={startName})
MATCH path = (n)-[r]->(m)
WHERE m.name = {name}
RETURN id(n), id(r), id(m)
ORDER by id(n), id(r), id(m)
`,
Parameters: map[string]interface{}{
"startName": "I",
"name": "you",
},
Result: &result0,
}
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
// Check result
expCol := []string{"id(n)", "id(r)", "id(m)"}
expDat0 := []resultStruct0{
resultStruct0{n0.Id(), r0.Id(), n1.Id()},
resultStruct0{n0.Id(), r1.Id(), n1.Id()},
}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat0, result0)
//
// Query with integer parameter and string results
//
type resultStruct1 struct {
Name string `json:"n.name"`
}
result1 := []resultStruct1{}
cq = CypherQuery{
Statement: `
START n = node:num_index(num={num})
RETURN n.name
`,
Parameters: map[string]interface{}{
"num": 42,
},
Result: &result1,
}
err = db.Cypher(&cq)
if err != nil {
t.Error(err)
}
expCol = []string{"n.name"}
expDat1 := []resultStruct1{resultStruct1{Name: "num"}}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat1, result1)
//
// Query with float parameter
//
result2 := []resultStruct1{}
cq = CypherQuery{
Statement: `
START n = node:float_index(float={float})
RETURN n.name
`,
Parameters: map[string]interface{}{
"float": 3.14,
},
Result: &result2,
}
err = db.Cypher(&cq)
if err != nil {
t.Error(err)
}
expCol = []string{"n.name"}
expDat2 := []resultStruct1{resultStruct1{Name: "float"}}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat2, result2)
//
// Query with array parameter
//
result3 := []resultStruct1{}
cq = CypherQuery{
Statement: `
START n=node(*)
WHERE id(n) IN {arr}
RETURN n.name
ORDER BY id(n)
`,
Parameters: map[string]interface{}{
"arr": []int{n0.Id(), n1.Id()},
},
Result: &result3,
}
err = db.Cypher(&cq)
if err != nil {
t.Error(err)
}
expCol = []string{"n.name"}
expDat3 := []resultStruct1{
resultStruct1{Name: "I"},
resultStruct1{Name: "you"},
}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat3, result3)
}
// 18.3.2. Send a Query
func TestCypher(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
idx0, _ := db.CreateLegacyNodeIndex("name_index", "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{"name": "I"})
idx0.Add(n0, "name", "I")
n1, _ := db.CreateNode(Props{"name": "you", "age": 69})
n0.Relate("know", n1.Id(), nil)
// Query
// query := "START x = node:name_index(name=I) MATCH path = (x-[r]-friend) WHERE friend.name = you RETURN TYPE(r)"
type resultStruct struct {
Type string `json:"type(r)"`
Name string `json:"n.name"`
Age int `json:"n.age"`
}
result := []resultStruct{}
cq := CypherQuery{
Statement: "start x = node(" + strconv.Itoa(n0.Id()) + ") match x -[r]-> n return type(r), n.name, n.age",
Result: &result,
}
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
// Check result
//
// Our test only passes if Neo4j returns columns in the expected order - is
// there any guarantee about order?
expCol := []string{"type(r)", "n.name", "n.age"}
expDat := []resultStruct{
resultStruct{
Type: "know",
Name: "you",
Age: 69,
},
}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat, result)
}
// Test multi-line Cypher query with embedded comments.
func TestCypherComment(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
idx0, _ := db.CreateLegacyNodeIndex("name_index", "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{"name": "I"})
idx0.Add(n0, "name", "I")
n1, _ := db.CreateNode(Props{"name": "you", "age": 69})
n0.Relate("know", n1.Id(), nil)
// Query
// query := "START x = node:name_index(name=I) MATCH path = (x-[r]-friend) WHERE friend.name = you RETURN TYPE(r)"
type resultStruct struct {
Type string `json:"type(r)"`
Name string `json:"n.name"`
Age int `json:"n.age"`
}
result := []resultStruct{}
stmt := `
START x = NODE(%d)
// This is a comment
MATCH x -[r]-> n
// This is another comment
RETURN TYPE(r), n.name, n.age
`
stmt = fmt.Sprintf(stmt, n0.Id())
cq := CypherQuery{
Statement: stmt,
Result: &result,
}
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
// Check result
//
// Our test only passes if Neo4j returns columns in the expected order - is
// there any guarantee about order?
expCol := []string{"TYPE(r)", "n.name", "n.age"}
expDat := []resultStruct{
resultStruct{
Type: "know",
Name: "you",
Age: 69,
},
}
assert.Equal(t, expCol, cq.Columns())
assert.Equal(t, expDat, result)
}
func TestCypherBadQuery(t *testing.T) {
db := connectTest(t)
cq := CypherQuery{
Statement: "foobar",
}
err := db.Cypher(&cq)
ne, ok := err.(NeoError)
if !ok {
t.Error(err)
}
s := ne.Error()
assert.NotEqual(t, "", s)
}
func TestCypherStats(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
cq := CypherQuery{
Statement: `
CREATE (n:Person)
`,
IncludeStats: true,
}
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
stats, err := cq.Stats()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, Stats{ContainsUpdates: true, LabelsAdded: 1, NodesCreated: 1}, *stats)
}
func TestCypherNoStats(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
cq := CypherQuery{
Statement: `
CREATE (n:Person)
`,
IncludeStats: false,
}
err := db.Cypher(&cq)
if err != nil {
t.Error(err)
}
_, err = cq.Stats()
if err == nil {
t.Fatal("Stats not requested - expected an error")
}
}
func TestCypherBatch(t *testing.T) {
db := connectTest(t)
type resultStruct0 struct {
N Node
}
type resultStruct2 struct {
R Relationship
}
r0 := []resultStruct0{}
r1 := []resultStruct0{}
r2 := []resultStruct2{}
// n0 := []interface{}{}
qs := []*CypherQuery{
&CypherQuery{
Statement: `CREATE (n:Person {name: "Mr Spock"}) RETURN n`,
Result: &r0,
},
&CypherQuery{
Statement: `CREATE (n:Person {name: "Mr Sulu"}) RETURN n`,
Result: &r1,
},
&CypherQuery{
Statement: `
MATCH (a:Person), (b:Person)
WHERE a.name = 'Mr Spock' AND b.name = 'Mr Sulu'
CREATE a-[r:Knows]->b
RETURN r
`,
Result: &r2,
},
}
err := db.CypherBatch(qs)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "Mr Spock", r0[0].N.Data["name"])
assert.Equal(t, "Mr Sulu", r1[0].N.Data["name"])
assert.Equal(t, "Knows", r2[0].R.Type)
//
// Cleanup
//
for _, r := range r2 {
r.R.Db = db
err = r.R.Delete()
if err != nil {
t.Error(err)
}
}
for _, n := range r0 {
n.N.Db = db
err = n.N.Delete()
if err != nil {
t.Error(err)
}
}
for _, n := range r1 {
n.N.Db = db
err = n.N.Delete()
if err != nil {
t.Error(err)
}
}
}
func TestCypherBadBatch(t *testing.T) {
db := connectTest(t)
cq := CypherQuery{
Statement: "foobar",
}
qs := []*CypherQuery{&cq}
err := db.CypherBatch(qs)
if _, ok := err.(NeoError); !ok {
t.Error(err)
}
}
func TestCypherBatchStats(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
qs := []*CypherQuery{
&CypherQuery{
Statement: `CREATE (n:Person)`,
IncludeStats: true,
},
}
err := db.CypherBatch(qs)
if err != nil {
t.Error(err)
}
stats, err := qs[0].Stats()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, Stats{ContainsUpdates: true, LabelsAdded: 1, NodesCreated: 1}, *stats)
}