From 081d8e0706cf3db55e224cbc9a628bf5eb41a6b6 Mon Sep 17 00:00:00 2001 From: maxdemarzi Date: Wed, 17 Nov 2010 14:43:28 -0800 Subject: [PATCH] added create_relationship --- README.rdoc | 1 + lib/neography/rest.rb | 5 ++- spec/integration/rest_spec.rb | 82 +++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 7aa0a6b..9b372f1 100644 --- a/README.rdoc +++ b/README.rdoc @@ -43,6 +43,7 @@ A thin ruby wrapper Neography::Rest which tries to mirror the Neo4j Rest API and Neography::Rest.remove_node_properties(id) # Remove all properties of a node Neography::Rest.remove_node_properties(id, "weight") # Remove one property of a node Neography::Rest.remove_node_properties(id, ["weight","age"]) # Remove multiple properties of a node + Neography::Rest.create_relationship("friends", node1, node2) # Create a relationship between node1 and node2 ... and a work in progress more rubyish layer that's not quite ready for use yet. diff --git a/lib/neography/rest.rb b/lib/neography/rest.rb index a459d27..c1cd158 100644 --- a/lib/neography/rest.rb +++ b/lib/neography/rest.rb @@ -63,7 +63,10 @@ def delete_node(id) rescue_ij { delete("/node/#{id}") } end - + def create_relationship(type, from, to, props = nil) + options = { :body => {:to => Neography::Config.to_s + "/node/#{to}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} } + rescue_ij { post("/node/#{from}/relationships", options) } + end private diff --git a/spec/integration/rest_spec.rb b/spec/integration/rest_spec.rb index 35e6b97..63787db 100644 --- a/spec/integration/rest_spec.rb +++ b/spec/integration/rest_spec.rb @@ -164,6 +164,17 @@ existing_node.should be_nil end + it "cannot delete a node that has relationships" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + Neography::Rest.delete_node(new_node1[:id]).should be_nil + existing_node = Neography::Rest.get_node(new_node1[:id]) + existing_node.should_not be_nil + end + it "returns nil if it tries to delete a node that does not exist" do new_node = Neography::Rest.create_node new_node[:id] = new_node["self"].split('/').last @@ -184,4 +195,75 @@ end end + describe "delete_node!" do + it "can delete an unrelated node" do + new_node = Neography::Rest.create_node + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.delete_node!(new_node[:id]).should be_nil + existing_node = Neography::Rest.get_node(new_node[:id]) + existing_node.should be_nil + end + + it "can delete a node that has relationships" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + Neography::Rest.delete_node!(new_node1[:id]).should be_nil + existing_node = Neography::Rest.get_node(new_node1[:id]) + existing_node.should be_nil + end + + it "returns nil if it tries to delete a node that does not exist" do + new_node = Neography::Rest.create_node + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.delete_node!(new_node[:id].to_i + 1000).should be_nil + existing_node = Neography::Rest.get_node(new_node[:id].to_i + 1000) + existing_node.should be_nil + end + + it "returns nil if it tries to delete a node that has already been deleted" do + new_node = Neography::Rest.create_node + new_node[:id] = new_node["self"].split('/').last + Neography::Rest.delete_node!(new_node[:id]).should be_nil + existing_node = Neography::Rest.get_node(new_node[:id]) + existing_node.should be_nil + Neography::Rest.delete_node!(new_node[:id]).should be_nil + existing_node = Neography::Rest.get_node(new_node[:id]) + existing_node.should be_nil + end + end + + describe "create_relationship" do + it "can create an empty relationship" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id]) + new_relationship["start"].should_not be_nil + new_relationship["end"].should_not be_nil + end + + it "can create a relationship with one property" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010'}) + new_relationship["data"]["since"].should == '10-1-2010' + end + + it "can create a relationship with more than one property" do + new_node1 = Neography::Rest.create_node + new_node1[:id] = new_node1["self"].split('/').last + new_node2 = Neography::Rest.create_node + new_node2[:id] = new_node2["self"].split('/').last + new_relationship = Neography::Rest.create_relationship("friends", new_node1[:id], new_node2[:id], {"since" => '10-1-2010', "met" => "college"}) + new_relationship["data"]["since"].should == '10-1-2010' + new_relationship["data"]["met"].should == "college" + end + end + end \ No newline at end of file