neography-batch 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/lib/neography-batch/batch.rb +80 -76
- data/lib/neography-batch/batch_reference.rb +29 -25
- data/neography-batch.gemspec +1 -1
- data/spec/batch_reference_spec.rb +33 -0
- data/spec/batch_spec.rb +266 -270
- metadata +2 -1
data/Gemfile.lock
CHANGED
@@ -1,95 +1,99 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
10
|
+
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
12
|
+
def self.get_resolved_commands(commands)
|
13
|
+
result = []
|
14
|
+
reference_and_index = {}
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
34
|
+
protected
|
35
|
+
def commands
|
36
|
+
@commands
|
37
|
+
end
|
41
38
|
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
52
|
+
reference = BatchReference.new(command_or_batch)
|
53
|
+
@commands << {:cmd => command_or_batch, :reference => reference}
|
54
|
+
reference
|
55
|
+
end
|
56
|
+
end
|
59
57
|
|
60
|
-
|
58
|
+
def find_reference
|
59
|
+
@commands.select { |c| !block_given? || yield(c[:cmd]) }.map { |c| c[:reference] }
|
60
|
+
end
|
61
61
|
|
62
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
81
|
+
def bind(batch)
|
82
|
+
Batch.new(@db, @commands.concat(batch.commands))
|
83
|
+
end
|
86
84
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module Neography
|
2
|
+
module Batch
|
3
|
+
class BatchReference
|
4
|
+
def initialize(command)
|
5
|
+
@command = command
|
6
|
+
end
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
protected
|
9
|
+
def after_submit_action
|
10
|
+
@after_submit_action
|
11
|
+
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
def command
|
14
|
+
@command
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
public
|
18
|
+
def after_submit(&after_commit_action)
|
19
|
+
@after_submit_action = after_commit_action
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
data/neography-batch.gemspec
CHANGED
@@ -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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
4
|
+
module Neography
|
5
|
+
module Batch
|
6
|
+
describe Batch do
|
17
7
|
|
18
|
-
|
19
|
-
|
20
|
-
|
8
|
+
def create_batches(command1, command2, command3)
|
9
|
+
batch1 = Batch.new do |b|
|
10
|
+
b<<command1
|
11
|
+
end
|
21
12
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
return batch1, batch2, batch3
|
26
|
-
end
|
13
|
+
batch2 = Batch.new do |b|
|
14
|
+
b<<command2
|
15
|
+
end
|
27
16
|
|
28
|
-
|
29
|
-
|
30
|
-
|
17
|
+
batch3 = Batch.new do |b|
|
18
|
+
b<<command3
|
19
|
+
end
|
20
|
+
return batch1, batch2, batch3
|
21
|
+
end
|
31
22
|
|
32
|
-
|
33
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
35
|
+
batch_with_block = Batch.new do |b|
|
36
|
+
b << command
|
37
|
+
end
|
48
38
|
|
49
|
-
|
50
|
-
|
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
|
-
|
82
|
-
|
83
|
-
|
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
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
117
|
-
|
110
|
+
Batch.unit().bind(batch1).should == batch1
|
111
|
+
end
|
118
112
|
|
119
|
-
|
120
|
-
|
113
|
+
it "with unit should be equal Batch" do
|
114
|
+
batch1, _, _ = create_batches(command1, command2, command3)
|
121
115
|
|
122
|
-
|
123
|
-
|
116
|
+
batch1.bind(Batch.unit()).should == batch1
|
117
|
+
end
|
124
118
|
|
125
|
-
|
126
|
-
|
127
|
-
|
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
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
135
|
-
|
128
|
+
result1.should == result2
|
129
|
+
end
|
136
130
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
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
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
-
|
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
|
-
|
197
|
-
|
198
|
-
|
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
|
-
|
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
|
-
|
188
|
+
subject << command
|
205
189
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
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
|
-
|
218
|
-
command = [:create_node]
|
219
|
-
subject.add(command)
|
198
|
+
subject << another_batch
|
220
199
|
|
221
|
-
|
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
|
-
|
224
|
-
|
211
|
+
it "should add command to be submitted later" do
|
212
|
+
command = [:create_node]
|
213
|
+
subject.add(command)
|
225
214
|
|
226
|
-
|
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
|
-
|
232
|
-
|
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
|
-
|
238
|
-
|
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.
|
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
|
-
|
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
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
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
|
-
|
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
|
-
|
297
|
-
|
298
|
-
|
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
|
-
|
301
|
-
|
302
|
-
|
303
|
-
end
|
290
|
+
it "should reset state of batch" do
|
291
|
+
subject << [:create_node, {"id" => 1}]
|
292
|
+
subject.submit()
|
304
293
|
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
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
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
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.
|
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
|