3
3
4
4
class TraversalPatternsTest < Test ::Unit ::TestCase
5
5
def setup
6
- @graph = RedGrape . load_graph 'data/graph-example-1.xml'
6
+ @g1 = RedGrape . load_graph 'data/graph-example-1.xml'
7
7
end
8
8
9
9
# https://github.com/tinkerpop/gremlin/wiki/Backtrack-Pattern
10
10
def test_backtrack_pattern
11
- assert_equal [ 29 ] , @graph . V . out ( 'knows' ) . has ( 'age' , :gt , 30 ) . back ( 2 ) . age . take
12
- assert_equal [ 29 ] , @graph . V . as ( 'x' ) . outE ( 'knows' ) . inV . has ( 'age' , :gt , 30 ) . back ( 'x' ) . age . take
11
+ assert_equal [ 29 ] , @g1 . V . out ( 'knows' ) . has ( 'age' , :gt , 30 ) . back ( 2 ) . age . take
12
+ assert_equal [ 29 ] , @g1 . V . as ( 'x' ) . outE ( 'knows' ) . inV . has ( 'age' , :gt , 30 ) . back ( 'x' ) . age . take
13
13
end
14
14
15
15
# https://github.com/tinkerpop/gremlin/wiki/Except-Retain-Pattern
16
16
def test_except_retain_pattern
17
- assert_equal %w( 2 3 4 ) , @graph . v ( 1 ) . out . take . map ( &:id ) . sort
18
- assert_equal %w( 3 5 ) , @graph . v ( 1 ) . out . out . take . map ( &:id ) . sort
19
- assert_equal %w( 5 ) , @graph . v ( 1 ) . out . aggregate ( :x ) . out . except ( :x ) . take . map ( &:id ) . sort
20
- assert_equal %w( 3 ) , @graph . v ( 1 ) . out . aggregate ( :x ) . out . retain ( :x ) . take . map ( &:id ) . sort
17
+ assert_equal %w( 2 3 4 ) , @g1 . v ( 1 ) . out . take . map ( &:id ) . sort
18
+ assert_equal %w( 3 5 ) , @g1 . v ( 1 ) . out . out . take . map ( &:id ) . sort
19
+ assert_equal %w( 5 ) , @g1 . v ( 1 ) . out . aggregate ( :x ) . out . except ( :x ) . take . map ( &:id ) . sort
20
+ assert_equal %w( 3 ) , @g1 . v ( 1 ) . out . aggregate ( :x ) . out . retain ( :x ) . take . map ( &:id ) . sort
21
21
end
22
22
23
23
# https://github.com/tinkerpop/gremlin/wiki/Flow-Rank-Pattern
24
24
def test_flow_rank_pattern
25
- assert_equal %w( 3 5 ) , @graph . V ( 'lang' , 'java' ) . take . map ( &:id ) . sort
25
+ assert_equal %w( 3 5 ) , @g1 . V ( 'lang' , 'java' ) . take . map ( &:id ) . sort
26
26
software = [ ]
27
- @graph . V ( 'lang' , 'java' ) . fill ( software ) . take
27
+ @g1 . V ( 'lang' , 'java' ) . fill ( software ) . take
28
28
assert_equal %w( 3 5 ) , software . map ( &:id ) . sort
29
29
assert_equal %w( marko josh peter josh ) , software . _ . in ( 'created' ) . name . take
30
30
assert_equal (
@@ -39,7 +39,7 @@ def test_flow_rank_pattern
39
39
40
40
# https://github.com/tinkerpop/gremlin/wiki/Path-Pattern
41
41
def test_path_pattern
42
- v1 = @graph . vertex ( 1 )
42
+ v1 = @g1 . vertex ( 1 )
43
43
44
44
assert_equal %w( josh lop vadas ) , v1 . out . name . take . sort
45
45
@@ -58,9 +58,9 @@ def test_path_pattern
58
58
end
59
59
60
60
def test_loop_pattern
61
- g = RedGrape . load_graph 'data/graph-example-2.xml'
62
- v89 = g . vertex 89
63
- assert_equal 36 , g . v ( 89 ) . outE . inV . path . take . size
61
+ @g2 || = RedGrape . load_graph 'data/graph-example-2.xml'
62
+ v89 = @g2 . vertex 89
63
+ assert_equal 36 , v89 . outE . inV . path . take . size
64
64
65
65
path = v89 . outE . inV . loop ( 2 ) { it . loops < 3 } . path . take . first
66
66
assert_equal '[v[89], e[7006][89-followed_by->127], v[127], e[7786][127-sung_by->340], v[340]]' , path . to_s
@@ -86,15 +86,46 @@ def test_loop_pattern
86
86
assert_equal RedGrape ::Edge , path [ 3 ] . class
87
87
assert_equal RedGrape ::Vertex , path [ 4 ] . class
88
88
89
- #assert_equal 70307, v89.out.loop(1, proc{it.loops < 4}).take.size #=> OK
90
- #assert_equal 71972, v89.out.loop(1, proc{it.loops < 4}, proc{true}).take.size #=> 70307
91
- #assert_equal 582, v89.out.loop(1, proc{it.loops < 4}, proc{it.object.id == '89'}).take.size #=> 564
89
+ #assert_equal(
90
+ # 70307,
91
+ # v89.out.loop(1, proc{it.loops < 4}).take.size
92
+ #) #=> OK
93
+
94
+ #assert_equal(
95
+ # 71972,
96
+ # v89.out.loop(1, proc{it.loops < 4}, proc{true}).take.size
97
+ #) #=> 70307
98
+
99
+ #assert_equal(
100
+ # 582,
101
+ # v89.out.loop(1, proc{it.loops < 4}, proc{it.object.id == '89'}).take.size
102
+ #) #=> 564
92
103
end
93
104
94
105
# https://github.com/tinkerpop/gremlin/wiki/Split-Merge-Pattern
95
- def test_split_marge
96
- v1 = @graph . vertex 1
97
- #assert_equal ['ripple', 27, 'lop', 32], v1.out('knows').copy_split(_.out('created').name, _.age).fair_merge.take # TODO: not yet
98
- assert_equal [ 27 , 'ripple' , 'lop' , 32 ] , v1 . out ( 'knows' ) . copy_split ( _ . out ( 'created' ) . name , _ . age ) . exhaust_merge . take
106
+ def test_split_marge_pattern
107
+ v1 = @g1 . vertex 1
108
+
109
+ #assert_equal(
110
+ # ['ripple', 27, 'lop', 32],
111
+ # v1.out('knows').copy_split(_.out('created').name, _.age).fair_merge.take
112
+ #) # TODO: not yet
113
+
114
+ assert_equal (
115
+ [ 27 , 'ripple' , 'lop' , 32 ] ,
116
+ v1 . out ( 'knows' ) . copy_split ( _ . out ( 'created' ) . name , _ . age ) . exhaust_merge . take
117
+ )
118
+ end
119
+
120
+ def test_map_reduce_pattern
121
+ @g2 ||= RedGrape . load_graph 'data/graph-example-2.xml'
122
+ assert_equal (
123
+ { 'cover' => 313 , 'original' => 184 } ,
124
+ @g2 . V . has_not ( 'song_type' , nil ) . group_count ( proc { it . song_type } ) . cap . take
125
+ )
126
+ assert_equal (
127
+ { } ,
128
+ @g2 . V . has_not ( 'song_type' , nil ) . group_by ( proc { it . song_type } , proc { it } ) . cap . take
129
+ )
99
130
end
100
131
end
0 commit comments