neography-batch 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- neography-batch (1.0.0)
4
+ neography-batch (1.0.1)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,95 +1,99 @@
1
- class Batch
2
- private
3
- def initialize(graph_db = Neography::Rest.new, commands = [])
4
- @commands = commands
5
- @db = graph_db
6
- yield(self) if block_given?
1
+ module Neography
2
+ module Batch
3
+ class Batch
4
+ private
5
+ def initialize(graph_db = Neography::Rest.new, commands = [])
6
+ @commands = commands
7
+ @db = graph_db
8
+ yield(self) if block_given?
7
9
 
8
- end
10
+ end
9
11
 
10
- def self.get_resolved_commands(commands)
11
- result = []
12
- reference_and_index = {}
12
+ def self.get_resolved_commands(commands)
13
+ result = []
14
+ reference_and_index = {}
13
15
 
14
- # write each command_or_batch and remember index of command_or_batch
15
- commands.each do |command|
16
- index = result.length
17
- reference_and_index[command[:reference]] = index
18
- result << command[:cmd]
19
- end
16
+ # write each command_or_batch and remember index of command_or_batch
17
+ commands.each do |command|
18
+ index = result.length
19
+ reference_and_index[command[:reference]] = index
20
+ result << command[:cmd]
21
+ end
20
22
 
21
- # replace references contained in the commands with the effective index in the batch
22
- result.each do |command|
23
- command.each_with_index do |element, index|
24
- if reference_and_index.has_key?(element)
25
- command[index] = "{#{reference_and_index[element]}}"
23
+ # replace references contained in the commands with the effective index in the batch
24
+ result.each do |command|
25
+ command.each_with_index do |element, index|
26
+ if reference_and_index.has_key?(element)
27
+ command[index] = "{#{reference_and_index[element]}}"
28
+ end
29
+ end
26
30
  end
31
+ result
27
32
  end
28
- end
29
- result
30
- end
31
-
32
- protected
33
- def commands
34
- @commands
35
- end
36
33
 
37
- public
38
- def self.unit
39
- Batch.new
40
- end
34
+ protected
35
+ def commands
36
+ @commands
37
+ end
41
38
 
42
- def add(command_or_batch)
43
- if command_or_batch.class == Batch
44
- bind(command_or_batch)
45
- else
46
- unless command_or_batch.respond_to?(:each)
47
- raise StandardError, "command_or_batch must respond to :each"
39
+ public
40
+ def self.unit
41
+ Batch.new
48
42
  end
49
43
 
50
- reference = BatchReference.new(command_or_batch)
51
- @commands << {:cmd => command_or_batch, :reference => reference}
52
- reference
53
- end
54
- end
44
+ def add(command_or_batch)
45
+ if command_or_batch.class == Batch
46
+ bind(command_or_batch)
47
+ else
48
+ unless command_or_batch.respond_to?(:each)
49
+ raise StandardError, "command_or_batch must respond to :each"
50
+ end
55
51
 
56
- def find_reference
57
- @commands.select { |c| !block_given? || yield(c[:cmd]) }.map { |c| c[:reference] }
58
- end
52
+ reference = BatchReference.new(command_or_batch)
53
+ @commands << {:cmd => command_or_batch, :reference => reference}
54
+ reference
55
+ end
56
+ end
59
57
 
60
- alias :<< :add
58
+ def find_reference
59
+ @commands.select { |c| !block_given? || yield(c[:cmd]) }.map { |c| c[:reference] }
60
+ end
61
61
 
62
- def submit
63
- return [] if @commands.empty?
64
- command_list = Batch.get_resolved_commands(@commands)
65
- result = @db.batch(*command_list)
66
- if result.nil?
67
- raise StandardError, "batch returned no result (nil)"
68
- end
69
- batch_errors = result.select { |r| r["status"] >= 400 }
70
- if batch_errors.count > 0
71
- raise StandardError, "batch returned one or more errors: #{batch_errors.map { |e| e["message"] }.join("|")}"
72
- end
73
- results = result.map { |r| r["body"] }
74
- @commands.each_with_index { |c, i| c[:reference].notify_after_submit(results[i]) }
75
- @commands.clear()
76
- results
77
- end
62
+ alias :<< :add
78
63
 
