/
aggregate_node_mixin_spec.rb
482 lines (391 loc) · 13.6 KB
/
aggregate_node_mixin_spec.rb
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
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/../../lib")
$LOAD_PATH << File.expand_path(File.dirname(__FILE__) + "/..")
require 'neo4j'
require 'neo4j/spec_helper'
class MyAggregateNode
include Neo4j::NodeMixin
include Neo4j::AggregateNodeMixin
end
describe "Aggregated nodes grouped by one property (colour)" do
before(:all) do
start
Neo4j::Transaction.new
@red=[]
@blue=[]
@green=[]
5.times {@red << Neo4j::Node.new}
4.times {@blue << Neo4j::Node.new}
3.times {@green << Neo4j::Node.new}
@red.each {|n| n[:colour] = 'red'}
@blue.each {|n| n[:colour] = 'blue'}
@green.each {|n| n[:colour] = 'green'}
@all = @red + @blue + @green
# create names a,b,c,d,a,b.c,d,a,b,c,d, ....
names = []
4.times { names += ('a' .. 'd').to_a}
@all.each {|n| n[:name] = names.pop}
end
after(:all) do
stop
end
# Called before each example.
before(:each) do
# For all nodes that have the same colour create a new aggregate node with property 'colour'
# The ColourAggregateNode has outgoing relationship of type blue, black, red to those aggegated nodes.
# Each aggregated node has a property colour with the value of its related nodes. There will be three aggregated nodes with property blue, black and red.
# Those nodes are connected to all nodes that have this property with the same relationship.
#
# @blue nodes<*----[aggregated node, prop colour='blue']<----<rel type=blue>--[@aggnode] ----<rel type=black>-->[aggregated node, prop colour='black']--->@black nodes
# | <rel type=red>
# V
# <relation: red>--->...
@agg_node = MyAggregateNode.new
@agg_node.aggregate(@all).group_by(:colour).execute
end
it "should have an enumeration of all groups" do
@agg_node.to_a.size.should == 3
end
it "should have group nodes with propertie aggregate_group" do
colours = @agg_node.inject([]) {|array, node| array << node.aggregate_group}
colours.should include('red', 'blue', 'green')
end
it "should have size property for how many groups there are" do
@agg_node.aggregate_size.should == 3
end
it "should have size property for each group" do
@agg_node[:red].aggregate_size.should == 5
@agg_node[:blue].aggregate_size.should == 4
@agg_node[:green].aggregate_size.should == 3
end
it "should aggregate properties on aggregated nodes, e.g find name of all people having favorite colour 'red'" do
names = @agg_node[:red][:name].to_a
names.size.should == 5 # duplicated name 'd'
names.should include('a', 'b', 'c', 'd')
end
it "should not add nodes to the aggreation that does not have a group property" do
# add a node that does not have the colour property
@agg_node.to_a.size.should == 3
@agg_node[:red].aggregate_size.should == 5
@agg_node[:blue].aggregate_size.should == 4
@agg_node[:green].aggregate_size.should == 3
@agg_node << Neo4j::Node.new
@agg_node.to_a.size.should == 3
@agg_node[:red].aggregate_size.should == 5
@agg_node[:blue].aggregate_size.should == 4
@agg_node[:green].aggregate_size.should == 3
end
end
describe "Aggregate nodes grouped by one each property" do
before(:all) do
start
Neo4j::Transaction.new
end
after(:all) do
stop
end
it "should implement group_by_each" do
node1 = Neo4j::Node.new; node1[:colour] = 'red'; node1[:type] = 'A'
node2 = Neo4j::Node.new; node2[:colour] = 'red'; node2[:type] = 'B'
agg_node = MyAggregateNode.new
agg_node.aggregate([node1, node2]).group_by_each(:colour, :type).execute
agg_node['red'].aggregate_size.should == 2
agg_node['A'].aggregate_size.should == 1
agg_node['A'].aggregate_size.should == 1
agg_node['A'].should include(node1)
agg_node['B'].should include(node2)
agg_node['red'].should include(node1, node2)
node1.aggregate_groups.to_a.size.should == 2
node2.aggregate_groups.to_a.size.should == 2
end
end
describe "Aggregate people into age groups 0-4, 5-9, 10-14" do
before(:all) do
start
Neo4j::Transaction.run do
@people = []
6.times {@people << Neo4j::Node.new}
@people[0][:age] = 2
@people[1][:age] = 4
@people[2][:age] = 5
@people[3][:age] = 5
@people[4][:age] = 9
@people[5][:age] = 10
# group 0 (0-4) - two people
# group 1 (5-9) - three people
# group 2 (10-14) - one person
Neo4j::Transaction.new
@aggregate_node = MyAggregateNode.new
@aggregate_node.aggregate(@people).group_by(:age).map_value{|age| age / 5}.execute
end
end
after(:all) do
stop
end
it "should traverse all people in an age group" do
@aggregate_node[0].should include(@people[0], @people[1])
@aggregate_node[0].to_a.size.should == 2
@aggregate_node[2].to_a.size.should == 1
@aggregate_node[2].should include(@people[5])
end
it "should count number of people in each age group" do
@aggregate_node.aggregate_size.should == 3 # there are 3 groups, 0-4, 5-9, 10-14
@aggregate_node[0].aggregate_size.should == 2
@aggregate_node[1].aggregate_size.should == 3
@aggregate_node[2].aggregate_size.should == 1
end
end
describe "Aggregate x and y coordinates into squares" do
before(:all) do
start
Neo4j::Transaction.run do
# create positions (0,0), (1,2), (2,4), (3,6) ...
@positions = []
6.times {@positions << Neo4j::Node.new}
@positions.each_with_index {|p, index| p[:x] = index}
@positions.each_with_index {|p, index| p[:y] = index*2}
Neo4j::Transaction.new
@aggregate_node = MyAggregateNode.new
@aggregate_node.aggregate(@positions).group_by(:x, :y).map_value{|x, y| (x/3)*3+(y/3)}.execute
end
end
after(:all) do
stop
end
it "should traverse all positions in a square" do
# find all coordinates in the square 0 - |0,0 2,0|
# |0,2 2,2|
@aggregate_node[0].should include(@positions[0], @positions[1])
@aggregate_node[0].aggregate_size.should == 2
# find all coordinates in the square 1 - |0,3 2,3|
# |0,5 2,5|
@aggregate_node[1].should include(@positions[2])
@aggregate_node[1].aggregate_size.should == 1
end
end
describe "Aggregate, append nodes" do
before(:all) do
start
Neo4j::Transaction.new
@red=[]
@blue=[]
@green=[]
5.times {@red << Neo4j::Node.new}
4.times {@blue << Neo4j::Node.new}
3.times {@green << Neo4j::Node.new}
@red.each {|n| n[:colour] = 'red'}
@blue.each {|n| n[:colour] = 'blue'}
@green.each {|n| n[:colour] = 'green'}
@all = @red + @blue + @green
# aggregate all nodes into colour groups
end
after(:all) do
stop
end
it "should add node into existing groups using the << operator" do
agg_node = MyAggregateNode.new
agg_node.aggregate(@all).group_by(:colour).execute
new_node = Neo4j::Node.new
new_node[:colour] = 'green'
agg_node[:green].aggregate_size.should == 3
# when
agg_node << new_node
# then
agg_node[:green].aggregate_size.should == 4
agg_node[:green].should include(new_node)
end
it "should add node into new groups using the << operator" do
agg_node = MyAggregateNode.new
agg_node.aggregate(@all).group_by(:colour).execute
new_node = Neo4j::Node.new
new_node[:colour] = 'black'
agg_node[:black].should be_nil
# when
agg_node << new_node
# then
agg_node[:black].aggregate_size.should == 1
agg_node[:black].should include(new_node)
end
it "should allow to add node into an empty aggregation using << operator" do
agg_node = MyAggregateNode.new
agg_node.aggregate.group_by(:colour)
new_node = Neo4j::Node.new
new_node[:colour] = 'black'
agg_node[:black].should be_nil
# when
agg_node << new_node
# then
agg_node[:black].aggregate_size.should == 1
agg_node[:black].should include(new_node)
end
it "should implement an effecient include_node? method, by only searching in the relevant group" do
agg_node = MyAggregateNode.new
agg_node.aggregate.group_by(:colour)
new_node = Neo4j::Node.new
new_node[:colour] = 'black'
agg_node.include_node?(new_node).should be_false
agg_node << new_node
# when
agg_node.include_node?(new_node).should be_true
end
it "should not append a node to an aggregate if it already exist in the aggregate" do
pending "Not sure if we want that. Will get bad performance if we have to check it each time we add a node to a aggregate"
agg_node = MyAggregateNode.new
agg_node.aggregate.group_by(:colour)
new_node = Neo4j::Node.new
new_node[:colour] = 'black'
# when
agg_node << new_node
agg_node << new_node
# then
agg_node[:black].aggregate_size.should == 1
agg_node[:black].should include(new_node)
end
end
#
#describe "Aggregates << another_aggregate" do
# before(:all) do
# start
#
# Neo4j::Transaction.new
# @set1 = [Neo4j::Node.new, Neo4j::Node.new]
# @set2 = [Neo4j::Node.new, Neo4j::Node.new]
# @set1[0][:colour] = 'red'
# @set1[1][:colour] = 'blue'
#
# @set2[0][:colour] = 'red'; @set2[0][:age] = 1
# @set2[1][:colour] = 'blue'; @set2[1][:age] = 2
# end
#
# after(:all) do
# stop
# end
#
# it "should add node into existing groups using the << operator" do
# pending "not sure how the << operator should work"
# agg1 = MyAggregateNode.new
# agg2 = MyAggregateNode.new
#
# agg1.aggregate(@set1).group_by(:colour).execute
# agg2.aggregate(@set2).group_by(:age).execute
#
# # when
# agg1 << agg2
#
# # then
# agg1[:red].aggregate_size.should == 2
# agg1[:red].should include(@set1[0], @set2[0])
#
# agg1[:blue].aggregate_size.should == 2
# agg1[:blue].should include(@set1[1], @set2[1])
#
# @set2[0].relationships.incoming.nodes.each {|x| puts "OUTGOING #{x.props.inspect}"}
# end
#
#end
#
#
#
#
describe "Aggregates, each node should know which aggregate(s) it belongs to" do
before(:each) do
start
Neo4j::Transaction.new
@set = []
4.times {@set << Neo4j::Node.new}
@set[0][:colour] = 'red'; @set[0][:name] = "a"
@set[1][:colour] = 'red'; @set[1][:name] = "b"
@set[2][:colour] = 'red'; @set[2][:name] = "c"
@set[3][:colour] = 'blue'; @set[3][:name] = "d"
# aggreate first on name
@agg1 = MyAggregateNode.new
@agg1.aggregate(@set).group_by(:name).execute
#use this name aggregate and aggregate on colour
#
# agg1 set agg2
# a -- @set[0] --+
# b -- @set[1] --+-- red
# c -- @set[2] --+
# d -- @set[3] ---- blue
#
@agg2 = MyAggregateNode.new
@agg2.aggregate(@set).group_by(:colour).execute
end
after(:each) do
stop
end
it "should know which aggregate it belongs to" do
@set[0].aggregates.to_a.size.should == 2
@set[1].aggregates.to_a.size.should == 2
@set[0].aggregates.should include(@agg1, @agg2)
end
it "should know which aggregate group it belongs to" do
# set[0] should belong to group agg1[a] and agg2[red]
@set[0].aggregate_groups.to_a.size.should == 2
@set[0].aggregate_groups.should include(@agg1['a'], @agg2['red'])
# set[2] should belong to group agg1[c] and agg2[red]
@set[2].aggregate_groups.to_a.size.should == 2
@set[2].aggregate_groups.should include(@agg1['c'], @agg2['red'])
# set[3] should belong to group agg[d] and agg2[blue]
@set[3].aggregate_groups.to_a.size.should == 2
@set[3].aggregate_groups.should include(@agg1['d'], @agg2['blue'])
end
end
describe "Aggregates, over another aggregate" do
before(:each) do
start
Neo4j::Transaction.new
@set = []
4.times {@set << Neo4j::Node.new}
@set[0][:colour] = 'red'; @set[0][:name] = "a"
@set[1][:colour] = 'red'; @set[1][:name] = "b"
@set[2][:colour] = 'red'; @set[2][:name] = "c"
@set[3][:colour] = 'blue'; @set[3][:name] = "d"
end
after(:all) do
stop
end
it "should allow to aggregate aggregate groups" do
# given
# agg2 agg1 set
# +-- a -- @set[0]
# red --|-- b -- @set[1]
# +-- c -- @set[2]
# blue ---- d -- @set[3]
#
agg1 = MyAggregateNode.new
agg1.aggregate(@set).group_by(:name).execute
# when
agg2 = MyAggregateNode.new
agg2.aggregate(agg1).group_by(:colour).execute
# then
agg2.aggregate_size.should == 2
agg2['red'].aggregate_size.should == 3
agg2['red'].should include(agg1['a'], agg1['b'], agg1['c'])
agg2['blue'].aggregate_size.should == 1
agg2['blue'].should include(agg1['d'])
end
it "should know which aggregate it belongs to" do
agg1 = MyAggregateNode.new('agg1')
agg1.aggregate(@set).group_by(:name).execute
# when
agg2 = MyAggregateNode.new('agg2')
agg2.aggregate(agg1).group_by(:colour).execute
# then
agg1['a'].aggregates.to_a.size.should == 1
@set[0].aggregates.to_a.size.should == 1
@set[0].aggregates.to_a.should include(agg1)
agg1['a'].aggregates.to_a.should include(agg2)
end
it "should know which aggregate groups it belongs to" do
agg1 = MyAggregateNode.new('agg1')
agg1.aggregate(@set).group_by(:name).execute
# when
agg2 = MyAggregateNode.new('agg2')
agg2.aggregate(agg1).group_by(:colour).execute
# then
agg1['a'].aggregate_groups.to_a.size.should == 1
@set[0].aggregate_groups.to_a.size.should == 1
@set[0].aggregate_groups.to_a.should include(agg1['a'])
agg1['a'].aggregate_groups.to_a.should include(agg2['red'])
end
end