-
Notifications
You must be signed in to change notification settings - Fork 73
/
tests.py
1129 lines (999 loc) · 41.8 KB
/
tests.py
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from datetime import datetime
try:
import cPickle as pickle
except:
import pickle
import client
import constants
import request
import unittest
class NodesTestCase(unittest.TestCase):
def setUp(self):
self.url = "http://localhost:7474/db/data/"
self.gdb = client.GraphDatabase(self.url)
# self.secured_url = "http://<instance>.hosted.neo4j.org:7000/db/data/"
# self.username = "username"
# self.password = "password"
# self.gdb = client.GraphDatabase(self.secured_url, username=self.username,
# password=self.password)
def tearDown(self):
import options as clientCacheDebug
clientCacheDebug.DEBUG = False
clientCacheDebug.CACHE = False
if self.gdb:
self.gdb.flush()
def test_connection_cache(self):
import options as clientCache
clientCache.CACHE = True
gdb = client.GraphDatabase(self.url)
clientCache.CACHE = False
self.assertEqual(gdb.url, self.url)
def test_connection_debug(self):
import options as clientDebug
clientDebug.DEBUG = True
gdb = client.GraphDatabase(self.url)
clientDebug.DEBUG = False
self.assertEqual(gdb.url, self.url)
def test_connection_cache_debug(self):
import options as clientCacheDebug
clientCacheDebug.CACHE = True
clientCacheDebug.DEBUG = True
gdb = client.GraphDatabase(self.url)
clientCacheDebug.CACHE = False
clientCacheDebug.DEBUG = False
self.assertEqual(gdb.url, self.url)
def test_create_node(self):
n = self.gdb.nodes.create()
self.assertEqual(n.properties, {})
def test_create_node_properties(self):
n = self.gdb.nodes.create(name="John Doe", profession="Hacker")
self.assertEqual(n.properties, {"name": "John Doe",
"profession": "Hacker"})
def test_create_node_empty(self):
n = self.gdb.node()
self.assertEqual(n.properties, {})
def test_create_node_properties_dictionary(self):
n = self.gdb.node(name="John Doe", profession="Hacker")
self.assertEqual(n.properties, {"name": "John Doe",
"profession": "Hacker"})
def test_create_node_dictionary(self):
n = self.gdb.node(name="John Doe", profession="Hacker")
self.assertEqual(n["name"], "John Doe")
def test_create_node_get(self):
n = self.gdb.node(name="John Doe", profession="Hacker")
self.assertEqual(n.get("name"), "John Doe")
def test_create_node_get_default(self):
n = self.gdb.node(name="John Doe", profession="Hacker")
self.assertEqual(n.get("surname", "Doe"), "Doe")
def test_get_node(self):
n1 = self.gdb.nodes.create(name="John Doe", profession="Hacker")
n2 = self.gdb.nodes.get(n1.id)
self.assertEqual(n1, n2)
def test_get_node_url(self):
n1 = self.gdb.nodes.create(name="John Doe", profession="Hacker")
n2 = self.gdb.nodes.get(n1.url)
self.assertEqual(n1, n2)
def test_get_node_dicionary(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
n2 = self.gdb.node[n1.id]
self.assertEqual(n1, n2)
def test_get_node_dicionary_with_false(self):
n1 = self.gdb.node(name="John Doe", enable=False)
n2 = self.gdb.node[n1.id]
self.assertEqual(n1.properties, n2.properties)
def test_get_node_url_dicionary(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
n2 = self.gdb.node[n1.url]
self.assertEqual(n1, n2)
def test_set_node_property_dictionary(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
n1["name"] = "Jimmy Doe"
n2 = self.gdb.node[n1.id]
self.assertEqual(n1["name"], n2["name"])
def test_set_node_property(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
n1.set("name", "Jimmy Doe")
n2 = self.gdb.nodes.get(n1.id)
self.assertEqual(n1.get("name"), n2.get("name"))
def test_set_node_properties(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
n1.properties = {"name": "Jimmy Doe"}
n2 = self.gdb.node[n1.id]
self.assertEqual(n1.properties, n2.properties)
def test_set_node_property_safe(self):
n1 = self.gdb.node(language="Español antigüillo")
n1.set("Idioma de los subtítulos", "Español antigüillo")
self.assertEqual(n1.get("Idioma de los subtítulos"), n1.get("language"))
def test_set_node_properties_safe(self):
n1 = self.gdb.node()
n1.properties = {"Idioma de los subtítulos": "Español antigüillo"}
n1_properties = n1.properties
n2 = self.gdb.node[n1.id]
n2_properties = n2.properties
self.assertEqual(n1_properties, n2_properties)
def test_delete_node_property_dictionary(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
del n1["name"]
self.assertEqual(n1.get("name", None), None)
def test_delete_node_property(self):
n1 = self.gdb.nodes.create(name="John Doe", profession="Hacker")
n1.delete("name")
self.assertEqual(n1.get("name", None), None)
def test_delete_node_properties(self):
n1 = self.gdb.node(name="John Doe", profession="Hacker")
del n1.properties
self.assertEqual(n1.properties, {})
def test_delete_node(self):
n1 = self.gdb.nodes.create(name="John Doe", profession="Hacker")
identifier = n1.id
n1.delete()
try:
self.gdb.nodes.get(identifier)
self.fail()
except request.NotFoundError, request.StatusException:
pass
def test_node_hash(self):
n1 = self.gdb.node()
n2 = self.gdb.node[n1.id]
self.assertEqual(len(set([n1, n2])), 1)
self.assertEqual(hash(n1), hash(n2))
class RelationshipsTestCase(NodesTestCase):
def test_create_relationship(self):
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = n1.Knows(n2)
self.assertEqual(rel.properties, {})
def test_create_relationship_functional(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2)
self.assertEqual(rel.properties, {})
def test_create_relationship_functional_safe(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
r1 = n1.relationships.create("recommends ñapa motörhead", n2)
self.assertEqual(r1.properties, {})
self.assertEqual(r1.type, "recommends ñapa motörhead")
r2 = self.gdb.relationships.get(r1.id)
self.assertEqual(r1.type, r2.type)
def test_create_relationship_properties(self):
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = n1.Knows(n2, since=1970)
self.assertEqual(rel.properties, {"since": 1970})
def test_create_relationship_functional_properties(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2, since=1970)
self.assertEqual(rel.properties, {"since": 1970})
def test_set_relationship_property_safe(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2,
language="Español antigüillo")
rel.set("Idioma de los subtítulos", "Español antigüillo")
self.assertEqual(rel.get("Idioma de los subtítulos"),
rel.get("language"))
def test_set_relationship_properties_safe(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
r1 = n1.relationships.create("Knows", n2)
r1.properties = {"Idioma de los subtítulos": "Español antigüillo"}
r1_properties = r1.properties
r2 = self.gdb.relationships.get(r1.id)
r2_properties = r2.properties
self.assertEqual(r1_properties, r2_properties)
def test_delete_relationship(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2, since=1970)
rel.delete()
self.assertEqual(len(n1.relationships.outgoing("Knows")), 0)
def test_get_relationship(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2, since=1970)
self.assertTrue(isinstance(rel, client.Relationship))
def test_delete_relationship_not_found(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
rel = n1.relationships.create("Knows", n2, since=1970)
rel_id = rel.id
rel.delete()
self.assertRaises(request.NotFoundError, self.gdb.relationships.get,
rel_id)
class IndexesTestCase(RelationshipsTestCase):
def test_create_index_for_nodes(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
index = self.gdb.nodes.indexes.create(name="doe")
index["surnames"]["d"] = n1
self.assertTrue(n1 in index["surnames"]["d"])
def test_create_index_for_nodes_and_dots(self):
# From https://github.com/versae/neo4j-rest-client/issues/43
n1 = self.gdb.nodes.create(name="John.Doe", place="Texas.s")
index = self.gdb.nodes.indexes.create(name="doe")
index["surnames.s"]["d.d"] = n1
self.assertTrue(n1 in index["surnames.s"]["d.d"])
def test_create_index_for_nodes_unicode(self):
n1 = self.gdb.nodes.create(name="Lemmy", band="Motörhead")
index = self.gdb.nodes.indexes.create(name="doe")
index["bands"]["Motörhead"] = n1
self.assertTrue(n1 in index["bands"]["Motörhead"])
def test_create_index_for_nodes_url_safe(self):
n1 = self.gdb.nodes.create(name="Brian", place="AC/DC")
index = self.gdb.nodes.indexes.create(name="doe")
index["bands"]["AC/DC"] = n1
self.assertTrue(n1 in index["bands"]["AC/DC"])
def test_delete_index_for_nodes(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
index = self.gdb.nodes.indexes.create(name="doe")
index["surnames"]["d"] = n1
index.delete()
self.assertRaises(request.NotFoundError,
index["surnames"].__getitem__, "d")
def test_create_index_for_relationships(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
n2 = self.gdb.nodes.create(name="Michael Doe", place="Tijuana")
r1 = self.gdb.relationships.create(n1, "Hates", n2)
index = self.gdb.relationships.indexes.create(name="brothers")
index["feeling"]["hate"] = r1
self.assertTrue(r1 in index["feeling"]["hate"])
def test_delete_node_from_index(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
index = self.gdb.nodes.indexes.create(name="doe")
index["surnames"]["d"] = n1
index.delete("surnames", "d", n1)
self.assertTrue(n1 not in index["surnames"]["d"])
def test_delete_node_from_index_with_no_value(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
index = self.gdb.nodes.indexes.create(name="doe")
index["surnames"]["d"] = n1
index.delete("surnames", None, n1)
self.assertTrue(n1 not in index["surnames"]["d"])
def test_delete_relationship_from_index(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
n2 = self.gdb.nodes.create(name="Michael Doe", place="Tijuana")
r1 = self.gdb.relationships.create(n1, "Hates", n2)
index = self.gdb.relationships.indexes.create(name="brothers")
index["feeling"]["hate"] = r1
index.delete("feeling", "hate", r1)
self.assertTrue(r1 not in index["feeling"]["hate"])
def test_delete_index_for_relationships(self):
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
n2 = self.gdb.nodes.create(name="Michael Doe", place="Tijuana")
r1 = self.gdb.relationships.create(n1, "Hates", n2)
index = self.gdb.relationships.indexes.create(name="brothers")
index["feeling"]["hate"] = r1
index.delete()
self.assertRaises(request.NotFoundError,
index["feeling"].__getitem__, "hate")
def test_query_index(self):
Q = client.Q
n1 = self.gdb.nodes.create(name="John Doe", place="Texas")
n2 = self.gdb.nodes.create(name="Michael Donald", place="Tijuana")
index = self.gdb.nodes.indexes.create(name="do", type="fulltext")
index["surnames"]["doe"] = n1
index["surnames"]["donald"] = n2
index['place']['Texas'] = n1
index['place']['Tijuana'] = n2
results = index.query("surnames", "do*")
self.assertTrue(n1 in results and n2 in results)
results = index.query("surnames:do*")
self.assertTrue(n1 in results and n2 in results)
results = index.query('surnames', Q('do*', wildcard=True))
self.assertTrue(n1 in results and n2 in results)
results = index.query(Q('surnames', 'do*', wildcard=True))
self.assertTrue(n1 in results and n2 in results)
results = index.query(Q('surnames', 'do*', wildcard=True)
& Q('place', 'Tijuana'))
self.assertTrue(n1 not in results and n2 in results)
results = index.query(-Q('surnames', 'donald') | +Q('place', 'Texas'))
self.assertTrue(n2 not in results and n1 in results)
class TraversalsTestCase(IndexesTestCase):
def test_create_traversal(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
n1.relationships.create("Knows", n2, since=1970)
types = [
client.All.Knows,
]
traversal = n1.traverse(types=types)
self.assertTrue(len(traversal) > 0)
def test_path_traversal(self):
"""
Tests the use of Path as returnable type.
"""
nodes = [self.gdb.nodes.create() for i in xrange(10)]
# Chain them into a linked list
last = None
for n in nodes:
if last:
last.relationships.create("Knows", n)
last = n
# Toss in a different relationship type to ensure the
# STOP_AT_END_OF_GRAPH didn't break traversing by type
nodes[-1].relationships.create("Test", self.gdb.nodes.create())
types = [
client.All.Knows,
]
stop = constants.STOP_AT_END_OF_GRAPH
traversal = nodes[0].traverse(types=types, stop=stop,
returns=constants.PATH)
paths = [path for path in traversal]
self.assertTrue(len(traversal) > 0)
self.assertTrue(all([isinstance(path, client.Path) for path in paths]))
start_node = paths[0].start
relationship = paths[0].relationships[0]
self.assertTrue(isinstance(start_node, client.Node))
self.assertTrue(isinstance(relationship, client.Relationship))
def test_path_traversal_getitem(self):
# Test from @shahin: https://gist.github.com/1418704
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = self.gdb.relationships.create(n1,'knows',n2)
# Define path traverser
class PathTraverser(self.gdb.Traversal):
stop = constants.STOP_AT_END_OF_GRAPH
returns = constants.PATH
try:
paths = [traversal for traversal in PathTraverser(n1)]
self.assertEqual(len(paths[0]), 1)
except:
raise
finally:
# Clean up on fail
self.gdb.relationships.delete(rel.id)
self.gdb.node.delete(n2.id)
self.gdb.node.delete(n1.id)
def test_graph_wide_traversal(self):
"""
Tests the use of constants.STOP_AT_END_OF_GRAPH as a stop depth.
"""
nodes = [self.gdb.nodes.create() for i in xrange(10)]
# Chain them into a linked list
last = None
for n in nodes:
if last:
last.relationships.create("Knows", n)
last = n
# Toss in a different relationship type to ensure the
# STOP_AT_END_OF_GRAPH didn't break traversing by type
nodes[-1].relationships.create("Test", self.gdb.nodes.create())
types = [
client.All.Knows,
]
stop = constants.STOP_AT_END_OF_GRAPH
traversal = nodes[0].traverse(types=types, stop=stop)
self.assertEqual(len(traversal), len(nodes) - 1)
# Test an untyple traversal
traversal = nodes[0].traverse(stop=stop)
self.assertEqual(len(traversal), len(nodes))
def test_traversal_return_filter(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
n3 = self.gdb.nodes.create(name='test name')
n1.KNOWS(n2)
n2.KNOWS(n3)
#all traversal
traversal = n1.traverse(stop=3,\
returnable=constants.RETURN_ALL_NODES)
self.assertEqual(len(traversal), 3)
#all but start traversal
traversal = n1.traverse(stop=3,\
returnable=constants.RETURN_ALL_BUT_START_NODE)
self.assertEqual(len(traversal), 2)
#custom javascript return filter
return_filter_body = 'position.endNode().hasProperty("name")&&'\
'position.endNode().getProperty("name")=="'\
'test name";'
traversal = n1.traverse(stop=3, returnable=return_filter_body)
self.assertEqual(len(traversal), 1)
def test_create_traversal_class(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
n1.relationships.create("Knows", n2, since=1970)
class TraversalClass(self.gdb.Traversal):
types = [
client.All.Knows,
]
results = []
for result in TraversalClass(n1):
results.append(result)
self.assertTrue(len(results) > 0)
def test_paginated_traversal(self):
"""
Tests the use of paginated traversals.
"""
nodes = [self.gdb.nodes.create() for i in xrange(10)]
# Chain them into a linked list
last = None
for n in nodes:
if last:
last.relationships.create("Knows", n)
last = n
# Toss in a different relationship type to ensure the
# STOP_AT_END_OF_GRAPH didn't break traversing by type
nodes[-1].relationships.create("Test", self.gdb.nodes.create())
types = [
client.All.Knows,
]
stop = constants.STOP_AT_END_OF_GRAPH
pages = nodes[0].traverse(types=types, stop=stop, page_size=5)
traversal_length = 0
pages_count = 0
for traversal in pages:
traversal_length += len(traversal)
pages_count += 1
self.assertEqual(traversal_length, len(nodes) - 1)
self.assertEqual(pages_count, 2)
# And make the same test only passing "paginated" argument
pages = nodes[0].traverse(types=types, stop=stop, paginated=True)
traversal_length = 0
for traversal in pages:
traversal_length += len(traversal)
self.assertEqual(traversal_length, len(nodes) - 1)
# Set a ridiculous time_out for getting no results
pages = nodes[0].traverse(stop=stop, time_out=0.000001)
traversal_length = len([n for n in [t for t in pages]])
self.assertEqual(traversal_length, 0)
# Taken from the official tests by Neo4j python-embedded
# https://github.com/neo4j/python-embedded/blob/master/src/test/python/traversal.py
def create_data(self):
with self.gdb.transaction():
self.source = self.gdb.node(message='hello')
target = self.gdb.node(message='world')
relationship = self.source.related_to(target, message="graphy")
secondrel = target.likes(self.source, message="buh")
def test_traverse_string_types(self):
self.create_data()
db = self.gdb
start_node = self.source
# START SNIPPET: basicTraversal
traverser = db.traversal()\
.relationships('related_to')\
.traverse(start_node)
# The graph is traversed as
# you loop through the result.
for node in traverser.nodes:
pass
# END SNIPPET: basicTraversal
self.assertEqual(len(list(traverser.nodes)), 1)
# START SNIPPET: directedTraversal
from traversals import RelationshipDirection
OUTGOING = RelationshipDirection.OUTGOING
INCOMING = RelationshipDirection.INCOMING
ANY = RelationshipDirection.ANY
traverser = db.traversal()\
.relationships('related_to', OUTGOING)\
.traverse(start_node)
# END SNIPPET: directedTraversal
self.assertEqual(len(list(traverser.nodes)), 1)
traverser = db.traversal()\
.relationships('related_to', INCOMING)\
.relationships('likes')\
.traverse(start_node)
# END SNIPPET: multiRelationshipTraversal
self.assertEqual(len(list(traverser.nodes)), 1)
# START SNIPPET: traversalResults
traverser = db.traversal()\
.relationships('related_to')\
.traverse(start_node)
# Get each possible path
for path in traverser:
pass
# Get each node
for node in traverser.nodes:
pass
# Get each relationship
for relationship in traverser.relationships:
pass
# END SNIPPET: traversalResults
def test_traverse_programmatic_types(self):
from client import Direction
self.create_data()
t = self.gdb.traversal()\
.depthFirst()\
.relationships(Direction.ANY.related_to)\
.traverse(self.source)
res = list(t.nodes)
self.assertEqual(len(res), 1)
def test_uniqueness(self):
self.create_data()
db = self.gdb
start_node = self.source
# START SNIPPET: uniqueness
from traversals import Uniqueness
traverser = db.traversal()\
.uniqueness(Uniqueness.NODE_PATH)\
.traverse(start_node)
# END SNIPPET: uniqueness
res = list(traverser.nodes)
self.assertEqual(len(res), 2)
def test_ordering(self):
self.create_data()
db = self.gdb
start_node = self.source
# START SNIPPET: ordering
# Depth first traversal, this
# is the default.
traverser = db.traversal()\
.depthFirst()\
.traverse(self.source)
# Breadth first traversal
traverser = db.traversal()\
.breadthFirst()\
.traverse(start_node)
# END SNIPPET: ordering
res = list(traverser.nodes)
self.assertEqual(len(res), 1)
def test_paths(self):
self.create_data()
t = self.gdb.traversal()\
.traverse(self.source)
for path in t:
# START SNIPPET: accessPathStartAndEndNode
start_node = path.start
end_node = path.end
# END SNIPPET: accessPathStartAndEndNode
self.assertNotEqual(start_node, None)
self.assertNotEqual(end_node, None)
# START SNIPPET: accessPathLastRelationship
last_relationship = path.last_relationship
# END SNIPPET: accessPathLastRelationship
# START SNIPPET: loopThroughPath
for item in path:
# Item is either a Relationship,
# or a Node
pass
for nodes in path.nodes:
# All nodes in a path
pass
for nodes in path.relationships:
# All relationships in a path
pass
# END SNIPPET: loopThroughPath
break
class ExtensionsTestCase(TraversalsTestCase):
def test_get_graph_extensions(self):
fail = False
try:
self.gdb.extensions
except:
fail = True
self.assertTrue(not fail)
def test_get_node_extensions(self):
fail = False
n1 = self.gdb.nodes.create()
try:
n1.extensions
except:
fail = True
self.assertTrue(not fail)
def test_gremlin_extension_reference_node(self):
# Assuming the GremlinPlugin installed
ext = self.gdb.extensions.GremlinPlugin
n = ext.execute_script(script='g.v(0)')
self.assertTrue(isinstance(n, client.Node))
def test_gremlin_extension_reference_node_returns(self):
# Assuming the GremlinPlugin installed
ext = self.gdb.extensions.GremlinPlugin
n = ext.execute_script(script='g.v(0)', returns=constants.NODE)
self.assertTrue(isinstance(n, client.Node))
def test_gremlin_extension_relationships(self):
# Assuming the GremlinPlugin installed
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
n3 = self.gdb.nodes.create()
n1.relationships.create("related", n2)
n1.relationships.create("related", n3)
gremlin = self.gdb.extensions.GremlinPlugin.execute_script
rels = gremlin(script='g.v(%s).outE' % n1.id)
self.assertEqual(len(rels), 2)
for rel in rels:
self.assertTrue(isinstance(rel, client.Relationship))
def test_gremlin_extension_relationships_returns(self):
# Assuming the GremlinPlugin installed
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
n3 = self.gdb.nodes.create()
n1.relationships.create("related", n2)
n1.relationships.create("related", n3)
gremlin = self.gdb.extensions.GremlinPlugin.execute_script
import options as clientDebug
clientDebug.DEBUG = True
rels = gremlin(script='g.v(%s).outE' % n1.id,
returns=constants.RELATIONSHIP)
self.assertEqual(len(rels), 2)
for rel in rels:
self.assertTrue(isinstance(rel, client.Relationship))
clientDebug.DEBUG = False
def test_gremlin_extension_reference_raw_returns(self):
# Assuming the GremlinPlugin installed
ext = self.gdb.extensions.GremlinPlugin
n = ext.execute_script(script='g.v(0)', returns=constants.RAW)
self.assertTrue(isinstance(n, dict))
class TransactionsTestCase(ExtensionsTestCase):
def test_transaction_delete(self):
n = self.gdb.nodes.create()
n["age"] = 25
with self.gdb.transaction():
n.delete("age")
self.assertTrue(isinstance(n, client.Node))
self.assertTrue("age" not in n.properties)
def test_transaction_delete_node(self):
n = self.gdb.nodes.create()
with self.gdb.transaction():
n.delete()
self.assertFalse(n)
self.assertEqual(n, None)
def test_transaction_delete_relationship(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
r = n1.relationships.create("relation", n2)
with self.gdb.transaction():
r.delete()
self.assertFalse(r)
self.assertEqual(r, None)
def test_transaction_properties(self):
n = self.gdb.nodes.create()
n["age"] = 25
n["place"] = "Houston"
with self.gdb.transaction():
n.delete("age")
self.assertTrue(isinstance(n, client.Node))
self.assertTrue("age" not in n.properties)
self.assertTrue("place" in n.properties)
def test_transaction_properties_update(self):
n = self.gdb.nodes.create()
n["age"] = 25
with self.gdb.transaction(update=False):
n.delete("age")
self.assertTrue(isinstance(n, client.Node))
self.assertTrue("age" in n.properties)
def test_transaction_create(self):
with self.gdb.transaction():
n = self.gdb.nodes.create(age=25)
self.assertTrue(isinstance(n, client.Node))
self.assertTrue(n.get("age", True))
def test_transaction_create_and_set(self):
with self.gdb.transaction():
n = self.gdb.nodes.create(age=25)
n.set("surname", u"AC/DC")
n["name"] = u"Motörhead"
self.assertEqual(n.properties, {
"age": 25,
"name": u"Motörhead",
"surname": u"AC/DC",
})
self.assertTrue(isinstance(n, client.Node))
self.assertEqual(n.get("age"), 25)
self.assertEqual(n.get("name"), u"Motörhead")
self.assertEqual(n.properties, {
"age": 25,
"name": u"Motörhead",
"surname": u"AC/DC",
})
def test_transaction_get(self):
n1 = self.gdb.nodes.get(1)
with self.gdb.transaction():
n2 = self.gdb.nodes.get(1)
self.assertTrue(isinstance(n1, client.Node))
self.assertTrue(isinstance(n2, client.Node))
self.assertEqual(n1, n2)
def test_transaction_property(self):
n = self.gdb.nodes.create()
with self.gdb.transaction():
n["age"] = 25
self.assertTrue(isinstance(n, client.Node))
self.assertTrue("age" in n.properties)
def test_transaction_relationship(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
with self.gdb.transaction():
r = n1.relationships.create("Knows", n2, since=1970)
self.assertTrue(isinstance(r, client.Relationship))
self.assertTrue(r is not None)
def test_transaction_commit(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
initial_rels = len(n1.relationships)
rels_number = 10
with self.gdb.transaction(commit=False) as tx:
for i in range(1, 1 + rels_number):
n1.relationships.create("relation_%s" % i, n2)
pre_commit_rels = len(n1.relationships)
self.assertEqual(initial_rels, pre_commit_rels)
tx.commit()
post_commit_rels = len(n1.relationships)
self.assertEqual(initial_rels + rels_number, post_commit_rels)
def test_transaction_globals(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
initial_rels = len(n1.relationships)
rels_number = 10
with self.gdb.transaction(using_globals=False) as tx:
for i in range(1, 1 + rels_number):
n1.relationships.create("relation_%s" % i, n2, tx=tx)
self.assertEqual(initial_rels + rels_number, len(n1.relationships))
def test_transaction_update(self):
n = self.gdb.nodes.create()
n["age"] = 25
with self.gdb.transaction(update=False):
n.delete("age")
self.assertTrue(isinstance(n, client.Node))
self.assertTrue("age" in n.properties)
n.update()
self.assertTrue("age" not in n.properties)
def test_transaction_set(self):
n = self.gdb.nodes.create()
n["age"] = 25
n["name"] = "John"
n["place"] = "Houston"
with self.gdb.transaction(commit=False, using_globals=False) as tx:
n["name"] = tx("Jonathan")
n["age", tx] = 30
n.set("place", "Toronto", tx=tx)
self.assertEqual(n["age"], 25)
self.assertEqual(n["name"], "John")
self.assertEqual(n["place"], "Houston")
tx.commit()
self.assertEqual(n["age"], 30)
self.assertEqual(n["name"], "Jonathan")
self.assertEqual(n["place"], "Toronto")
def test_transaction_multiple(self):
n = self.gdb.nodes.create()
n["age"] = 25
n["name"] = "John"
n["place"] = "Houston"
with self.gdb.transaction(commit=False, using_globals=False) as tx1:
with self.gdb.transaction(commit=False, using_globals=False) as tx2:
n.delete("age", tx=tx1)
n["name"] = tx2("Jonathan")
n["place", tx2] = "Toronto"
self.assertTrue("age" in n.properties)
tx1.commit()
self.assertTrue("age" not in n.properties)
self.assertEqual(n["name"], "John")
self.assertEqual(n["place"], "Houston")
tx2.commit()
self.assertEqual(n["name"], "Jonathan")
self.assertEqual(n["place"], "Toronto")
def test_transaction_list(self):
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
initial_rels = len(n1.relationships)
relations = []
rels_number = 10
with self.gdb.transaction(commit=False) as tx:
for i in range(1, 1 + rels_number):
relation = n1.relationships.create("relation_%s" % i, n2)
relations.append(relation)
tx.commit()
self.assertEqual(initial_rels + rels_number, len(n1.relationships))
self.assertTrue(all([isinstance(r, client.Relationship)]
for r in relations))
def test_transaction_dict(self):
nodes = {}
nodes_number = 10
with self.gdb.transaction():
for i in range(1, 1 + nodes_number):
nodes[i] = self.gdb.nodes.create(position=i)
for position, node in nodes.items():
self.assertTrue(isinstance(node, client.Node))
self.assertEqual(position, node["position"])
def test_transaction_conections(self):
import options as clientDebug
clientDebug.DEBUG = True
nodes = []
with self.gdb.transaction(commit=False) as tx:
for i in range(5):
nodes.append(self.gdb.node[i])
tx.commit()
clientDebug.DEBUG = False
def test_transaction_create_relationship_functional(self):
with self.gdb.transaction():
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = n1.relationships.create("Knows", n2)
rel["when"] = "January"
self.assertEqual(rel.properties, {"when": "January"})
def test_transaction_create_relationship_functional_mixed2(self):
n1 = self.gdb.node()
with self.gdb.transaction():
n2 = self.gdb.node()
rel = n1.relationships.create("Knows", n2)
rel["when"] = "January"
self.assertEqual(rel.properties, {"when": "January"})
def test_transaction_create_relationship_functional_mixed2(self):
n2 = self.gdb.node()
with self.gdb.transaction():
n1 = self.gdb.node()
rel = n1.relationships.create("Knows", n2)
rel["when"] = "January"
self.assertEqual(rel.properties, {"when": "January"})
def test_transaction_create_relationship_functional(self):
with self.gdb.transaction():
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = n1.relationships.create("Knows", n2)
rel["when"] = "January"
self.assertEqual(rel.start, n1)
self.assertEqual(rel.end, n2)
self.assertEqual(rel.properties, {"when": "January"})
self.assertTrue(isinstance(n1, client.Node))
self.assertTrue(isinstance(n2, client.Node))
def test_transaction_create_relationship(self):
with self.gdb.transaction():
n1 = self.gdb.node()
n2 = self.gdb.node()
rel = n1.Knows(n2)
rel["when"] = "January"
self.assertEqual(rel.properties, {"when": "January"})
# The next tests for transaction were taken from @mhluongo fork
# https://github.com/mhluongo/neo4j-rest-client/blob/master/neo4jrestclient/tests.py
def test_transaction_index_creation(self):
"""
Tests whether indexes are properly created during a transaction.
Asserts the creation also behaves transactionally (ie, not until
commit).
"""
with self.gdb.transaction():
i1 = self.gdb.nodes.indexes.create('index_from_tx')
transactionality_test = i1()
self.assertTrue(isinstance(transactionality_test, dict))
self.assertTrue(i1 is not None)
self.assertTrue(isinstance(i1, client.Index))
i2 = self.gdb.nodes.indexes.get('index_from_tx')
self.assertTrue(i1 == i2)
def test_transaction_add_node_to_index(self):
"""
Tests whether a node can be added to an index within a transaction.
Does not assert transactionality.
"""
n1 = self.gdb.nodes.create()
index = self.gdb.nodes.indexes.create('index_nodes')
with self.gdb.transaction():
index.add('test1','test1', n1)
index['test2']['test2'] = n1
self.assertTrue(index['test1']['test1'][-1] == n1)
self.assertTrue(index['test2']['test2'][-1] == n1)
def test_transaction_index_add_rel_to_index(self):
"""
Tests whether a relationship can be added to an index within a transaction.
Does not assert transactionality.
"""
#test nodes
n1 = self.gdb.nodes.create()
n2 = self.gdb.nodes.create()
r = n1.relationships.create('Knows', n2)
index = self.gdb.relationships.indexes.create('index_rel')
with self.gdb.transaction():
index.add('test1','test1', r)
index['test2']['test2'] = r
self.assertTrue(index['test1']['test1'][-1] == r)
self.assertTrue(index['test2']['test2'][-1] == r)
def test_transaction_index_query(self):
"""
Tests whether the transaction methods work with index queries.
Note- this test does not prove query transactionality.
"""
n1 = self.gdb.nodes.create()
index = self.gdb.nodes.indexes.create('index2')
index.add('test2','test2', n1)
# Test getting nodes from index during transaction
tx = self.gdb.transaction()
index_hits = index['test2']['test2']
tx.commit()
self.assertTrue(n1 == index_hits[:][-1])