79
- def bind(batch)
80
- Batch.new(@db, @commands.concat(batch.commands))
81
- end
64
+ def submit
65
+ return [] if @commands.empty?
66
+ command_list = Batch.get_resolved_commands(@commands)
67
+ result = @db.batch(*command_list)
68
+ if result.nil?
69
+ raise StandardError, "batch returned no result (nil)"
70
+ end
71
+ batch_errors = result.select { |r| r["status"] >= 400 }
72
+ if batch_errors.count > 0
73
+ raise StandardError, "batch returned one or more errors: #{batch_errors.map { |e| e["message"] }.join("|")}"
74
+ end
75
+ results = result.map { |r| r["body"] }
76
+ @commands.each_with_index { |c, i| c[:reference].notify_after_submit(results[i]) }
77
+ @commands.clear()
78
+ results
79
+ end
82
80
 
83
- def ==(other)
84
- eql?(other)
85
- end
81
+ def bind(batch)
82
+ Batch.new(@db, @commands.concat(batch.commands))
83
+ end
86
84
 
87
- def eql?(other)
88
- if other.class != self.class
89
- false
90
- else
91
- commands.length == other.commands.length &&
92
- commands.zip(other.commands).all? { |z| z[0] == z[1] }
85
+ def ==(other)
86
+ eql?(other)
87
+ end
88
+
89
+ def eql?(other)
90
+ if other.class != self.class
91
+ false
92
+ else
93
+ commands.length == other.commands.length &&
94
+ commands.zip(other.commands).all? { |z| z[0] == z[1] }
95
+ end
96
+ end
93
97
  end
94
98
  end
95
99
  end
@@ -1,31 +1,35 @@
1
- class BatchReference
2
- def initialize(command)
3
- @command = command
4
- end
1
+ module Neography
2
+ module Batch
3
+ class BatchReference
4
+ def initialize(command)
5
+ @command = command
6
+ end
5
7
 
6
- protected
7
- def after_submit_action
8
- @after_submit_action
9
- end
8
+ protected
9
+ def after_submit_action
10
+ @after_submit_action
11
+ end
10
12
 
11
- def command
12
- @command
13
- end
13
+ def command
14
+ @command
15
+ end
14
16
 
15
- public
16
- def after_submit(&after_commit_action)
17
- @after_submit_action = after_commit_action
18
- end
17
+ public
18
+ def after_submit(&after_commit_action)
19
+ @after_submit_action = after_commit_action
20
+ end
19
21
 
20
- def notify_after_submit(result)
21
- unless @after_submit_action.nil?
22
- @after_submit_action.call(result)
23
- end
24
- end
22
+ def notify_after_submit(result)
23
+ unless @after_submit_action.nil?
24
+ @after_submit_action.call(result)
25
+ end
26
+ end
25
27
 
26
- def ==(other)
27
- return false if other.nil?
28
- @after_submit_action.equal?(other.after_submit_action) &&
29
- @command.equal?(other.command)
28
+ def ==(other)
29
+ return false if other.nil?
30
+ @after_submit_action.equal?(other.after_submit_action) &&
31
+ @command.equal?(other.command)
32
+ end
33
+ end
30
34
  end
31
- end
35
+ end
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "neography-batch"
6
- s.version = "1.0.0"
6
+ s.version = "1.0.1"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = "Jorg Jenni"
9
9
  s.email = "jorg.jenni@jennius.co.uk"
@@ -0,0 +1,33 @@
1
+ require "spec_helper"
2
+
3
+ module Neography
4
+ module Batch
5
+ describe BatchReference do
6
+ describe "==" do
7
+ specify do
8
+ ref1 = BatchReference.new([:create, {"id" => 1}])
9
+ ref2 = BatchReference.new([:create, {"id" => 1}])
10
+ (ref1==ref2).should be_false
11
+ end
12
+ specify do
13
+ command = [:create, {"id" => 1}]
14
+ ref1 = BatchReference.new(command)
15
+ ref2 = BatchReference.new(command)
16
+ (ref1==ref2).should be_true
17
+ end
18
+ specify do
19
+ command = [:create, {"id" => 1}]
20
+ ref1 = BatchReference.new(command)
21
+ ref1.after_submit do
22
+ puts "Do this"
23
+ end
24
+ ref2 = BatchReference.new(command)
25
+ ref2.after_submit do
26
+ puts "Do something else"
27
+ end
28
+ (ref1==ref2).should be_false
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
data/spec/batch_spec.rb CHANGED
@@ -1,320 +1,316 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- class Function
4
- def self.persist_inserts(elements)
5
- batch = Batch.new
6
- elements.each do |e|
7
- batch << [:create_node, e]
8
- end
9
- return batch
10
- end
11
- end
12
3
 
