forked from jmcvetta/neoism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
449 lines (420 loc) · 10.2 KB
/
node_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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// 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.
//
// The Neo4j Manual section numbers quoted herein refer to the manual for
// milestone release 1.8. http://docs.neo4j.org/chunked/1.8/
package neoism
import (
"github.com/jmcvetta/randutil"
"github.com/stretchr/testify/assert"
"testing"
)
// 18.4.1. Create Node
func TestCreateNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
n0, err := db.CreateNode(nil)
if err != nil {
t.Error(err)
}
// Confirm creation
_, err = db.Node(n0.Id())
if err != nil {
t.Error(err)
}
//
// Bad Href
//
db1 := connectTest(t)
db1.HrefNode = db1.HrefNode + "foobar"
_, err = db1.CreateNode(nil)
assert.Equal(t, NotFound, err)
}
// 18.4.2. Create Node with properties
func TestCreateNodeWithProperties(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
props0 := Props{}
props0["foo"] = "bar"
props0["spam"] = "eggs"
n0, err := db.CreateNode(props0)
if err != nil {
t.Error(err)
}
// Confirm creation
_, err = db.Node(n0.Id())
if err != nil {
t.Error(err)
}
// Confirm properties
props1, _ := n0.Properties()
assert.Equal(t, props0, props1, "Node properties not as expected")
}
// 18.4.2. Create Node with properties
func TestGetOrCreateNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
label, err := randutil.String(12, randutil.Alphabet)
if err != nil {
t.Fatal(err)
}
key, err := randutil.String(12, randutil.Alphabet)
if err != nil {
t.Fatal(err)
}
value, err := randutil.String(12, randutil.Alphabet)
if err != nil {
t.Fatal(err)
}
p0 := Props{key: value, "foo": "bar"}
p1 := Props{key: value}
p2 := Props{"foo": "bar"}
//
// Create unique node
//
n0, created, err := db.GetOrCreateNode(label, key, p0)
if err != nil {
t.Fatal(err)
}
if !created {
t.Fatal("Failed to create unique node")
}
check0, err := n0.Properties()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, p0, check0)
//
// Get unique node
//
n1, created, err := db.GetOrCreateNode(label, key, p1)
if err != nil {
t.Fatal(err)
}
if created {
t.Fatal("Failed to retrieve unique node")
}
check1, err := n1.Properties()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, p0, check1)
//
// No key in props
//
_, _, err = db.GetOrCreateNode(label, key, p2)
assert.NotEqual(t, nil, err)
//
// Empty label
//
_, _, err = db.GetOrCreateNode("", key, p0)
assert.NotEqual(t, nil, err)
}
// 18.4.3. Get node
func TestGetNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
n0, _ := db.CreateNode(Props{})
// Get Node
n1, err := db.Node(n0.Id())
if err != nil {
t.Error(err)
}
// Confirm nodes are the same
assert.Equal(t, n0.Id(), n1.Id(), "Nodes do not have same ID")
}
// 18.4.4. Get non-existent node
func TestGetNonexistentNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create a node
n0, _ := db.CreateNode(Props{})
// Try to get non-existent node with next Id
implausible := n0.Id() + 1000
_, err := db.Node(implausible)
assert.Equal(t, err, NotFound)
}
// 18.4.5. Delete node
func TestDeleteNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create then delete a node
n0, _ := db.CreateNode(Props{})
id := n0.Id()
err := n0.Delete()
if err != nil {
t.Error(err)
}
// Check that node is no longer in db
_, err = db.Node(id)
assert.Equal(t, err, NotFound)
//
// Delete non-existent node
//
err = n0.Delete()
assert.Equal(t, NotFound, err)
}
// 18.4.6. Nodes with relationships can not be deleted;
func TestDeleteNodeWithRelationships(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
n0, _ := db.CreateNode(Props{})
n1, _ := db.CreateNode(Props{})
n0.Relate("knows", n1.Id(), Props{})
// Attempt to delete node without deleting relationship
err := n0.Delete()
assert.Equal(t, err, CannotDelete, "Should not be possible to delete node with relationship.")
}
// 18.7.1. Set property on node
func TestSetPropertyOnNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
n0, _ := db.CreateNode(Props{})
key := rndStr(t)
value := rndStr(t)
err := n0.SetProperty(key, value)
if err != nil {
t.Error(err)
}
// Confirm
props, _ := n0.Properties()
checkVal, present := props[key]
assert.True(t, present, "Expected property key not found")
assert.True(t, checkVal == value, "Expected property value not found")
}
// 18.7.1. Set property on node
func TestSetBadPropertyOnNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(Props{})
key := ""
value := rndStr(t)
err := n0.SetProperty(key, value)
if _, ok := err.(NeoError); !ok {
t.Fatal(err)
}
}
// 18.7.2. Update node properties
func TestUpdatePropertyOnNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
props0 := Props{rndStr(t): rndStr(t)}
props1 := Props{rndStr(t): rndStr(t)}
n0, _ := db.CreateNode(props0)
// Update
err := n0.SetProperties(props1)
if err != nil {
t.Error(err)
}
// Confirm
checkProps, _ := n0.Properties()
assert.Equal(t, props1, checkProps, "Did not recover expected properties after updating with SetProperties().")
}
// 18.7.3. Get properties for node
func TestGetPropertiesForNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
props := Props{rndStr(t): rndStr(t)}
n0, _ := db.CreateNode(props)
// Get properties & confirm
checkProps, err := n0.Properties()
if err != nil {
t.Error(err)
}
assert.Equal(t, props, checkProps, "Did not return expected properties.")
}
//
// 18.7.4. Property values can not be null
//
// This section cannot be tested. Properties - which is a map[string]string -
// cannot be instantiated with a nil value. If you try, the code will not compile.
//
//
// 18.7.5. Property values can not be nested
//
// This section cannot be tested. Properties is defined as map[string]string -
// only strings may be used as values. If you try to create a nested
// Properties, the code will not compile.
//
// 18.7.6. Delete all properties from node
func TestDeleteAllPropertiesFromNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
props := Props{
rndStr(t): rndStr(t),
rndStr(t): rndStr(t),
}
n0, _ := db.CreateNode(props)
// Delete properties
err := n0.DeleteProperties()
if err != nil {
t.Error(err)
}
// Confirm deletion
checkProps, _ := n0.Properties()
assert.Equal(t, Props{}, checkProps, "Properties should be empty after call to DeleteProperties()")
n0.Delete()
err = n0.DeleteProperties()
assert.Equal(t, NotFound, err)
}
// 18.7.7. Delete a named property from a node
func TestDeleteNamedPropertyFromNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
props0 := Props{"foo": "bar"}
props1 := Props{"foo": "bar", "spam": "eggs"}
n0, _ := db.CreateNode(props1)
// Delete
err := n0.DeleteProperty("spam")
if err != nil {
t.Error(err)
}
// Confirm
checkProps, _ := n0.Properties()
assert.Equal(t, props0, checkProps, "Failed to remove named property with DeleteProperty().")
//
// Delete non-existent property
//
err = n0.DeleteProperty("eggs")
assert.NotEqual(t, nil, err)
//
// Delete and check 404
//
n0.Delete()
err = n0.DeleteProperty("spam")
assert.Equal(t, NotFound, err)
}
func TestNodeProperty(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
props := Props{"foo": "bar"}
n0, _ := db.CreateNode(props)
value, err := n0.Property("foo")
if err != nil {
t.Error(err)
}
assert.Equal(t, value, "bar", "Incorrect value when getting single property.")
//
// Check Not Found
//
n0.Delete()
_, err = n0.Property("foo")
assert.Equal(t, NotFound, err)
}
func TestAddLabels(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(nil)
labels, err := n0.Labels()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []string{}, labels)
newLabels := []string{"Person", "Bicyclist"}
err = n0.AddLabel(newLabels...)
if err != nil {
t.Fatal(err)
}
labels, err = n0.Labels()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, newLabels, labels)
}
func TestLabelsInvalidNode(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(nil)
n0.Delete()
err := n0.AddLabel("foobar")
assert.Equal(t, NotFound, err)
_, err = n0.Labels()
assert.Equal(t, NotFound, err)
}
func TestRemoveLabel(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(nil)
n0.AddLabel("foobar")
labels, _ := n0.Labels()
assert.Equal(t, []string{"foobar"}, labels)
err := n0.RemoveLabel("foobar")
if err != nil {
t.Fatal(err)
}
labels, _ = n0.Labels()
assert.Equal(t, []string{}, labels)
n0.Delete()
err = n0.RemoveLabel("foobar")
assert.Equal(t, NotFound, err)
}
func TestAddLabelInvalidName(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(nil)
err := n0.AddLabel("") // Blank string is invalid label name
if _, ok := err.(NeoError); !ok {
t.Fatal(err)
}
}
func TestSetLabels(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
n0, _ := db.CreateNode(nil)
n0.AddLabel("spam", "eggs")
err := n0.SetLabels([]string{"foobar"})
if err != nil {
t.Fatal(err)
}
labels, _ := n0.Labels()
assert.Equal(t, []string{"foobar"}, labels)
n0.Delete()
err = n0.SetLabels([]string{"foobar"})
assert.Equal(t, NotFound, err)
}
func TestNodesByLabel(t *testing.T) {
db := connectTest(t)
cleanup(t, db) // Make sure no nodes exist before we start
defer cleanup(t, db)
nodes, err := db.NodesByLabel("foobar")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 0, len(nodes))
n0, _ := db.CreateNode(nil)
n0.AddLabel("foobar")
nodes, err = db.NodesByLabel("foobar")
if err != nil {
t.Fatal(err)
}
exp := []*Node{n0}
assert.Equal(t, exp, nodes)
}
func TestGetAllLabels(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
rndLabel := rndStr(t)
n0, _ := db.CreateNode(nil)
n0.AddLabel(rndLabel)
labels, err := db.Labels()
if err != nil {
t.Fatal(err)
}
m := make(map[string]bool, len(labels))
for _, l := range labels {
m[l] = true
}
if _, ok := m[rndLabel]; !ok {
t.Fatal("Label not returned: " + rndLabel)
}
}