Skip to content

Commit

Permalink
added create_relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Nov 17, 2010
1 parent 100cbbc commit 081d8e0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 4 additions & 1 deletion lib/neography/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
82 changes: 82 additions & 0 deletions spec/integration/rest_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 081d8e0

Please sign in to comment.