13
- def create_batches(command1, command2, command3)
14
- batch1 = Batch.new do |b|
15
- b<<command1
16
- end
4
+ module Neography
5
+ module Batch
6
+ describe Batch do
17
7
 
18
- batch2 = Batch.new do |b|
19
- b<<command2
20
- end
8
+ def create_batches(command1, command2, command3)
9
+ batch1 = Batch.new do |b|
10
+ b<<command1
11
+ end
21
12
 
22
- batch3 = Batch.new do |b|
23
- b<<command3
24
- end
25
- return batch1, batch2, batch3
26
- end
13
+ batch2 = Batch.new do |b|
14
+ b<<command2
15
+ end
27
16
 
28
- describe "Batch" do
29
- let(:db) { Neography::Rest.new }
30
- subject { Batch.new(db) }
17
+ batch3 = Batch.new do |b|
18
+ b<<command3
19
+ end
20
+ return batch1, batch2, batch3
21
+ end
31
22
 
32
- describe "new" do
33
- it "should create unit when no block provided" do
34
- Batch.new.should == Batch.unit()
35
- end
36
- it "should call block with itself when block is provided" do
37
- command = [:command]
38
- batch_configured_without_block = Batch.new
39
- batch_configured_without_block << command
23
+ let(:db) { Neography::Rest.new }
24
+ subject { Batch.new(db) }
40
25
 
41
- batch_with_block = Batch.new do |b|
42
- b << command
43
- end
26
+ describe "new" do
27
+ it "should create unit when no block provided" do
28
+ Batch.new.should == Batch.unit()
29
+ end
30
+ it "should call block with itself when block is provided" do
31
+ command = [:command]
32
+ batch_configured_without_block = Batch.new
33
+ batch_configured_without_block << command
44
34
 
45
- batch_with_block.should == batch_configured_without_block
46
- end
47
- end
35
+ batch_with_block = Batch.new do |b|
36
+ b << command
37
+ end
48
38
 
49
- describe "==" do
50
- specify { (Batch.new==Batch.new).should be_true }
51
- specify { (Batch.new==[]).should be_false }
52
- specify do
53
- command1 = [:command_1]
54
- command2 = [:command_2]
55
- batch1 = Batch.new do |b|
56
- b << command1
57
- b << command2
58
- end
59
- batch2 = Batch.new do |b|
60
- b << command1
61
- b << command2
62
- end
63
- (batch1==batch2).should be_true
64
- end
65
- specify do
66
- batch1 = Batch.new do |b|
67
- b << [:command_1]
68
- b << [:command_2]
69
- end
70
- batch2 = Batch.new do |b|
71
- b << [:command_2]
72
- b << [:command_1]
73
- end
74
- (batch1==batch2).should be_false
75
- end
76
- specify do
77
- batch1 = Batch.new do |b|
78
- b << [:command_1, {:id => 7}]
79
- b << [:command_2, {:id => 9}]
39
+ batch_with_block.should == batch_configured_without_block
40
+ end
80
41
  end
81
- batch2 = Batch.new do |b|
82
- b << [:command_1, {:id => 7}]
83
- b << [:command_2, {:id => 8}]
42
+
43
+ describe "==" do
44
+ specify { (Batch.new==Batch.new).should be_true }
45
+ specify { (Batch.new==[]).should be_false }
46
+ specify do
47
+ command1 = [:command_1]
48
+ command2 = [:command_2]
49
+ batch1 = Batch.new do |b|
50
+ b << command1
51
+ b << command2
52
+ end
53
+ batch2 = Batch.new do |b|
54
+ b << command1
55
+ b << command2
56
+ end
57
+ (batch1==batch2).should be_true
58
+ end
59
+ specify do
60
+ batch1 = Batch.new do |b|
61
+ b << [:command_1]
62
+ b << [:command_2]
63
+ end
64
+ batch2 = Batch.new do |b|
65
+ b << [:command_2]
66
+ b << [:command_1]
67
+ end
68
+ (batch1==batch2).should be_false
69
+ end
70
+ specify do
71
+ batch1 = Batch.new do |b|
72
+ b << [:command_1, {:id => 7}]
73
+ b << [:command_2, {:id => 9}]
74
+ end
75
+ batch2 = Batch.new do |b|
76
+ b << [:command_1, {:id => 7}]
77
+ b << [:command_2, {:id => 8}]
78
+ end
79
+ (batch1==batch2).should be_false
80
+ end
81
+ specify do
82
+ batch1 = Batch.new { |b| b << [:command_1] }
83
+ batch2 = Batch.new { |b| b << [:command_2] }
84
+ (batch1==batch2).should be_false
85
+ end
86
+ specify do
87
+ batch1 = Batch.new { |b| b << [] }
88
+ batch2 = Batch.new { |b| b << [:command_2] }
89
+ (batch1==batch2).should be_false
90
+ end
91
+ specify do
92
+ batch1 = Batch.new
93
+ batch2 = Batch.new { |b| b << [:command_2] }
94
+ (batch1==batch2).should be_false
95
+ end
96
+ specify do
97
+ batch1 = Batch.new { |b| b << [:command_1] }
98
+ batch2 = Batch.new { |b| b << [] }
99
+ (batch1==batch2).should be_false
100
+ end
84
101
  end
