Skip to content

Commit

Permalink
added build_node so we return neo_id on node creates and a few more t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
maxdemarzi committed Nov 15, 2010
1 parent bc1f9fc commit 3e4206d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/neography.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def evaluate_response(response)
require 'httparty'
require 'json'
require 'logger'
#require 'net-http-spy'
require 'net-http-spy'

#Net::HTTP.http_logger_options = {:verbose => true}
#Net::HTTP.http_logger_options = {:body => true}
Expand Down
47 changes: 34 additions & 13 deletions lib/neography/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ class << self

def new(*args)
if args[0].respond_to?(:each_pair) && args[0]
headers 'Content-Type' => 'application/json'
response = post("/node", :body => args[0].to_json)
options = { :body => args[0].to_json, :headers => {'Content-Type' => 'application/json'} }
response = post("/node", options)
evaluate_response(response)
response.parsed_response
build_node(response)
else
response = post("/node")
evaluate_response(response)
response.parsed_response
build_node(response)
end
end

def get_node(id)
def load(id)
begin
response = get("/node/#{id}")
evaluate_response(response)
response.parsed_response
build_node(response)
rescue
nil
end
Expand All @@ -34,15 +34,36 @@ def properties(id)
end

def set_properties(id, properties)
begin
response = put("/node/#{id}/properties", :body => properties.to_json)
evaluate_response(response)
response.parsed_response
rescue
nil
end
options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} }
response = put("/node/#{id}/properties", options)
evaluate_response(response)
response.parsed_response
end

def remove_property(id, property)
response = delete("/node/#{id}/properties/#{property}")
evaluate_response(response)
response.parsed_response
end

def del(id)
response = delete("/node/#{id}")
evaluate_response(response)
response.parsed_response
end

private

def build_node(response)
begin
node = response.parsed_response["data"]
rescue
node = Array.new
end
node[:neo_id] = response.parsed_response["self"].split('/').last
node
end

end
end
end
55 changes: 47 additions & 8 deletions spec/neography_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
require 'neography'

describe Neography::Neo do
# it "has a root node" do
# Neography::Neo.root_node.should include("reference_node")
# end
it "has a root node" do
Neography::Neo.root_node.should include("reference_node")
end
end

describe Neography::Node do
it "can create an empty node" do
Neography::Node.new.should include("data"=>{})
Neography::Node.new.should include(:neo_id)
end

it "can create a node with one property" do
Neography::Node.new(:name => "Max").should include("data"=>{"name"=>"Max"})
Neography::Node.new(:name => "Max").should include("name"=>"Max")
end

it "can create a node with more than one property" do
Neography::Node.new(:age => 31, :name => "Max").should include("data"=>{"age"=>31, "name"=>"Max"})
Neography::Node.new(:age => 31, :name => "Max").should include("age"=>31, "name"=>"Max")
end

it "can find a node by its id" do
Neography::Node.get_node(2).should include("self"=>"http://localhost:9999/node/2")
Neography::Node.load(2).should include(:neo_id=>"2")
end

it "fails to find a node that does not exist" do
Neography::Node.get_node(999).should be_nil
Neography::Node.load(999).should be_nil
end

it "can get a node's properties" do
Expand All @@ -45,6 +45,45 @@
Neography::Node.set_properties(999,{:age => 33, :name => "Harry"}).should be_nil
end

it "can delete a node's property" do
Neography::Node.set_properties(2, {:age => 32, :name => "Tom", :weight => 200} ).should be_nil
Neography::Node.remove_property(2, :weight).should be_nil
Neography::Node.properties(2).should_not include("weight"=>200)
end

it "returns nil if it tries to delete a property that does not exist" do
Neography::Node.set_properties(2, {:age => 32, :name => "Tom", :weight => 200} ).should be_nil
Neography::Node.remove_property(2, :height).should be_nil
end

it "returns nil if it tries to delete a property on a node that does not exist" do
Neography::Node.remove_property(9999, :height).should be_nil
end

it "can delete an unrelated node" do
newnode = Neography::Node.new
Neography::Node.del(newnode[:neo_id]).should be_nil
end

it "returns nil if it tries to delete a node that does not exist" do
Neography::Node.del(9999).should be_nil
end

it "returns nil if it tries to delete a node that has already been deleted" do
newnode = Neography::Node.new
Neography::Node.del(newnode[:neo_id]).should be_nil
Neography::Node.del(newnode[:neo_id]).should be_nil
end

it "returns nil if it tries to delete a node that has existing relationships" do
node1 = Neography::Node.new
node2 = Neography::Node.new
pending "create relationship from node1 to node2"
Neography::Node.del(node1[:neo_id]).should be_nil
Neography::Node.del(node2[:neo_id]).should be_nil
end




end

0 comments on commit 3e4206d

Please sign in to comment.