diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index df9a2b6..9d100e2 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -352,9 +352,31 @@ def execute_script(script) result = post("/ext/GremlinPlugin/graphdb/execute_script", options) result == "null" ? nil : result end + + def batch(*args) + batch = [] + Array(args).each_with_index do |c,i| + batch << {:id => i}.merge(get_batch(c)) + end + options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json'} } + post("/batch", options) + end private + def get_batch(args) + case args[0] + when :get_node + {:method => "GET", :to => "/node/#{get_id(args[1])}", :body => args[2]} + when :create_node + {:method => "POST", :to => "/node/", :body => args[1]} + when :set_node_property + {:method => "PUT", :to => "/node/#{get_id(args[1])}/properties/#{args[2].keys.first}", :body => args[2].values.first} + when :reset_node_properties + {:method => "PUT", :to => "/node/#{get_id(args[1])}/properties", :body => args[2]} + end + end + def evaluate_response(response) code = response.code body = response.body diff --git a/spec/integration/rest_batch_spec.rb b/spec/integration/rest_batch_spec.rb index a7cd72e..158b072 100644 --- a/spec/integration/rest_batch_spec.rb +++ b/spec/integration/rest_batch_spec.rb @@ -7,27 +7,93 @@ describe "simple batch" do it "can get a single node" do - pending + new_node = @neo.create_node + new_node[:id] = new_node["self"].split('/').last + batch_result = @neo.batch [:get_node, new_node] + batch_result.first.should_not be_nil + batch_result.first.should have_key("id") + batch_result.first.should have_key("body") + batch_result.first.should have_key("from") + batch_result.first["body"]["self"].split('/').last.should == new_node[:id] end it "can get multiple nodes" do - pending - end + node1 = @neo.create_node + node1[:id] = node1["self"].split('/').last + node2 = @neo.create_node + node2[:id] = node2["self"].split('/').last - it "can create a single node" do - pending - end + batch_result = @neo.batch [:get_node, node1], [:get_node, node2] + batch_result.first.should_not be_nil + batch_result.first.should have_key("id") + batch_result.first.should have_key("body") + batch_result.first.should have_key("from") + batch_result.first["body"]["self"].split('/').last.should == node1[:id] + batch_result.last.should have_key("id") + batch_result.last.should have_key("body") + batch_result.last.should have_key("from") + batch_result.last["body"]["self"].split('/').last.should == node2[:id] - it "can create multiple nodes" do - pending end - it "can update a single node" do - pending + it "can create a single node" do + batch_result = @neo.batch [:create_node, {"name" => "Max"}] + batch_result.first["body"]["data"]["name"].should == "Max" end - it "can update multiple nodes" do - pending + it "can create multiple nodes" do + batch_result = @neo.batch [:create_node, {"name" => "Max"}], [:create_node, {"name" => "Marc"}] + batch_result.first["body"]["data"]["name"].should == "Max" + batch_result.last["body"]["data"]["name"].should == "Marc" + end + + it "can update a property of a node" do + new_node = @neo.create_node("name" => "Max") + batch_result = @neo.batch [:set_node_property, new_node, {"name" => "Marc"}] + batch_result.first.should have_key("id") + batch_result.first.should have_key("from") + existing_node = @neo.get_node(new_node) + existing_node["data"]["name"].should == "Marc" + end + + it "can update a property of multiple nodes" do + node1 = @neo.create_node("name" => "Max") + node2 = @neo.create_node("name" => "Marc") + batch_result = @neo.batch [:set_node_property, node1, {"name" => "Tom"}], [:set_node_property, node2, {"name" => "Jerry"}] + batch_result.first.should have_key("id") + batch_result.first.should have_key("from") + batch_result.last.should have_key("id") + batch_result.last.should have_key("from") + existing_node = @neo.get_node(node1) + existing_node["data"]["name"].should == "Tom" + existing_node = @neo.get_node(node2) + existing_node["data"]["name"].should == "Jerry" + end + + it "can reset the properties of a node" do + new_node = @neo.create_node("name" => "Max", "weight" => 200) + batch_result = @neo.batch [:reset_node_properties, new_node, {"name" => "Marc"}] + batch_result.first.should have_key("id") + batch_result.first.should have_key("from") + existing_node = @neo.get_node(new_node) + existing_node["data"]["name"].should == "Marc" + existing_node["data"]["weight"].should be_nil + end + + it "can reset the properties of multiple nodes" do + node1 = @neo.create_node("name" => "Max", "weight" => 200) + node2 = @neo.create_node("name" => "Marc", "weight" => 180) + batch_result = @neo.batch [:reset_node_properties, node1, {"name" => "Tom"}], [:reset_node_properties, node2, {"name" => "Jerry"}] + batch_result.first.should have_key("id") + batch_result.first.should have_key("from") + batch_result.last.should have_key("id") + batch_result.last.should have_key("from") + existing_node = @neo.get_node(node1) + existing_node["data"]["name"].should == "Tom" + existing_node["data"]["weight"].should be_nil + existing_node = @neo.get_node(node2) + existing_node["data"]["name"].should == "Jerry" + existing_node["data"]["weight"].should be_nil end it "can get a single relationship" do