85
- (batch1==batch2).should be_false
86
- end
87
- specify do
88
- batch1 = Batch.new { |b| b << [:command_1] }
89
- batch2 = Batch.new { |b| b << [:command_2] }
90
- (batch1==batch2).should be_false
91
- end
92
- specify do
93
- batch1 = Batch.new { |b| b << [] }
94
- batch2 = Batch.new { |b| b << [:command_2] }
95
- (batch1==batch2).should be_false
96
- end
97
- specify do
98
- batch1 = Batch.new
99
- batch2 = Batch.new { |b| b << [:command_2] }
100
- (batch1==batch2).should be_false
101
- end
102
- specify do
103
- batch1 = Batch.new { |b| b << [:command_1] }
104
- batch2 = Batch.new { |b| b << [] }
105
- (batch1==batch2).should be_false
106
- end
107
- end
108
102
 
109
- describe "bind" do
110
- let(:command1) { [:create_node, {"id" => 7}] }
111
- let(:command2) { [:create_unique_node, {"id" => 8}] }
112
- let(:command3) { [:create_node, {"id" => 9}] }
113
- it "on unit with f should be equal f" do
114
- batch1, _, _ = create_batches(command1, command2, command3)
103
+ describe "bind" do
104
+ let(:command1) { [:create_node, {"id" => 7}] }
105
+ let(:command2) { [:create_unique_node, {"id" => 8}] }
106
+ let(:command3) { [:create_node, {"id" => 9}] }
107
+ it "on unit with f should be equal f" do
108
+ batch1, _, _ = create_batches(command1, command2, command3)
115
109
 
116
- Batch.unit().bind(batch1).should == batch1
117
- end
110
+ Batch.unit().bind(batch1).should == batch1
111
+ end
118
112
 
119
- it "with unit should be equal Batch" do
120
- batch1, _, _ = create_batches(command1, command2, command3)
113
+ it "with unit should be equal Batch" do
114
+ batch1, _, _ = create_batches(command1, command2, command3)
121
115
 
122
- batch1.bind(Batch.unit()).should == batch1
123
- end
116
+ batch1.bind(Batch.unit()).should == batch1
117
+ end
124
118
 
125
- it "chained should be equal to bind with inner binds" do
126
- batch1, batch2, batch3 = create_batches(command1, command2, command3)
127
- result1 = batch3.bind(batch1).bind(batch2)
119
+ it "chained should be equal to bind with inner binds" do
120
+ batch1, batch2, batch3 = create_batches(command1, command2, command3)
121
+ result1 = batch3.bind(batch1).bind(batch2)
128
122
 
129
- batch1, batch2, batch3 = create_batches(command1, command2, command3)
130
- f = ->() { batch1 }
131
- g = ->() { batch2 }
132
- result2 = batch3.bind(batch1.bind(batch2))
123
+ batch1, batch2, batch3 = create_batches(command1, command2, command3)
124
+ f = ->() { batch1 }
125
+ g = ->() { batch2 }
126
+ result2 = batch3.bind(batch1.bind(batch2))
133
127
 
134
- result1.should == result2
135
- end
128
+ result1.should == result2
129
+ end
136
130
 
137
- describe "and submit" do
138
- it "should combine separate batches into one batch" do
139
- batch1 = Batch.new(db)
140
- ref11 = batch1 << [:create_node, {"id" => 1}]
141
- ref12 = batch1 << [:create_node, {"id" => 2}]
142
- batch1 << [:create_relationship, ref11, ref12, {}]
143
-
144
- batch2 = Batch.new(db)
145
- ref21 = batch2 << [:create_node, {"id" => 3}]
146
- ref22 = batch2 << [:create_node, {"id" => 4}]
147
- batch2 << [:create_relationship, ref21, ref22, {}]
148
-
149
- batch = batch2.bind(batch1)
150
-
151
- db.should_receive(:batch).with do |*commands|
152
- commands[0].should == [:create_node, {"id" => 3}]
153
- commands[1].should == [:create_node, {"id" => 4}]
154
- commands[2].should == [:create_relationship, "{0}", "{1}", {}]
155
- commands[3].should == [:create_node, {"id" => 1}]
156
- commands[4].should == [:create_node, {"id" => 2}]
157
- commands[5].should == [:create_relationship, "{3}", "{4}", {}]
158
- end.and_return([])
159
-
160
- batch.submit()
131
+ describe "and submit" do
132
+ it "should combine separate batches into one batch" do
133
+ batch1 = Batch.new(db)
134
+ ref11 = batch1 << [:create_node, {"id" => 1}]
135
+ ref12 = batch1 << [:create_node, {"id" => 2}]
136
+ batch1 << [:create_relationship, ref11, ref12, {}]
137
+
138
+ batch2 = Batch.new(db)
139
+ ref21 = batch2 << [:create_node, {"id" => 3}]
140
+ ref22 = batch2 << [:create_node, {"id" => 4}]
141
+ batch2 << [:create_relationship, ref21, ref22, {}]
142
+
143
+ batch = batch2.bind(batch1)
144
+
145
+ db.should_receive(:batch).with do |*commands|
146
+ commands[0].should == [:create_node, {"id" => 3}]
147
+ commands[1].should == [:create_node, {"id" => 4}]
148
+ commands[2].should == [:create_relationship, "{0}", "{1}", {}]
149
+ commands[3].should == [:create_node, {"id" => 1}]
150
+ commands[4].should == [:create_node, {"id" => 2}]
151
+ commands[5].should == [:create_relationship, "{3}", "{4}", {}]
152
+ end.and_return([])
153
+
154
+ batch.submit()
155
+ end
156
+ end
161
157
  end
162
- end
163
- end
164
- describe "find_reference" do
165
- it "should return reference when command is found" do
166
- ref1 = subject << [:create_node, {"id" => 7}]
167
- ref2 = subject << [:create_node, {"id" => 8}]
168
- subject << [:create_relationship, ref2, ref1, {}]
169
-
170
- result = subject.find_reference { |c| c[0] == :create_node && c[1]["id"] == 8 }
171
- result.should have(1).item
172
- result.should include(ref2)
173
- end
174
-
175
- it "should return empty when command is not found" do
176
- result = subject.find_reference { |c| c[0] == :create_node && c[1]["id"] == 8 }
177
- result.should be_empty
178
- end
179
-
180
- it "should return all reference when no predicate specified" do
181
- ref1 = subject << [:create_node, {"id" => 7}]
182
- ref2 = subject << [:create_node, {"id" => 8}]
158
+ describe "find_reference" do
159
+ it "should return reference when command is found" do
160
+ ref1 = subject << [:create_node, {"id" => 7}]
161
+ ref2 = subject << [:create_node, {"id" => 8}]
162
+ subject << [:create_relationship, ref2, ref1, {}]
163
+
164
+ result = subject.find_reference { |c| c[0] == :create_node && c[1]["id"] == 8 }
165
+ result.should have(1).item
166
+ result.should include(ref2)
167
+ end
183
168
 
184
- result = subject.find_reference()
185
- result.should have(2).item
186
- end
187
- end
188
- describe "<<" do
189
- it "should be the same as add when command is argument" do
190
- command = [:create_node]
191
- batch_used_with_add = Batch.new
192
- batch_used_with_add.add command
169
+ it "should return empty when command is not found" do
170
+ result = subject.find_reference { |c| c[0] == :create_node && c[1]["id"] == 8 }
171
+ result.should be_empty
172
+ end
193
173
 
194
- subject << command
174
+ it "should return all reference when no predicate specified" do
175
+ ref1 = subject << [:create_node, {"id" => 7}]
176
+ ref2 = subject << [:create_node, {"id" => 8}]
195
177
 
196
- subject.should == batch_used_with_add
197
- end
198
- it "should be the same as bind when command is Batch" do
199
- another_batch = Batch.new do |b|
200
- b << [:create_node]
178
+ result = subject.find_reference()
179
+ result.should have(2).item
180
+ end
201
181
  end
202
- subject << [:create_node]
182
+ describe "<<" do
183
+ it "should be the same as add when command is argument" do
184
+ command = [:create_node]
185
+ batch_used_with_add = Batch.new
186
+ batch_used_with_add.add command
203
187
 
204
- subject << another_batch
188
+ subject << command
205
189
 
206
- db.should_receive(:batch) do |*commands|
207
- commands.should have(2).items
208
- end.and_return([])
209
- subject.submit()
210
- end
211
- end
212
- describe "add and submit" do
213
- it "should raise exception when added element doesn't respond to :each" do
214
- -> { subject.add(1) }.should raise_exception(StandardError)
215
- end
190
+ subject.should == batch_used_with_add
191
+ end
192
+ it "should be the same as bind when command is Batch" do
193
+ another_batch = Batch.new do |b|
194
+ b << [:create_node]
195
+ end
196
+ subject << [:create_node]
216
197
 
217
- it "should add command to be submitted later" do
218
- command = [:create_node]
219
- subject.add(command)
198
+ subject << another_batch
220
199
 
221
- db.should_receive(:batch).with(command).and_return([])
200
+ db.should_receive(:batch) do |*commands|
201
+ commands.should have(2).items
202
+ end.and_return([])
203
+ subject.submit()
204
+ end
205
+ end
206
+ describe "add and submit" do
207
+ it "should raise exception when added element doesn't respond to :each" do
208
+ -> { subject.add(1) }.should raise_exception(StandardError)
209
+ end
222
210
 
223
- subject.submit()
224
- end
211
+ it "should add command to be submitted later" do
212
+ command = [:create_node]
213
+ subject.add(command)
225
214
 
226
- it "should resolve references to preceding commands" do
227
- jim = subject << [:create_node]
228
- john = subject << [:create_node]
229
- subject << [:create_relationship, john, jim]
215
+ db.should_receive(:batch).with(command).and_return([])
230
216
 
231
- db.should_receive(:batch).with do |*commands|
232
- relationship_cmd = commands.last
233
- relationship_cmd[1].should == "{1}" # john was resolved to index where john was created
234
- relationship_cmd[2].should == "{0}" # jim was resolved to index where john was created
235
- end.and_return([])
217
+ subject.submit()
218
+ end
236
219
 
237
- subject.submit()
238
- end
220
+ it "should resolve references to preceding commands" do
221
+ jim = subject << [:create_node]
222
+ john = subject << [:create_node]
223
+ subject << [:create_relationship, john, jim]
239
224
 
240
- context "when list 2 commands" do
241
- before do
242
- @john = subject.add [:create_node]
243
- @markus = subject.add [:create_node]
244
- end
245
- context "and another command is added" do
246
- let(:another_command) { [:create_relationship] }
247
- before do
248
- subject.add(another_command)
249
- end
250
- it "should contain 3 commands" do
251
225
  db.should_receive(:batch).with do |*commands|
252
- commands.should have(3).items
226
+ relationship_cmd = commands.last
227
+ relationship_cmd[1].should == "{1}" # john was resolved to index where john was created
228
+ relationship_cmd[2].should == "{0}" # jim was resolved to index where john was created
253
229
  end.and_return([])
254
230
 
255
231
  subject.submit()
256
232
  end
257
- it "should append command" do
258
- db.should_receive(:batch).with do |*commands|
259
- commands.last.should == another_command
260
- end.and_return([])
261
233
 
262
- subject.submit()
234
+ context "when list 2 commands" do
235
+ before do
236
+ @john = subject.add [:create_node]
237
+ @markus = subject.add [:create_node]
238
+ end
239
+ context "and another command is added" do
240
+ let(:another_command) { [:create_relationship] }
241
+ before do
242
+ subject.add(another_command)
243
+ end
244
+ it "should contain 3 commands" do
245
+ db.should_receive(:batch).with do |*commands|
246
+ commands.should have(3).items
247
+ end.and_return([])
248
+
249
+ subject.submit()
250
+ end
251
+ it "should append command" do
252
+ db.should_receive(:batch).with do |*commands|
253
+ commands.last.should == another_command
254
+ end.and_return([])
255
+
256
+ subject.submit()
257
+ end
258
+ end
263
259
  end
264
260
  end
265
- end
266
- end
267
- describe "submit" do
268
- it "should raise exception when server returns error" do
269
- # this provokes a server error, because the second create_unique_node cannot be reference in create_relationship
270
- subject << [:create_unique_node, "test", "id", "1", {}]
271
- subject << [:create_unique_node, "test", "id", "2", {}]
272
- subject << [:create_relationship, "test", "{0}", "{1}", {}]
273
-
274
- ->() { subject.submit() }.should raise_error(StandardError)
275
- end
276
- it "should notify reference handler" do
277
- node_1_result = nil
278
- node_2_result = nil
279
- ref1 = subject << [:create_node, {"id" => 1}]
280
- ref2 = subject << [:create_node, {"id" => 2}]
281
- ref1.after_submit do |node|
282
- node_1_result = node
283
- end
284
- ref2.after_submit do |node|
285
- node_2_result = node
286
- end
287
-
288
- subject.submit()
261
+ describe "submit" do
262
+ it "should raise exception when server returns error" do
263
+ # this provokes a server error, because the second create_unique_node cannot be reference in create_relationship
264
+ subject << [:create_unique_node, "test", "id", "1", {}]
265
+ subject << [:create_unique_node, "test", "id", "2", {}]
266
+ subject << [:create_relationship, "test", "{0}", "{1}", {}]
267
+
268
+ ->() { subject.submit() }.should raise_error(StandardError)
269
+ end
270
+ it "should notify reference handler" do
271
+ node_1_result = nil
272
+ node_2_result = nil
273
+ ref1 = subject << [:create_node, {"id" => 1}]
274
+ ref2 = subject << [:create_node, {"id" => 2}]
275
+ ref1.after_submit do |node|
276
+ node_1_result = node
277
+ end
278
+ ref2.after_submit do |node|
279
+ node_2_result = node
280
+ end
289
281
 
290
- node_1_result.should_not be_nil
291
- node_2_result.should_not be_nil
292
- node_1_result["data"]["id"].should == 1
293
- node_2_result["data"]["id"].should == 2
294
- end
282
+ subject.submit()
295
283
 
296
- it "should reset state of batch" do
297
- subject << [:create_node, {"id" => 1}]
298
- subject.submit()
284
+ node_1_result.should_not be_nil
285
+ node_2_result.should_not be_nil
286
+ node_1_result["data"]["id"].should == 1
287
+ node_2_result["data"]["id"].should == 2
288
+ end
299
289
 
300
- db.should_not_receive(:batch)
301
- result = subject.submit()
302
- result.should be_empty
303
- end
290
+ it "should reset state of batch" do
291
+ subject << [:create_node, {"id" => 1}]
292
+ subject.submit()
304
293
 
305
- context "one create_node added" do
306
- before { subject << [:create_node, {}] }
307
- it "should return created node" do
308
- result = subject.submit()
309
- result.should have(1).items
310
- end
311
- it "should create-node on Db" do
312
- result = subject.submit()
294
+ db.should_not_receive(:batch)
295
+ result = subject.submit()
296
+ result.should be_empty
297
+ end
313
298
 
314
- node = result.first
315
- node_id = db.get_id(node)
316
- count = db.execute_query("start s=node(#{node_id}) return count(*)")
317
- count["data"][0][0].should == 1
299
+ context "one create_node added" do
300
+ before { subject << [:create_node, {}] }
301
+ it "should return created node" do
302
+ result = subject.submit()
303
+ result.should have(1).items
304
+ end
305
+ it "should create-node on Db" do
306
+ result = subject.submit()
307
+
308
+ node = result.first
309
+ node_id = db.get_id(node)
310
+ count = db.execute_query("start s=node(#{node_id}) return count(*)")
311
+ count["data"][0][0].should == 1
312
+ end
313
+ end
318
314
  end
319
315
  end
320
316
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neography-batch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,7 @@ files:
75
75
  - lib/neography-batch/batch.rb
76
76
  - lib/neography-batch/batch_reference.rb
77
77
  - neography-batch.gemspec
78
+ - spec/batch_reference_spec.rb
78
79
  - spec/batch_spec.rb
79
80
  - spec/spec_helper.rb
80
81
  homepage: https://github.com/Enceradeira/neography-batch